From: Eduardo San Martin Morote Date: Mon, 24 Oct 2022 07:58:12 +0000 (+0200) Subject: feat(matcher): avoid empty records to be reached X-Git-Tag: v4.1.6~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=756f755;p=thirdparty%2Fvuejs%2Frouter.git feat(matcher): avoid empty records to be reached Resolves https://github.com/posva/unplugin-vue-router/issues/81 --- diff --git a/packages/router/__tests__/matcher/resolve.spec.ts b/packages/router/__tests__/matcher/resolve.spec.ts index 2fc768ed..af5392a4 100644 --- a/packages/router/__tests__/matcher/resolve.spec.ts +++ b/packages/router/__tests__/matcher/resolve.spec.ts @@ -1004,6 +1004,44 @@ describe('RouterMatcher.resolve', () => { ) ).toMatchSnapshot() }) + + it('avoids records with children without a component nor name', () => { + assertErrorMatch( + { + path: '/articles', + children: [{ path: ':id', components }], + }, + { path: '/articles' } + ) + }) + + it('avoids nested records with children without a component nor name', () => { + assertErrorMatch( + { + path: '/app', + components, + children: [ + { + path: '/articles', + children: [{ path: ':id', components }], + }, + ], + }, + { path: '/articles' } + ) + }) + + it('can reach a named route with children and no component if named', () => { + assertRecordMatch( + { + path: '/articles', + name: 'ArticlesParent', + children: [{ path: ':id', components }], + }, + { name: 'ArticlesParent' }, + { name: 'ArticlesParent', path: '/articles' } + ) + }) }) describe('children', () => { diff --git a/packages/router/src/matcher/index.ts b/packages/router/src/matcher/index.ts index c2881d44..9b1e1b31 100644 --- a/packages/router/src/matcher/index.ts +++ b/packages/router/src/matcher/index.ts @@ -178,7 +178,16 @@ export function createRouterMatcher( // parent.children.push(originalRecord) // } - insertMatcher(matcher) + // Avoid adding a record that doesn't display anything. This allows passing through records without a component to + // not be reached and pass through the catch all route + if ( + (matcher.record.components && + Object.keys(matcher.record.components).length) || + matcher.record.name || + matcher.record.redirect + ) { + insertMatcher(matcher) + } } return originalMatcher