-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat: add reroute
hook
#11537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add reroute
hook
#11537
Conversation
🦋 Changeset detectedLatest commit: 689811e The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
in the PR description: //file: src/hooks.server.js
export const rewriteUrl = ({ url }) => url; should this be: //file: src/hooks.router.js
export const rewriteUrl = ({ url }) => url; perhaps? |
When
|
500 Response |
I'm not sure about the name |
Re the filename, I'm wondering whether just |
I like I am a bit worried that this will cause some confusion with shared hooks. People might mistakenly believe that the isomorphic hooks file is where shared hooks can be added. The phrasing in the docs will need to be precise to avoid this. Update: Changed it to just |
I'm open to alternate name proposals, but I don't think having |
How about type HandleRouteMatch = ({ url, routes, matchRoute }: { url: URL, routes: Route[], matchRoute: () => MatchedRoute }): MatchedRoute | null; So function matchRoute(routes: Route[], url: URL) {
return routes.find((route) => route.exec(get_url_path(url.pathname)));
} And the default hook would be export function handleRouteMatch({ url, routes, matchRoute }) {
return matchRoute(routes, url);
} |
I don't see the advantage of resolving a route instead of returning a url that then get's resolved to a route by the framework. // src/hooks.js
export function handleRouteMatch({ url, routes, matchRoute }) {
return matchRoute(routes, rewriteUrl({ url }));
}
const mapping = {
"/en/about" : "/en/about",
"/de/ueber-uns": "/de/about",
}
function rewriteUrl({ url }) {
url.pathname = mapping[url.pathname] ?? url.pathname
return url
} |
Yea, I just thought it might open this hook for other purposes. |
Unless there's something that prevents you from doing it, we normally try to add new tests to existing test apps rather than adding a new test app. There's some amount of overhead for each new test app, so it helps keep the test suite from getting too slow if we can share. Hopefully you could put the new test in |
Related to sveltejs/kit#11537 Also turns the error on unknown exports into a warning - people could be stuck on old versions of language tools while new features are added, and SvelteKit will throw a dev time error anyway
packages/kit/test/apps/basics/src/routes/reroute/error-handling/+page.svelte
Outdated
Show resolved
Hide resolved
thank you! |
Hello everyone. Thanks for the good work. Given the example usage in the docs /** @type {Record<string, string>} */
const translated = {
'/en/about': '/en/about',
'/de/ueber-uns': '/de/about',
'/fr/a-propos': '/fr/about',
};
/** @type {import('@sveltejs/kit').Reroute} */
export function reroute({ url }) {
if (url.pathname in translated) {
return translated[url.pathname];
}
}
---UPDATE--- I've tried a working redirect setup in import { redirect } from '@sveltejs/kit';
/** @type {import('@sveltejs/kit').Handle} */
export async function handle({ event, resolve }) {
if (event.url.pathname === '/de/about') {
redirect(301, '/de/ueber-uns')
} else if (event.url.pathname === '/fr/about') {
redirect(301, '/de/a-propos')
}
return resolve(event);
} Without reading |
Good Point! There are two ways I can think of to address this:
|
Thank you @LorisSigrist. (2) does seem more convenient, however, we still need to redirect the
I see, thanks for clarifying. It is certainly something I struggle to wrap my head around at first, perhaps due to the way |
Yes, you would still need to redirect Your understanding diagram looks mostly right, except that |
(Per #11595, it might be desirable to have access to cookies etc — perhaps we need to create the event before |
Related to sveltejs/kit#11537 Also turns the error on unknown exports into a warning - people could be stuck on old versions of language tools while new features are added, and SvelteKit will throw a dev time error anyway. Also commented out that never worked.
This PR changes the generated Is there somewhere else this type of code can be placed? |
Closes #5703
Continuation of #11396
This PR adds a
rewriteUrl
hook that allows you to edit a url before SvelteKit uses it to render a route. This is different from a redirect, as it does not change the URL shown to the user, or change the value ofevent.url
. Only the route resolution logic is affected.Because this hooks is required both on the server and the client, this PR adds a new
hooks.js
file for isomorphic hooks. This can be changed if that's undesired.API
The returned URL get's used to determine which route to render.
Notes:
rewriteUrl
throws an error on the client it falls back to a native navigationrewriteUrl
throws an error on the server a 500 response is returnedPlease don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpm changeset
and following the prompts. Changesets that add features should beminor
and those that fix bugs should bepatch
. Please prefix changeset messages withfeat:
,fix:
, orchore:
.