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
{{ message }}
Repository navigation
Add a function to map from path to routeId (inverse of resolveRoute fn) #11746
We have ?next=<path> routing in our app, and want to modify <path> on the basis of whether it is from a certain routeID. If the path does not match the relevant routeID, it is left unmodified.
Currently we hardcode a list of path prefixes that can't match the routeID. More generally, we would like to take advantage of SvelteKit's filesystem based routing to get a function that maps from a string path to its RouteID (if one exists).
A function of the form getRouteIdByPathname(pathname: string) -> routeId: string, or a function of that provides not just the top matching route but the actual list of matches: https://kit.svelte.dev/docs/advanced-routing#sorting.
pathname
routeId
/signin
/(auth)/signin
/signup
/(auth)/signup
/forgot
/(auth)/forgot
/archive/1
/archive/[page]
/archive/2
/archive/[page]
/news/sport/1
/news/[type]/[page]
/news/music/1
/news/[type]/[page]
We could then do if (getRouteIdByPathname(nextPath) === "/news/[type]/[page]") { ... }.
Alternatives considered
Keep maintaining the hardcoded list of path prefixes that shouldn't get modified
And while we are at it: can we also make it so that resolveRoute's id auto-completes known ids? I saw that all the generated $types.d.ts files already contain RouteId. Something like so:
typeAllRoutes=RouteId1|RouteId2| ... |(string&{})// ^^^^^^^^^^^^^ Always allow string// ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Imported from all $types.d.ts filesdeclarefunctionresolveRoute(route: AllRoutes,params: Record<string,string>): string
Would love to have this feature. Would be particularly useful for managing state with shallow routing. I have modals defined in the root +layout.svelte and match them based on the current page.state.modal.routeId. But I also have a separate page.state.modal.path, in order to know the exact path to preload my page data from. Ideally, I could solely rely on the complete path and map it back to a route id for matching.
Activity
mikerowe81 commentedon Feb 10, 2024
I would also like this feature.
Perhaps something like:
e.g.
returns
My use case is for route based permissions where I'd like to hide menu items or links if the user does not have permission to that route.
I'm sure the functionality is already built-in, it's just a matter of making the function public.
ptrxyz commentedon Aug 27, 2024
And while we are at it: can we also make it so that resolveRoute's id auto-completes known ids? I saw that all the generated $types.d.ts files already contain
RouteId. Something like so:ptrxyz commentedon Aug 27, 2024
As a workaround I made this: https://www.npmjs.com/package/vite-plugin-svelte-typed-routes
Simply use
resolveRouteand it will automagically autocomplete the routeId.hmnd commentedon Nov 27, 2025
Would love to have this feature. Would be particularly useful for managing state with shallow routing. I have modals defined in the root
+layout.svelteand match them based on the currentpage.state.modal.routeId. But I also have a separatepage.state.modal.path, in order to know the exact path to preload my page data from. Ideally, I could solely rely on the complete path and map it back to a route id for matching.resolveRoute()#14017 as a duplicate of this issuefeat: provide match function to allow the opposite of resolve (svelte…
feat: provide match function to allow the opposite of resolve (svelte…