fix(react-router): prevent incorrectly matching paths without slash separator#14689
Conversation
🦋 Changeset detectedLatest commit: e3c81b0 The changes in this PR will be included in the next version bump. This PR includes changesets to release 11 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Hi @nwleedev, Welcome, and thank you for contributing to React Router! Before we consider your pull request, we ask that you sign our Contributor License Agreement (CLA). We require this only once. You may review the CLA and sign it by adding your name to contributors.yml. Once the CLA is signed, the If you have already signed the CLA and received this response in error, or if you have any questions, please contact us at hello@remix.run. Thanks! - The Remix team |
|
Thank you for signing the Contributor License Agreement. Let's get this merged! 🥳 |
|
Would you mind adding a changeset via |
Apologies for missing the changeset request. I have created a patch changeset for react-router. |
| (_: string, paramName: string, isOptional) => { | ||
| params.push({ paramName, isOptional: isOptional != null }); | ||
| return isOptional ? "/?([^\\/]+)?" : "/([^\\/]+)"; | ||
| return isOptional ? "(?:/([^\\/]*))?" : "/([^\\/]+)"; |
There was a problem hiding this comment.
Making the question mark optional can cause issues when there is a suffix after the param - here's a failing case:
expect(matchPath("/sitemap/:lang?.xml", "/sitemap.xml")?.params).toBeNull();
Normally we do require the params to be the full segment, but the dot notation slipped through out original implementations and enough apps rely on it that it would be a breaking change at this point so we plan to continue supporting it.
Can you see if there's a different solution that plays nicely with that format?
|
Checked the suffix case you mentioned and adjusted the approach to preserve dot-notation compatibility.
|
|
Thanks! |
|
🤖 Hello there, We just published version Thanks! |
|
🤖 Hello there, We just published version Thanks! |
Problem
Optional dynamic segments (
:param?) incorrectly match paths that extend the base path without a separator. For example:The current regex pattern /?([^\/]+)? makes the slash and capture group independently optional.
It allows /test_route_more to match as /test_route + _more without a preceding slash.
Changes
The regex pattern to match paths
(e.g., /sitemap/:lang?.xml matching /sitemap/.xml)
Impact
Fixes #14640