Skip to content

Commit

Permalink
fix(resolve): exclude empty optional arguments from params
Browse files Browse the repository at this point in the history
  • Loading branch information
babu-ch committed Jan 7, 2025
1 parent bd37fd4 commit abe1453
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
9 changes: 9 additions & 0 deletions packages/playground/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@
<li>
<router-link to="/p_1/absolute-a">/p_1/absolute-a</router-link>
</li>
<li>
<router-link :to="{ name: 'features' }">Go to Features (name)</router-link>
</li>
<li>
<router-link to="/features">Go to Features (string)</router-link>
</li>
<li>
<router-link to="/features/one">Go to Feature one</router-link>
</li>
</ul>
<button @click="toggleViewName">Toggle view</button>
<RouterView :name="viewName" v-slot="{ Component, route }">
Expand Down
5 changes: 5 additions & 0 deletions packages/playground/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions packages/router/__tests__/matcher/resolve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
)
})

Expand Down
6 changes: 6 additions & 0 deletions packages/router/src/matcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit abe1453

Please sign in to comment.