-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
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
fix: ensure consistency in generatePath/compilePath for partial splats #9238
Conversation
🦋 Changeset detectedLatest commit: 7017172 The changes in this PR will be included in the next version bump. This PR includes changesets to release 5 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 |
describe("when the pattern has a trailing *", () => { | ||
it("matches the remaining portion of the pathname", () => { | ||
expect(matchPath("/files*", "/files/mj.jpg")).toMatchObject({ | ||
params: { "*": "/mj.jpg" }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously trimmed leading slashes are now included since we support partial splat matching
|
||
function computeScore(path: string, index: boolean | undefined): number { | ||
let segments = path.split("/"); | ||
let initialScore = segments.length; | ||
if (segments.some(isSplat)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
splat scoring moved inside the below loop to avoid multiple traversals of segments
e8f7403
to
08b2442
Compare
{ path: "prefix-:param" }, // Score 12 | ||
{ path: "prefix-*" }, // Score 11 | ||
{ path: ":param" }, // Score 6 | ||
{ path: "*" }, // Score 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is my attempt to pressure test all the potential combinations of partial/full splat/dynamic params and assert that we get the expected matches
/params
Closing in favor of #9506 |
This fixes a small inconsistency ingeneratePath
w.r.t.compilePath
. We do support partial named params but at the moment we do not support partial splat params, butgeneratePath
was permitting them. This alignsgeneratePath
behavior and provides the same warning ascompilePath
After chatting with @ryanflorence we decided that if possible we should look into removing that warning and supporting partial splat params (in path matching and
generatePath
), such the the path/prefix-*
is matched by the URL/prefix-thing
. This PR adds specific scoring for both partial dynamic (/prefix-:param
) and partial static (/prefix-*
) path segments.There are I think 2 exiting unit tests that break, that rely on the behavior indicated by the previous warning. So want to confirm those changes with @mjackson and @ryanflorence, but otherwise everything else passes and I added a decently sized test to try to really kick the tire son the scoring algorithm and potential conflicts.
Note: Recommend viewing the PR with whitespace hidden