Skip to content

Opposite of resolveRoute() #14017

Closed as duplicate
Closed as duplicate

Description

@rChaoz

Describe the problem

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.

Importance

would make my life easier

Activity

  1. transferred this issue fromsveltejs/svelteon Jul 19, 2025
  2. 7nik commented on Jul 19, 2025

    @7nik

    Can you give an example where you need this?

  3. rChaoz commented on Jul 19, 2025

    @rChaoz
    ContributorAuthor

    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 continue
        return handle(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:

    - (auth)/
    | - +layout.svelte
    | - home/
    | | - +error.svelte
    | | - +page.svelte
    | - dashboard/
    | | - +page.svelte
    - (public)/
    | - +page.svelte
    | - other/
    | | - +page.svelte
    

    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: {}
      }
    }
  4. PatrickG commented on Jul 19, 2025

    @PatrickG
    Member

    You can use event.route.id for that.

    if (event.route.id?.startsWith('/(auth)')) { ...
  5. rChaoz commented on Jul 19, 2025

    @rChaoz
    ContributorAuthor

    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.

  6. david-plugge commented on Jul 20, 2025

    @david-plugge
    Contributor

    But why do you need that

  7. rChaoz commented on Jul 20, 2025

    @rChaoz
    ContributorAuthor

    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.

  8. david-plugge commented on Jul 20, 2025

    @david-plugge
    Contributor

    You could group public pages and set a specific value in the layout load function

  9. rChaoz commented on Jul 20, 2025

    @rChaoz
    ContributorAuthor

    I'm not exactly sure what you mean? Which layout load function?

  10. david-plugge commented on Jul 20, 2025

    @david-plugge
    Contributor

    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

  11. rChaoz commented on Jul 20, 2025

    @rChaoz
    ContributorAuthor

    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.

  12. david-plugge commented on Jul 21, 2025

    @david-plugge
    Contributor

    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

  13. david-plugge commented on Jul 21, 2025

    @david-plugge
    Contributor

    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.

  14. rChaoz commented on Jul 21, 2025

    @rChaoz
    ContributorAuthor

    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.

  15. sacrosanctic commented on Dec 12, 2025

    @sacrosanctic
    Contributor

    @teemingc duplicate of #11746

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions