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(router): Skip search params with undefined and null values passed to named routes #11635

Merged
merged 7 commits into from
Dec 28, 2024
Merged
6 changes: 6 additions & 0 deletions packages/router/src/__tests__/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,4 +451,10 @@ describe('replaceParams', () => {

expect(replaceParams('/calc', { expr: '1+2' })).toEqual('/calc?expr=1%2B2')
})

it('skips search parameters with `undefined` and `null` values', () => {
expect(replaceParams('/s', { a: '', b: 0, c: undefined, d: null })).toEqual(
'/s?a=&b=0',
)
})
})
4 changes: 3 additions & 1 deletion packages/router/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,9 @@ export function replaceParams(
})

const paramNames = params.map((param) => param[0])
const extraArgKeys = Object.keys(args).filter((x) => !paramNames.includes(x))
const extraArgKeys = Object.keys(args)
.filter((x) => !paramNames.includes(x))
.filter((x) => args[x] !== undefined && args[x] !== null)

// Append any unnamed params as search params.
if (extraArgKeys.length) {
Expand Down
Loading