From: Eduardo San Martin Morote Date: Tue, 19 Jul 2022 10:42:04 +0000 (+0200) Subject: fix(matcher): correctly resolve empty paths with optional params X-Git-Tag: v4.1.3~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4d5182a3e8e74e1bb66b641c39391349be6d963b;p=thirdparty%2Fvuejs%2Frouter.git fix(matcher): correctly resolve empty paths with optional params Fix #1475 --- diff --git a/packages/router/__tests__/matcher/resolve.spec.ts b/packages/router/__tests__/matcher/resolve.spec.ts index 672e79bb..2f9c01e0 100644 --- a/packages/router/__tests__/matcher/resolve.spec.ts +++ b/packages/router/__tests__/matcher/resolve.spec.ts @@ -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: {} } + ) }) }) diff --git a/packages/router/src/matcher/pathParserRanker.ts b/packages/router/src/matcher/pathParserRanker.ts index 0a98fd00..b5d672a7 100644 --- a/packages/router/src/matcher/pathParserRanker.ts +++ b/packages/router/src/matcher/pathParserRanker.ts @@ -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 {