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: keep optional params coming from a parent record #2031

Merged
merged 6 commits into from
Feb 13, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/router/src/matcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,15 @@ export function createRouterMatcher(
paramsFromLocation(
currentLocation.params,
// only keep params that exist in the resolved location
// TODO: only keep optional params coming from a parent record
matcher.keys.filter(k => !k.optional).map(k => k.name)
// only keep optional params coming from a parent record
matcher.keys
.filter(k => !k.optional)
.map(k => k.name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be done once at the end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. A direct parent's keys contains all optional params, includes ancestors
  2. I'll open a new PR to improve the code if the behavior it is necessary, absolutely i think this is better to keep the optional params from parent record.
{
    path: '/:type?/root',
    children: [
    {
        path: 'path_a',
        name: 'a',
    },
    {
        path: 'path_b',
        name: 'b',
    }
  ]
}

// current path:   '/optionType/root/path_a'

resolve({
    name: 'b'
});

I think /optionType/root/path_b is better than /root/path_b in this case

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess o forgot how I wrote my own code 😂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to keep optional params coming from a parent record, that is really useful

.concat(
matcher.parent
? matcher.parent.keys.filter(k => k.optional).map(k => k.name)
: []
)
),
// discard any existing params in the current location that do not exist here
// #1497 this ensures better active/exact matching
Expand Down
Loading