Skip to content

Add a function to map from path to routeId (inverse of resolveRoute fn) #11746

Description

@thundergolfer

Describe the problem

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).

This problem has come up before on Reddit: https://www.reddit.com/r/sveltejs/comments/14azrdg/how_can_i_determine_the_corresponding_route_id/

Describe the proposed solution

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

Importance

would make my life easier

Additional Information

No response

Activity

  1. mikerowe81 commented on Feb 10, 2024

    @mikerowe81

    I would also like this feature.

    Perhaps something like:

    function resolvePath(
        path: string
    ): {
        id: string
        params: Record<string, string | undefined>
    };
    

    e.g.

    resolvePath("/blog/page1")
    

    returns

    {
        id: "/blog/[slug]",
        params: {
            slug: "page1"
        }
    }
    

    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.

  2. ptrxyz commented on Aug 27, 2024

    @ptrxyz

    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:

    type AllRoutes = RouteId1 | RouteId2 | ... | (string & {})
    //                                           ^^^^^^^^^^^^^ Always allow string
    //               ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Imported from all $types.d.ts files
    declare function resolveRoute(route: AllRoutes, params: Record<string, string>): string
  3. ptrxyz commented on Aug 27, 2024

    @ptrxyz

    As a workaround I made this: https://www.npmjs.com/package/vite-plugin-svelte-typed-routes

    // vite.config.ts
    import vitePluginSvelteTypedRoutes from 'vite-plugin-svelte-typed-routes'
    export default defineConfig({
      plugins: [
        vitePluginSvelteTypedRoutes({
          runPrettier: true
        }), 
        //other plugins here
      ]
    })

    Simply use resolveRoute and it will automagically autocomplete the routeId.

  4. hmnd commented on Nov 27, 2025

    @hmnd
    Contributor

    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.

  5. marked Functions for routes #11141 as a duplicate of this issue on Dec 12, 2025
  6. added theissue type on Feb 12, 2026
  7. added a commit that references this issue on Feb 14, 2026
    e7cbc78
  8. added a commit that references this issue on Mar 6, 2026
    fdc116a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions