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
Show file tree
Hide file tree
Changes from all 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
34 changes: 34 additions & 0 deletions packages/router/__tests__/matcher/resolve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,40 @@ describe('RouterMatcher.resolve', () => {
)
})

it('keep optional params from parent record', () => {
const Child_A = { path: 'a', name: 'child_a', components }
const Child_B = { path: 'b', name: 'child_b', components }
const Parent = {
path: '/:optional?/parent',
name: 'parent',
components,
children: [Child_A, Child_B],
}
assertRecordMatch(
Parent,
{ name: 'child_b' },
{
name: 'child_b',
path: '/foo/parent/b',
params: { optional: 'foo' },
matched: [
Parent as any,
{
...Child_B,
path: `${Parent.path}/${Child_B.path}`,
},
],
},
{
params: { optional: 'foo' },
path: '/foo/parent/a',
matched: [],
meta: {},
name: undefined,
}
)
})

it('discards non existent params', () => {
assertRecordMatch(
{ path: '/', name: 'home', components },
Expand Down
9 changes: 7 additions & 2 deletions packages/router/src/matcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,13 @@ 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)
.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