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
This function runs before `handle` and allows you to change how URLs are translated into routes. The returned pathname (which defaults to `url.pathname`) is used to select the route and its parameters. In order to use this hook, you need to opt in to [server-side route resolution](configuration#router), which means a server request is made before each navigation in order to invoke the server `reroute` hook.
153
+
154
+
In contrast to the [universal `reroute` hook](#universal-hooks-reroute), it
155
+
156
+
- is allowed to be async (though you should take extra caution to not do long running operations here, as it will delay navigation)
157
+
- also receives headers and cookies (though you cannot modify them)
158
+
159
+
For example, you might have two variants of a page via `src/routes/sale/variant-a/+page.svelte` and `src/routes/sale/variant-b/+page.svelte`, which should be accessible as `/sale` and want to use a cookie to determine what variant of the sales page to load. You could implement this with `reroute`:
Using `reroute` will _not_ change the contents of the browser's address bar, or the value of `event.url`.
176
+
150
177
## Shared hooks
151
178
152
179
The following can be added to `src/hooks.server.js` _and_ `src/hooks.client.js`:
@@ -273,6 +300,11 @@ The following can be added to `src/hooks.js`. Universal hooks run on both server
273
300
274
301
This function runs before `handle` and allows you to change how URLs are translated into routes. The returned pathname (which defaults to `url.pathname`) is used to select the route and its parameters.
275
302
303
+
In contrast to the [server `reroute` hook](#server-hooks-reroute), it
304
+
305
+
- must be synchronous
306
+
- only receives the URL
307
+
276
308
For example, you might have a `src/routes/[[lang]]/about/+page.svelte` page, which should be accessible as `/en/about` or `/de/ueber-uns` or `/fr/a-propos`. You could implement this with `reroute`:
throw new Error('Cannot define "reroute" in both server hooks and universal hooks. Remove the function from one of the files.');
80
+
}
81
+
82
+
if (server_reroute && ${config.kit.router.resolution==='client'}) {
83
+
throw new Error('Cannot define "reroute" in server hooks when router.resolution is set to "client". Remove the function from the file, or set router.resolution to "server".');
* The [`reroute`](https://svelte.dev/docs/kit/hooks#Server-hooks-reroute) hook on the server allows you to modify the URL before it is used to determine which route to render.
821
+
* In contrast to the universal [`reroute`](https://svelte.dev/docs/kit/hooks#Universal-hooks-reroute) hook, it
822
+
* - is allowed to be async (though you should take extra caution to not do long running operations here, as it will delay navigation)
823
+
* - also receives headers and cookies (though you cannot modify them)
824
+
*
825
+
* @since 2.18.0
826
+
*/
827
+
exporttypeServerReroute=(event: {
828
+
url: URL;
829
+
headers: Omit<Headers,'set'|'delete'|'append'>;
830
+
cookies: {get: Cookies['get']};
831
+
})=>MaybePromise<void|string>;
832
+
819
833
/**
820
834
* The [`transport`](https://svelte.dev/docs/kit/hooks#Universal-hooks-transport) hook allows you to transport custom types across the server/client boundary.
* The [`reroute`](https://svelte.dev/docs/kit/hooks#Server-hooks-reroute) hook on the server allows you to modify the URL before it is used to determine which route to render.
803
+
* In contrast to the universal [`reroute`](https://svelte.dev/docs/kit/hooks#Universal-hooks-reroute) hook, it
804
+
* - is allowed to be async (though you should take extra caution to not do long running operations here, as it will delay navigation)
805
+
* - also receives headers and cookies (though you cannot modify them)
806
+
*
807
+
* @since 2.18.0
808
+
*/
809
+
exporttypeServerReroute=(event: {
810
+
url: URL;
811
+
headers: Omit<Headers,'set'|'delete'|'append'>;
812
+
cookies: {get: Cookies['get']};
813
+
})=>MaybePromise<void|string>;
814
+
801
815
/**
802
816
* The [`transport`](https://svelte.dev/docs/kit/hooks#Universal-hooks-transport) hook allows you to transport custom types across the server/client boundary.
0 commit comments