]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat(matcher): avoid empty records to be reached
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 24 Oct 2022 07:58:12 +0000 (09:58 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 24 Oct 2022 07:58:12 +0000 (09:58 +0200)
Resolves https://github.com/posva/unplugin-vue-router/issues/81

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

index 2fc768ed3719c65bb70fb526e00b52d8203c749e..af5392a42c7852aeb1067950f15241be35f010ae 100644 (file)
@@ -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', () => {
index c2881d4416333f12a754df64bea2813f593e8999..9b1e1b3193572ddf0dd48b05e80258238347c2c1 100644 (file)
@@ -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