Skip to content

Commit

Permalink
fix(matcher): correctly resolve empty paths with optional params
Browse files Browse the repository at this point in the history
Fix #1475
  • Loading branch information
posva committed Jul 19, 2022
1 parent 9030279 commit 4d5182a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
5 changes: 5 additions & 0 deletions packages/router/__tests__/matcher/resolve.spec.ts
Expand Up @@ -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: {} }
)
})
})

Expand Down
8 changes: 4 additions & 4 deletions packages/router/src/matcher/pathParserRanker.ts
Expand Up @@ -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
Expand All @@ -270,7 +269,8 @@ export function tokensToParser(
}
}

return path
// avoid empty path when we have multiple optional params
return path || '/'
}

return {
Expand Down

0 comments on commit 4d5182a

Please sign in to comment.