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
Sometimes, it's useful to know what route a path would go to, without navigating there.
For example, commonly, if the user is not authenticated, he is redirected to /login?redirect=<previous-path>. Here, the +page.server.ts for the login page has access to the redirect param, but it's not very useful as you can't tell what route the user actually wants to go to.
The only way to perform actions based on redirect route right now is to manually match routes, e.g. using a regex, which is not ideal. If there is a reroute hook, this is even trickier and it's easy to introduce bugs.
Describe the proposed solution
A similar resolvePath() function that returns the route ID and params, or null if the path doesn't match any routes.
I think a great example is the auth hook. Generally one looks like so:
if(path.startsWith("/home")||path=="/dashboard"|| ...){// ... authenticate user, place result in event.locals ...if(!authenticated)redirect(303,"/login?redirect="+encodeUriComponent(path))// let request continuereturnhandle(event)}
This is error-prone, requires manual path matching and needs to be updated everytime new routes are added or changed. Instead, it would be great to know the actual route matched, as it's very easy to group all authenticated routes, for example:
Then, all you need to do is resolvePath(path).startsWith("(auth)"). Right now, to implement this, one needs to re-do SvelteKit's job to figure out the route.
Also, if added, this function should match partial routes (i.e. that 404-match to an +error.svelte). For example, for /home/unknown, resolvePath() could return:
result={type: "not-found",// can also be 'page' or 'server'route: {id: "(auth)/home"params: {}}}
Right, my bad, shouldn't have given that example. The initial example (my original use-case) is to parse the redirect URL query parameter before redirecting to it.
Alright. In my specific case, certain pages are public. They still ask the user to log in, but the login page contains an additional "continue as guest" button. This functionality is enabled based on whether the page the user is trying to access is public, which depends on the route.
If you put all public pages in a group like src/routes/(public)/... you can add a layout.ts with a load function which returns a flag. That way all public pages have that flag in their route data.
Not sure if that solves your problem, it's just what comes up to my mind right now
When the user tries to access a page that could be accessed as a guest, he would try to navigate to that page and somewhere in your app, probably the handle hook or a load function would intercept that request and throws a redirect. At that point you have the target route id right? There you could append a query parameter to the redirect to /login to indicate that the requested page could be accessed without authentication. But personally, I'd prefer to render the requested url but with something like a modal on top which prompts the user to either go to the login page or continue as a guest.
There you could append a query parameter to the redirect to /login to indicate that the requested page could be accessed without authentication.
That would work. I was hoping to put logic soley into the login page, but oh well.
I'd prefer to render the requested url but with something like a modal on top which prompts the user to either go to the login page or continue as a guest.
That's definitely an option, but sadly doesn't work with my app requirements. To access a page, a valid account is needed (to store/record various information). Clicking on "continue as guest" creates a temporary guest account, which allows the user to access the page.
Activity
7nik commentedon Jul 19, 2025
Can you give an example where you need this?
rChaoz commentedon Jul 19, 2025
I think a great example is the auth hook. Generally one looks like so:
This is error-prone, requires manual path matching and needs to be updated everytime new routes are added or changed. Instead, it would be great to know the actual route matched, as it's very easy to group all authenticated routes, for example:
Then, all you need to do is
resolvePath(path).startsWith("(auth)"). Right now, to implement this, one needs to re-do SvelteKit's job to figure out the route.Also, if added, this function should match partial routes (i.e. that 404-match to an
+error.svelte). For example, for/home/unknown, resolvePath() could return:PatrickG commentedon Jul 19, 2025
You can use
event.route.idfor that.rChaoz commentedon Jul 19, 2025
Right, my bad, shouldn't have given that example. The initial example (my original use-case) is to parse the redirect URL query parameter before redirecting to it.
david-plugge commentedon Jul 20, 2025
But why do you need that
rChaoz commentedon Jul 20, 2025
Alright. In my specific case, certain pages are public. They still ask the user to log in, but the login page contains an additional "continue as guest" button. This functionality is enabled based on whether the page the user is trying to access is public, which depends on the route.
david-plugge commentedon Jul 20, 2025
You could group public pages and set a specific value in the layout load function
rChaoz commentedon Jul 20, 2025
I'm not exactly sure what you mean? Which layout load function?
david-plugge commentedon Jul 20, 2025
If you put all public pages in a group like
src/routes/(public)/...you can add a layout.ts with a load function which returns a flag. That way all public pages have that flag in their route data.Not sure if that solves your problem, it's just what comes up to my mind right now
rChaoz commentedon Jul 20, 2025
I see. It doesn't help in my case as, again, I want to resolve a route that is not active/not navigated to, it's in a query param.
david-plugge commentedon Jul 21, 2025
But I don't get why you need that. Do you want to show a modal before navigating? Maybe a small reproduction could help to explain your flow
david-plugge commentedon Jul 21, 2025
When the user tries to access a page that could be accessed as a guest, he would try to navigate to that page and somewhere in your app, probably the handle hook or a load function would intercept that request and throws a redirect. At that point you have the target route id right? There you could append a query parameter to the redirect to /login to indicate that the requested page could be accessed without authentication. But personally, I'd prefer to render the requested url but with something like a modal on top which prompts the user to either go to the login page or continue as a guest.
rChaoz commentedon Jul 21, 2025
That would work. I was hoping to put logic soley into the login page, but oh well.
That's definitely an option, but sadly doesn't work with my app requirements. To access a page, a valid account is needed (to store/record various information). Clicking on "continue as guest" creates a temporary guest account, which allows the user to access the page.
sacrosanctic commentedon Dec 12, 2025
@teemingc duplicate of #11746