From abe1453013b50b5f5e22f9707237fcea50c935ba Mon Sep 17 00:00:00 2001 From: babu-ch Date: Tue, 7 Jan 2025 10:45:48 +0900 Subject: [PATCH 1/2] fix(resolve): exclude empty optional arguments from params --- packages/playground/src/App.vue | 9 +++++++++ packages/playground/src/router.ts | 5 +++++ packages/router/__tests__/matcher/resolve.spec.ts | 4 ++-- packages/router/src/matcher/index.ts | 6 ++++++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/playground/src/App.vue b/packages/playground/src/App.vue index 170d73919..111197c5b 100644 --- a/packages/playground/src/App.vue +++ b/packages/playground/src/App.vue @@ -158,6 +158,15 @@
  • /p_1/absolute-a
  • +
  • + Go to Features (name) +
  • +
  • + Go to Features (string) +
  • +
  • + Go to Feature one +
  • diff --git a/packages/playground/src/router.ts b/packages/playground/src/router.ts index 981b4192f..ba16c9646 100644 --- a/packages/playground/src/router.ts +++ b/packages/playground/src/router.ts @@ -159,6 +159,11 @@ export const router = createRouter({ { path: 'settings', component }, ], }, + { + path: '/features/:pathMatch(.*)*', + name: 'features', + component: User, + }, ], async scrollBehavior(to, from, savedPosition) { await scrollWaiter.wait() diff --git a/packages/router/__tests__/matcher/resolve.spec.ts b/packages/router/__tests__/matcher/resolve.spec.ts index 2b9c4e7ae..7c412083c 100644 --- a/packages/router/__tests__/matcher/resolve.spec.ts +++ b/packages/router/__tests__/matcher/resolve.spec.ts @@ -685,12 +685,12 @@ describe('RouterMatcher.resolve', () => { assertRecordMatch( { path: '/:a?', components, name: 'a' }, { path: '/' }, - { path: '/', params: { a: '' }, name: 'a' } + { path: '/', params: {}, name: 'a' } ) assertRecordMatch( { path: '/a/:a?', components, name: 'a' }, { path: '/a/' }, - { path: '/a/', params: { a: '' }, name: 'a' } + { path: '/a/', params: {}, name: 'a' } ) }) diff --git a/packages/router/src/matcher/index.ts b/packages/router/src/matcher/index.ts index 9d787ddbc..5cfbd5945 100644 --- a/packages/router/src/matcher/index.ts +++ b/packages/router/src/matcher/index.ts @@ -310,6 +310,12 @@ export function createRouterMatcher( // we know the matcher works because we tested the regexp params = matcher.parse(path)! name = matcher.record.name + + matcher?.keys.forEach(key => { + if (key.optional && params[key.name] === '') { + delete params[key.name] + } + }) } // location is a relative path } else { From 66f824f7f9a2b8f71828267b32ec6a14ce60d7e4 Mon Sep 17 00:00:00 2001 From: babu-ch Date: Wed, 8 Jan 2025 17:27:40 +0900 Subject: [PATCH 2/2] fix: ?. to . --- packages/router/src/matcher/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/router/src/matcher/index.ts b/packages/router/src/matcher/index.ts index 5cfbd5945..9ccbc059c 100644 --- a/packages/router/src/matcher/index.ts +++ b/packages/router/src/matcher/index.ts @@ -311,7 +311,7 @@ export function createRouterMatcher( params = matcher.parse(path)! name = matcher.record.name - matcher?.keys.forEach(key => { + matcher.keys.forEach(key => { if (key.optional && params[key.name] === '') { delete params[key.name] }