]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix(matcher): keep trailing slash on empty optional params
authorEduardo San Martin Morote <posva13@gmail.com>
Fri, 1 Apr 2022 08:34:42 +0000 (10:34 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Fri, 1 Apr 2022 08:34:42 +0000 (10:34 +0200)
Fix #1357

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

index 6c9533f9a8581a6463e0b8e6f623732f95ac547e..e26829be1fea9b562c5484410701bc0c9c526c6c 100644 (file)
@@ -789,6 +789,14 @@ describe('RouterMatcher.resolve', () => {
         }
       )
     })
+
+    it('resolves root path with optional params', () => {
+      assertRecordMatch(
+        { path: '/:tab?', name: 'h', components },
+        { name: 'h' },
+        { name: 'h', path: '/', params: {} }
+      )
+    })
   })
 
   describe('LocationAsRelative', () => {
index 71a8b6cd72b5a951c5e9b43ca66067141a8f584a..483a29c473ef21ccb766236e47f7a7111602f51e 100644 (file)
@@ -250,9 +250,9 @@ export function tokensToParser(
           const text: string = Array.isArray(param) ? param.join('/') : param
           if (!text) {
             if (optional) {
-              // 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) {
+              // 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) {
                 // 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