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 {