Skip to content

Commit 756f755

Browse files
committed
feat(matcher): avoid empty records to be reached
Resolves posva/unplugin-vue-router#81
1 parent d218cca commit 756f755

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

packages/router/__tests__/matcher/resolve.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,44 @@ describe('RouterMatcher.resolve', () => {
10041004
)
10051005
).toMatchSnapshot()
10061006
})
1007+
1008+
it('avoids records with children without a component nor name', () => {
1009+
assertErrorMatch(
1010+
{
1011+
path: '/articles',
1012+
children: [{ path: ':id', components }],
1013+
},
1014+
{ path: '/articles' }
1015+
)
1016+
})
1017+
1018+
it('avoids nested records with children without a component nor name', () => {
1019+
assertErrorMatch(
1020+
{
1021+
path: '/app',
1022+
components,
1023+
children: [
1024+
{
1025+
path: '/articles',
1026+
children: [{ path: ':id', components }],
1027+
},
1028+
],
1029+
},
1030+
{ path: '/articles' }
1031+
)
1032+
})
1033+
1034+
it('can reach a named route with children and no component if named', () => {
1035+
assertRecordMatch(
1036+
{
1037+
path: '/articles',
1038+
name: 'ArticlesParent',
1039+
children: [{ path: ':id', components }],
1040+
},
1041+
{ name: 'ArticlesParent' },
1042+
{ name: 'ArticlesParent', path: '/articles' }
1043+
)
1044+
})
10071045
})
10081046

10091047
describe('children', () => {

packages/router/src/matcher/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,16 @@ export function createRouterMatcher(
178178
// parent.children.push(originalRecord)
179179
// }
180180

181-
insertMatcher(matcher)
181+
// Avoid adding a record that doesn't display anything. This allows passing through records without a component to
182+
// not be reached and pass through the catch all route
183+
if (
184+
(matcher.record.components &&
185+
Object.keys(matcher.record.components).length) ||
186+
matcher.record.name ||
187+
matcher.record.redirect
188+
) {
189+
insertMatcher(matcher)
190+
}
182191
}
183192

184193
return originalMatcher

0 commit comments

Comments
 (0)