Skip to content

Commit 2f1e9b9

Browse files
committed
fix(matcher): keep trailing slash on empty optional params
Fix #1357
1 parent c255f5d commit 2f1e9b9

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

__tests__/matcher/resolve.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,14 @@ describe('RouterMatcher.resolve', () => {
789789
}
790790
)
791791
})
792+
793+
it('resolves root path with optional params', () => {
794+
assertRecordMatch(
795+
{ path: '/:tab?', name: 'h', components },
796+
{ name: 'h' },
797+
{ name: 'h', path: '/', params: {} }
798+
)
799+
})
792800
})
793801

794802
describe('LocationAsRelative', () => {

src/matcher/pathParserRanker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,9 @@ export function tokensToParser(
250250
const text: string = Array.isArray(param) ? param.join('/') : param
251251
if (!text) {
252252
if (optional) {
253-
// if we have more than one optional param like /:a?-static we
254-
// don't need to care about the optional param
255-
if (segment.length < 2) {
253+
// if we have more than one optional param like /:a?-static and there are more segments, we don't need to
254+
// care about the optional param
255+
if (segment.length < 2 && segments.length > 1) {
256256
// remove the last slash as we could be at the end
257257
if (path.endsWith('/')) path = path.slice(0, -1)
258258
// do not append a slash on the next iteration

0 commit comments

Comments
 (0)