Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(matcher): keep trailing slash on empty optional params
Fix #1357
  • Loading branch information
posva committed Apr 1, 2022
1 parent c255f5d commit 2f1e9b9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 8 additions & 0 deletions __tests__/matcher/resolve.spec.ts
Expand Up @@ -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', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/matcher/pathParserRanker.ts
Expand Up @@ -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
Expand Down

0 comments on commit 2f1e9b9

Please sign in to comment.