From 4d5182a3e8e74e1bb66b641c39391349be6d963b Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Tue, 19 Jul 2022 12:42:04 +0200 Subject: [PATCH] fix(matcher): correctly resolve empty paths with optional params Fix #1475 --- packages/router/__tests__/matcher/resolve.spec.ts | 5 +++++ packages/router/src/matcher/pathParserRanker.ts | 8 ++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/router/__tests__/matcher/resolve.spec.ts b/packages/router/__tests__/matcher/resolve.spec.ts index 672e79bb3..2f9c01e0b 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 0a98fd005..b5d672a70 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 {