Skip to content

Commit 15e20cb

Browse files
fix: remove nullish params when resolving (#1814)
NOTES: This change improves allows passing `null` or `undefined` to a param to completely drop. In practice, this should be better than casting it to an empty string but it should make no change if you check the absence of empty params with `!route.params.optional` rather than `route.params.optional !== ''` or any other stricter check. If you were doing stricter checks for optional params, make sure to change them to looser checks. Note that when resolving from the URL, optional parameters are always absent in the resolved object.
1 parent dcafc02 commit 15e20cb

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

packages/router/__tests__/router.spec.ts

+15-10
Original file line numberDiff line numberDiff line change
@@ -297,20 +297,25 @@ describe('Router', () => {
297297
expect(router.currentRoute.value).toMatchObject({ params: { p: '0' } })
298298
})
299299

300-
it('casts null/undefined params to empty strings', async () => {
300+
it('removes null/undefined params', async () => {
301301
const { router } = await newRouter()
302-
expect(
303-
router.resolve({ name: 'optional', params: { p: undefined } })
304-
).toMatchObject({
305-
params: {},
302+
303+
const route1 = router.resolve({
304+
name: 'optional',
305+
params: { p: undefined },
306306
})
307-
expect(
308-
router.resolve({ name: 'optional', params: { p: null } })
309-
).toMatchObject({
310-
params: {},
307+
expect(route1.path).toBe('/optional')
308+
expect(route1.params).toEqual({})
309+
310+
const route2 = router.resolve({
311+
name: 'optional',
312+
params: { p: null },
311313
})
314+
expect(route2.path).toBe('/optional')
315+
expect(route2.params).toEqual({})
316+
312317
await router.push({ name: 'optional', params: { p: null } })
313-
expect(router.currentRoute.value).toMatchObject({ params: {} })
318+
expect(router.currentRoute.value.params).toEqual({})
314319
await router.push({ name: 'optional', params: {} })
315320
})
316321

packages/router/src/router.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ export function createRouter(options: RouterOptions): Router {
491491
}
492492
// pass encoded values to the matcher, so it can produce encoded path and fullPath
493493
matcherLocation = assign({}, rawLocation, {
494-
params: encodeParams(rawLocation.params),
494+
params: encodeParams(targetParams),
495495
})
496496
// current location params are decoded, we need to encode them in case the
497497
// matcher merges the params

0 commit comments

Comments
 (0)