]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix(matcher): correctly resolve empty paths with optional params
authorEduardo San Martin Morote <posva13@gmail.com>
Tue, 19 Jul 2022 10:42:04 +0000 (12:42 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Tue, 19 Jul 2022 10:42:04 +0000 (12:42 +0200)
Fix #1475

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

index 672e79bb3d6b65c81f138c820ea45bed0f70999f..2f9c01e0bda65b24b29427fdf384950e8bd6cc9f 100644 (file)
@@ -797,6 +797,11 @@ describe('RouterMatcher.resolve', () => {
         { name: 'h' },
         { name: 'h', path: '/', params: {} }
       )
+      assertRecordMatch(
+        { path: '/:tab?/:other?', name: 'h', components },
+        { name: 'h' },
+        { name: 'h', path: '/', params: {} }
+      )
     })
   })
 
index 0a98fd005fee9614149bd6581a3b1582c890f6fa..b5d672a70bb359238d26861004aed764b6ac9553 100644 (file)
@@ -255,9 +255,8 @@ export function tokensToParser(
             : (param as string)
           if (!text) {
             if (optional) {
-              // if we have more than one optional param like /:a?-static and there are more segments, we don't need to
-              // care about the optional param
-              if (segment.length < 2 && segments.length > 1) {
+              // if we have more than one optional param like /:a?-static we don't need to care about the optional param
+              if (segment.length < 2) {
                 // remove the last slash as we could be at the end
                 if (path.endsWith('/')) path = path.slice(0, -1)
                 // do not append a slash on the next iteration
@@ -270,7 +269,8 @@ export function tokensToParser(
       }
     }
 
-    return path
+    // avoid empty path when we have multiple optional params
+    return path || '/'
   }
 
   return {