Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve always matches beyond the specified string when using named params #9

Closed
ariroffe opened this issue Nov 27, 2023 · 3 comments
Closed

Comments

@ariroffe
Copy link

For example, in the demo page:

{#if resolve($path, '/blog/:post')}
  <Post id={$params['post']}

the resolve call will return true for urls such as https://elegua.netlify.app/blog/fake-post-0/abc (see here or here)

If I'm not mistaken, can be fixed by adding a $ at the end of namedPath:

export function namedPath(route: string): RegExp {
  // checking for named component paths
  return RegExp(
    route.split('/').map((x) =>
        x.startsWith(':') ? `(?<${regExpEscape(x.slice(1, x.length))}>[a-zA-Z0-9][a-zA-Z0-9\_\-]*)` : regExpEscape(x)
      ).join(`\\/`) + '$'
  );
}
@howesteve
Copy link
Owner

Good catch, I've fixed it on the latest version and released it.

@ariroffe
Copy link
Author

ariroffe commented Nov 29, 2023

Thanks for the fix!

Sorry to be a pain, but I was just thinking, won't this still be a problem for regexp-based routes? Since they will not go into the namedPath function and therefore will not get the new $ at the end. For example, in the demo website, the route /users\/([0-9]+)/ still matches this: https://elegua.netlify.app/users/998/abc/def/

Maybe the fix should have been applied before this line in resolve:

const m = (route as RegExp).exec(path);

We should concatenate route with $ here instead of inside namedPath, before running exec

@howesteve
Copy link
Owner

regexp routes are not affected by namedPath(), so that will not change it's current behavior. The url:

https://elegua.netlify.app/users/998/abc/def/

... matches because it's defined as this in the demo:

{:else if resolve($path, /users\/(?<user_id>[0-9]+)/)}

If you want it not to match the extra path, just add the '$' to the regexp:

{:else if resolve($path, /users\/(?<user_id>[0-9]+)$/)}

... and I think it will function as you desire.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants