Is there anyway to handle an optional trailing`/` in route path without using `*`?
See original GitHub issueI have this routes definition
const routes = {
'/foo': () => <Foo />,
'/bar': () => <Bar />,
'/qux': () => <Qux />,
'/qux/:quxId': ({ quxId }) => <Bar quxId={quxId}/>,
};
As you can see there is a special case when 'qux/:quxId'
routes to a different view than '/qux'
(In fact it routes to the view for '/bar'
).
I want to be able to handle any of the routes also being visited with a trailing /
(for example '/foo/'
).
I can get it to work with *
but it has a couple of downsides.
- Because
*
can match anything, not just the trailing/
, routes are able to be activated with an invalid path (/foot
for instance) - It depends on ordering
'/qux/:quxId'
before'/qux'
so that the*
doesn’t clobber the more specific path. This feels pretty fragile, especially because routes is an object, not an array.
The other alternative I can see is to define 2 paths for each route, one with the slash and one without.
const routes = {
'/foo': () => <Foo />,
'/foo/': () => <Foo />,
'/bar': () => <Bar />,
'/bar/': () => <Bar />,
'/qux': () => <Qux />,
'/qux/': () => <Qux />,
'/qux/:quxId': ({ quxId }) => <Bar quxId={quxId}/>,
};
Obviously that is not very appealing
Is there any other way to address this special case of optionally allowing a trailing slash, or is it something you would consider adding?
Thanks as always 👍
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:6 (4 by maintainers)
Top Results From Across the Web
React Router with optional path parameter - Stack Overflow
My question is, can we declare it in one route? If I add only the second row then the route without the parameter...
Read more >Make trailing slash optional · Issue #820 · remix-run/react-router
Desired behavior: Always allow an optional trailing slash; Never add an optional trailing slash when generating links via makePath.
Read more >Trailing / in route patterns - Slim Framework
Slim treats a URL pattern with a trailing slash as different to one without. That is, /user and /user/ are different and so...
Read more >hapi — Ignore Trailing Slashes on Route Paths - Future Studio
The „bad solution“ is to just use a single route with an optional path parameter instead of two separate routes. Within the route...
Read more >Router options - router5
By default, the router is not in "strict match" mode. If you want trailing slashes to not be optional, you can set strictTrailingSlash...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yeah I think if the solution was to do
'/about/?'
you would need to expose the fact that the path matcher is a regex in your API which probably creates maintenance headaches for you.I think it’s the kind of thing you would want to apply to all your paths if you want it (I know that would be the case for us), so an option for
useRoutes
would be easier than anything that has to be done per route definition IMO.So that’s a vote for a
matchTrailingSlashes
option@micmcg added option
matchTrailingSlash
published under0.5.5