Skip to content
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

Closed

Conversation

brophdawg11
Copy link
Contributor

@brophdawg11 brophdawg11 commented Sep 12, 2022

This fixes a small inconsistency in generatePath w.r.t. compilePath. We do support partial named params but at the moment we do not support partial splat params, but generatePath was permitting them. This aligns generatePath behavior and provides the same warning as compilePath

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

@changeset-bot
Copy link

changeset-bot bot commented Sep 12, 2022

🦋 Changeset detected

Latest commit: 7017172

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 5 packages
Name Type
react-router Patch
@remix-run/router Patch
react-router-dom Patch
react-router-dom-v5-compat Patch
react-router-native Patch

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" },
Copy link
Contributor Author

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)) {
Copy link
Contributor Author

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

@brophdawg11 brophdawg11 force-pushed the brophdawg11/consistent-partial-splat-handling branch from e8f7403 to 08b2442 Compare September 12, 2022 21:51
{ path: "prefix-:param" }, // Score 12
{ path: "prefix-*" }, // Score 11
{ path: ":param" }, // Score 6
{ path: "*" }, // Score 1
Copy link
Contributor Author

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

@brophdawg11
Copy link
Contributor Author

Closing in favor of #9506

@MichaelDeBoey MichaelDeBoey deleted the brophdawg11/consistent-partial-splat-handling branch November 17, 2022 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant