Skip to content

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

@thundergolfer

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions