TanStack / router

🤖 Fully typesafe Router for React (and friends) w/ built-in caching, 1st class search-param APIs, client-side cache integration and isomorphic rendering.
https://tanstack.com/router
MIT License
7.19k stars 486 forks source link

`buildLocation` does not encode slashes in path params #1515

Closed andrejilderda closed 3 weeks ago

andrejilderda commented 3 weeks ago

Describe the bug

When using buildLocation() with params, I expect the values of the params to be encoded. This is the case for spaces and other special characters, but not for slashes. This results in invalid router url's.

Your Example Website or App

https://stackblitz.com/edit/github-lxalsq?file=src%2Froutes%2Findex.tsx

Steps to Reproduce the Bug or Issue

Also see the StackBlitz example above.

  1. Use buildLocation with a dynamic route segment and pass a string with a slash.
    router.buildLocation({
    from: '<routeWithDynamicSegment>', 
    params: { dynamicSegment: 'foo bar/baz' }
    })
  2. Notice how the generated .href is encoded, except for the slash: /foo%20bar/baz.
  3. The naive solution would be to apply the encoding to the parameters passed to buildLocation():
    ...
    params: { dynamicSegment: encodeURIComponent(stringWithSlash) }}) };
    ...
  4. But this results in the encoding of the encoded characters…: /foo%2520bar%252Fbaz

Expected behavior

I expected the slash to be encoded for every param passed to buildLocation. Going further on the example above I expected the output to be: /foo%20bar%2Fbaz

Screenshots or Videos

No response

Platform

Additional context

Thanks for all your work! 🫶️

SeanCassiere commented 3 weeks ago

Released the fix for this in the 1.30.1 release.

andrejilderda commented 3 weeks ago

Wow, thanks for the super fast fix @SeanCassiere!! 🥇