You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: text/0000-routing.md
+61-55Lines changed: 61 additions & 55 deletions
Original file line number
Diff line number
Diff line change
@@ -2,22 +2,22 @@
2
2
- RFC PR: (leave this empty)
3
3
- Svelte Issue: (leave this empty)
4
4
5
-
# Refactoring out Sapper's Routing
5
+
# Refactoring out SvelteKit's Routing
6
6
7
7
## Summary
8
8
9
-
Split Sapper's routing functionality into a standalone library and make it possible to use Sapper's routing outside of Sapper.
9
+
Split SvelteKit's routing functionality into a standalone library and make it possible to use SvelteKit's routing outside of SvelteKit.
10
10
11
11
## Motivation
12
12
13
13
Benefits include:
14
14
15
-
* Making it possible to use Sapper's routing outside of Sapper. The routing component should be able to be used in a client-side only application
16
-
* Making Sapper's routing more configurable. E.g. the user may wish to set a custom header on a certain route. We could expose the router component in the application and allow the user to set configuration on a per-route basis along the lines of `router.header('/[list]/[page]', {'Cache-Control': max-age=600'})`
15
+
* Making it possible to use SvelteKit's routing outside of SvelteKit. The routing component should be able to be used in a client-side only application
16
+
* Making SvelteKit's routing more configurable. E.g. the user may wish to set a custom header on a certain route. We could expose the router component in the application and allow the user to set configuration on a per-route basis along the lines of `router.header('/[list]/[page]', {'Cache-Control': max-age=600'})`
17
17
* Other requests users have for configuring the router include [routes aliases](https://github.com/sveltejs/sapper/issues/1450) and [configuring trailing slashes](https://github.com/sveltejs/sapper/issues/519), which we could expose APIs for as well
18
18
* Improved testability. Right now to test routing functionality you need a sample application and all tests are integration tests
19
-
* I believe it would become possible to make it such that Sapper's routing can be used with or without generating it from the file system though this is perhaps not a primary goal.
20
-
* Finally, Sapper's routing and Routify are quite similar. It may be possible to combine these two systems if everyone involved were open to such a possibility. I think it'd be good for the Svelte community to have a single solution because it means that solution would get more developer support than two individual solutions. Right now [there are many Svelte routing solutions](https://twitter.com/lihautan/status/1315482668440580096?s=19).
19
+
* I believe it would become possible to make it such that SvelteKit's routing can be used with or without generating it from the file system though this is perhaps not a primary goal.
20
+
* Finally, SvelteKit's routing and Routify are quite similar. It may be possible to combine these two systems if everyone involved were open to such a possibility. I think it'd be good for the Svelte community to have a single solution because it means that solution would get more developer support than two individual solutions. Right now [there are many Svelte routing solutions](https://twitter.com/lihautan/status/1315482668440580096?s=19).
21
21
22
22
## Detailed design
23
23
@@ -29,78 +29,91 @@ This could be split up into multiple PRs that are smaller and easier to review.
29
29
30
30
Remove need to specify CSS files in routing components.
31
31
32
-
Update: this has been completed in [#1508](https://github.com/sveltejs/sapper/pull/1508)
32
+
Update: this was completed in [#1508](https://github.com/sveltejs/sapper/pull/1508).
33
33
34
34
35
35
#### Step 2
36
36
37
-
Put all routing information in a single routing component. Historically it has been split between `start` and `app`.
37
+
Put all routing information in a single routing component. Historically, in Sapper, it has been split between `start` and `app`.
38
38
39
-
Update: this has been largely completed in [#1434](https://github.com/sveltejs/sapper/pull/1434). We can probably still clean up the prefetching code, etc.
39
+
Update: this has basically been completed with the router now living `packages/kit/runtime/internal/router`
40
40
41
41
42
42
#### Step 3
43
43
44
-
Add an API to register routes and make the generated code call the router rather than the router calling the generated code.
44
+
Parse routes at runtime to simplify API.
45
45
46
-
Right now, the generated `manifest-client.mjs` contains something like:
46
+
Right now, the generated `manifest.js` contains something like:
It would be nice to invert this such that the generated code calls the router instead of the router importing the generated code:
90
+
This API is a bit complex. However, it could be greatly simplified by making the route parsing be part of the router runtime instead of happening at compile time:
This API is a bit complex. However, it could be greatly simplified by making the route parsing be part of the router runtime instead of happening at compile time:
106
+
The `pattern` is needed on the server-side too, so we will still have to include it in the manifest.
Refactor out routes generation into separate plugin. Publish components separately.
102
111
103
-
#### Step 4
112
+
Review the API and make sure we like it. There's probably some minor cleanup to do before exposing the routing API more broadly
113
+
114
+
We could potentially have a library for the core routing functionality. And then a second for the file-system-based routes generator.
115
+
116
+
#### Step 5 - Optional
104
117
105
118
Unify the client and server routing APIs.
106
119
@@ -113,13 +126,6 @@ The server manifest also differs in a number of unnecessary ways:
113
126
114
127
We can remove the unnecessary differences. We will still need different client-side and server-side router instantiations registering their respective components, but they can utilize the same `router.register` API. On the client-side we can call `router.navigate`. On the server-side, we can just ask the router to return the registered component for a given route and let `get_page_handler` continue to handle it as it does today.
115
128
116
-
#### Step 5
117
-
118
-
Refactor out routes generation into separate plugin. Publish components separately.
119
-
120
-
Review the API and make sure we like it. There's probably some minor cleanup to do before exposing the routing API more broadly
121
-
122
-
We could potentially have a library for the core routing functionality. And then a second for the file-system-based routes generator.
0 commit comments