From 2f1e9b976d7c5c1ada38c57f276304688d31b7e4 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Fri, 1 Apr 2022 10:34:42 +0200 Subject: [PATCH] fix(matcher): keep trailing slash on empty optional params Fix #1357 --- __tests__/matcher/resolve.spec.ts | 8 ++++++++ src/matcher/pathParserRanker.ts | 6 +++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/__tests__/matcher/resolve.spec.ts b/__tests__/matcher/resolve.spec.ts index 6c9533f9a..e26829be1 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 71a8b6cd7..483a29c47 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