-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
[Bug]: generatePath strips trailing slashes #10291
Comments
Hello, is anyone working on this issue? |
As far as I can see, generatePath removed the slash from the end because it interpreted it as an empty segment. https://github.com/remix-run/react-router/blob/main/packages/router/utils.ts#L749 |
This is correct. React Router treats the slash as a separator. So, in generating a path, we treat that final segment as an empty string and remove it for having no value. We always generate URLs without the trailing slash. |
Is there a recommended way to get back the original behaviour? This breaking change from the original behaviour of the code (which whilst did not document that it would preserve trailing slashes, did not also say that it would remove empty segments, because it didn’t) has caught us by surprise. |
I would be happy to provide a patch to restore the original behaviour; otherwise I’d suggest that further detail in the documentation and tests verifying the stripping of empty segments is added to help anyone else upgrading who was expecting preservation of trailing slashes |
Yes, I agree that this is a breaking change, for all those who use React Router and who make SSG it is very problematic. export const PATHS = {
...
SEARCH: '/:lang/search/',
};
...
navigate({
pathname: generatePath(PATHS.SEARCH, { lang: i18n.language }),
search: `search=${value}`,
}); The generated url is Can add a |
@timdorr Why close this topic ? |
@timdorr Can this be reconsidered, or at least made backwards compatible? Due to the complex set-up of our project (a web app that is in transition phase and runs partly on an old AEM frontend with an Apache server which forces trailing slashes and partly on a SPA with react-router), we have a hard requirement at this moment to always end URL's with a trailing slash. Never had problem with this and react-router up until now, when we wanted to upgrade from |
Hi @Eliasvdb did you mentioned that you were working on an old AEM frontend? Were you able to migrate to v6? I'm not able to do that and I'm still researching on how to do that, as you (probably) already know, maybe you notice that does not contain parent. There is a file called RouterHelper, but when I use my app is rendering a blank page without errors. I will really appreciate your hints, thanks in advance. |
thank you |
This is the hack that I came up with: // HACK: always add trailing slash to path (works with query strings & anchors)
let path = generatePath(route.path, params);
path = path.replace(/\/?(\?|#|$)/, "/$1"); |
Another dev here that has a problem with this not being backwards compatible (and not being mentioned on the release notes) |
What version of React Router are you using?
6.10.0
Steps to Reproduce
generatePath('/foo/')
Expected Behavior
/foo/
Actual Behavior
/foo
I'm guessing this is caused by #10078
The text was updated successfully, but these errors were encountered: