From: Eduardo San Martin Morote Date: Fri, 1 Apr 2022 08:34:42 +0000 (+0200) Subject: fix(matcher): keep trailing slash on empty optional params X-Git-Tag: v4.0.15~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2f1e9b976d7c5c1ada38c57f276304688d31b7e4;p=thirdparty%2Fvuejs%2Frouter.git fix(matcher): keep trailing slash on empty optional params Fix #1357 --- diff --git a/__tests__/matcher/resolve.spec.ts b/__tests__/matcher/resolve.spec.ts index 6c9533f9..e26829be 100644 --- a/__tests__/matcher/resolve.spec.ts +++ b/__tests__/matcher/resolve.spec.ts @@ -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', () => { diff --git a/src/matcher/pathParserRanker.ts b/src/matcher/pathParserRanker.ts index 71a8b6cd..483a29c4 100644 --- a/src/matcher/pathParserRanker.ts +++ b/src/matcher/pathParserRanker.ts @@ -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