Skip to content

Commit

Permalink
fix(router): Skip search params with undefined and null values passed…
Browse files Browse the repository at this point in the history
… to named routes (#11635)

Co-authored-by: Tobbe Lundberg <[email protected]>
  • Loading branch information
antonmoiseev and Tobbe authored Dec 28, 2024
1 parent f2b1e75 commit 5b8d035
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
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

0 comments on commit 5b8d035

Please sign in to comment.