Make trailing slash optional
Desired behavior:
- Always allow an optional trailing slash
- Never add an optional trailing slash when generating links via
makePath
I think this behavior is pretty standard.
Today (as of v0.12.9) makePath appends a trailing slash if a trailing slash is optionally allowed.
Food for thought (tangential): It would be nice to support optional trailing slashes automatically without the need to add a /? to all routes.
For me, the desired behavior would be
- Force trailing slash for routes (via redirect), but only for routes with children/DefaultRoutes ("directories")
- Always add trailing slash to links to those routes
So I don't think changing the default behavior is the ideal solution. But it would be nice to have some more control over this.
+1
+1
+1
This is really an important issue. However, since the issue hasn’t been resolved, what is the workaround?
+1
+1
Yep, very annoying issue. Path must be handled with and without trailing slash by default as it is not classic REST router.
@ryanflorence @mjackson will this be addressed/changed in #1158? Does it make sense to implement it on the current code base, or should I rather start based on the code in #1158 ?
@Dignifiedquire This is already fixed in #1158
@mjackson how to solve this issue now?
its now optional in 1.0
workaround for this in 0.13.2 ?
@kevzettler add /? to the url
workaround for this in 0.13.2 ?
+1
/? is not work in 1.0.0-beta3?
It is not working in Link component. Class "active" is not added in case of trailing slash.
In the method Router#isActive are compared wrong strings:
to === state.path
which in my case of using /? in route's path looks like this:
"/about" === "/about/"
Hi guys! Can't you just redirect every path with trailing slash to the path without it on server (using express for example)?
It completely solved this issue for us.
@th0r yes I ended up with unification of all routes
(On 1.0.0-rc3) This is strange, but when using IndexRedirect, having a trailing slash on the root path caused a problem for me.
If I do this (a slash after admin):
<Route path='/admin/' component={App}>
<IndexRedirect to='posts' />
Then if I open a new window and go to localhost:.../admin/ it gets redirected to /admin/posts correctly, BUT if I open a new window and go to localhost:.../admin (without a slash at the end) it gives me an error:
Warning: Location "/admin" did not match any routes
To fix this, I had to remove the trailing slash from the root component:
<Route path='/admin' component={App}>
<IndexRedirect to='posts' />
I wanted to write a failing test but couldn't figure out how: https://github.com/rackt/react-router/blob/c025177a10785f96f7683d4f033f6acdd46c1f2e/modules/tests/IndexRedirect-test.js#L20-L31
If someone could help me write a failing test, I'd create a new issue.