Skip to content

Commit 4fce5c0

Browse files
committed
fix: combining 'end' and 'strict'
1 parent 13303bd commit 4fce5c0

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

packages/router/__tests__/matcher/pathParser.spec.ts

+53-2
Original file line numberDiff line numberDiff line change
@@ -619,8 +619,59 @@ describe('Path parser', () => {
619619
})
620620

621621
it('can not match the end', () => {
622-
matchParams('/home', '/home/other', null, { end: true })
623-
matchParams('/home', '/home/other', {}, { end: false })
622+
const options = { end: false }
623+
624+
matchParams('/home', '/home', {}, options)
625+
matchParams('/home', '/home/', {}, options)
626+
matchParams('/home', '/home/other', {}, options)
627+
matchParams('/home', '/homepage', {}, options)
628+
629+
matchParams('/home/:p', '/home', null, options)
630+
matchParams('/home/:p', '/home/', null, options)
631+
matchParams('/home/:p', '/home/a', { p: 'a' }, options)
632+
matchParams('/home/:p', '/home/a/', { p: 'a' }, options)
633+
matchParams('/home/:p', '/home/a/b', { p: 'a' }, options)
634+
matchParams('/home/:p', '/homepage', null, options)
635+
636+
matchParams('/home/', '/home', {}, options)
637+
matchParams('/home/', '/home/', {}, options)
638+
matchParams('/home/', '/home/other', {}, options)
639+
matchParams('/home/', '/homepage', {}, options)
640+
641+
matchParams('/home/:p/', '/home', null, options)
642+
matchParams('/home/:p/', '/home/', null, options)
643+
matchParams('/home/:p/', '/home/a', { p: 'a' }, options)
644+
matchParams('/home/:p/', '/home/a/', { p: 'a' }, options)
645+
matchParams('/home/:p/', '/home/a/b', { p: 'a' }, options)
646+
matchParams('/home/:p/', '/homepage', null, options)
647+
})
648+
649+
it('can not match the end when strict', () => {
650+
const options = { end: false, strict: true }
651+
652+
matchParams('/home', '/home', {}, options)
653+
matchParams('/home', '/home/', {}, options)
654+
matchParams('/home', '/home/other', {}, options)
655+
matchParams('/home', '/homepage', null, options)
656+
657+
matchParams('/home/:p', '/home', null, options)
658+
matchParams('/home/:p', '/home/', null, options)
659+
matchParams('/home/:p', '/home/a', { p: 'a' }, options)
660+
matchParams('/home/:p', '/home/a/', { p: 'a' }, options)
661+
matchParams('/home/:p', '/home/a/b', { p: 'a' }, options)
662+
matchParams('/home/:p', '/homepage', null, options)
663+
664+
matchParams('/home/', '/home', null, options)
665+
matchParams('/home/', '/home/', {}, options)
666+
matchParams('/home/', '/home/other', {}, options)
667+
matchParams('/home/', '/homepage', null, options)
668+
669+
matchParams('/home/:p/', '/home', null, options)
670+
matchParams('/home/:p/', '/home/', null, options)
671+
matchParams('/home/:p/', '/home/a', null, options)
672+
matchParams('/home/:p/', '/home/a/', { p: 'a' }, options)
673+
matchParams('/home/:p/', '/home/a/b', { p: 'a' }, options)
674+
matchParams('/home/:p/', '/homepage', null, options)
624675
})
625676

626677
it('should not match optional params + static without leading slash', () => {

packages/router/src/matcher/pathParserRanker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ export function tokensToParser(
217217

218218
if (options.end) pattern += '$'
219219
// allow paths like /dynamic to only match dynamic or dynamic/... but not dynamic_something_else
220-
else if (options.strict) pattern += '(?:/|$)'
220+
else if (options.strict && !pattern.endsWith('/')) pattern += '(?:/|$)'
221221

222222
const re = new RegExp(pattern, options.sensitive ? '' : 'i')
223223

0 commit comments

Comments
 (0)