From 756f755731284f0f1899a2f8b62cb074b6f0f842 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Mon, 24 Oct 2022 09:58:12 +0200 Subject: [PATCH] feat(matcher): avoid empty records to be reached Resolves https://github.com/posva/unplugin-vue-router/issues/81 --- .../router/__tests__/matcher/resolve.spec.ts | 38 +++++++++++++++++++ packages/router/src/matcher/index.ts | 11 +++++- 2 files changed, 48 insertions(+), 1 deletion(-) 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 -- 2.39.5