Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: combining 'end' and 'strict' #2154

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 70 additions & 2 deletions packages/router/__tests__/matcher/pathParser.spec.ts
Expand Up @@ -618,9 +618,77 @@ describe('Path parser', () => {
matchParams('/home', '/other/home', {}, { start: false })
})

it('defaults to matching the end', () => {
// The default should behave like `end: true`
const optionSets = [{}, { end: true }]

for (const options of optionSets) {
matchParams('/home', '/home', {}, options)
matchParams('/home', '/home/', {}, options)
matchParams('/home', '/home/other', null, options)
matchParams('/home', '/homepage', null, options)

matchParams('/home/', '/home', {}, options)
matchParams('/home/', '/home/', {}, options)
matchParams('/home/', '/home/other', null, options)
matchParams('/home/', '/homepage', null, options)
}
})

it('can not match the end', () => {
matchParams('/home', '/home/other', null, { end: true })
posva marked this conversation as resolved.
Show resolved Hide resolved
matchParams('/home', '/home/other', {}, { end: false })
const options = { end: false }

matchParams('/home', '/home', {}, options)
matchParams('/home', '/home/', {}, options)
matchParams('/home', '/home/other', {}, options)
matchParams('/home', '/homepage', {}, options)

matchParams('/home/:p', '/home', null, options)
matchParams('/home/:p', '/home/', null, options)
matchParams('/home/:p', '/home/a', { p: 'a' }, options)
matchParams('/home/:p', '/home/a/', { p: 'a' }, options)
matchParams('/home/:p', '/home/a/b', { p: 'a' }, options)
matchParams('/home/:p', '/homepage', null, options)

matchParams('/home/', '/home', {}, options)
matchParams('/home/', '/home/', {}, options)
matchParams('/home/', '/home/other', {}, options)
matchParams('/home/', '/homepage', {}, options)

matchParams('/home/:p/', '/home', null, options)
matchParams('/home/:p/', '/home/', null, options)
matchParams('/home/:p/', '/home/a', { p: 'a' }, options)
matchParams('/home/:p/', '/home/a/', { p: 'a' }, options)
matchParams('/home/:p/', '/home/a/b', { p: 'a' }, options)
matchParams('/home/:p/', '/homepage', null, options)
})

it('can not match the end when strict', () => {
const options = { end: false, strict: true }

matchParams('/home', '/home', {}, options)
matchParams('/home', '/home/', {}, options)
matchParams('/home', '/home/other', {}, options)
matchParams('/home', '/homepage', null, options)

matchParams('/home/:p', '/home', null, options)
matchParams('/home/:p', '/home/', null, options)
matchParams('/home/:p', '/home/a', { p: 'a' }, options)
matchParams('/home/:p', '/home/a/', { p: 'a' }, options)
matchParams('/home/:p', '/home/a/b', { p: 'a' }, options)
matchParams('/home/:p', '/homepage', null, options)

matchParams('/home/', '/home', null, options)
matchParams('/home/', '/home/', {}, options)
matchParams('/home/', '/home/other', {}, options)
matchParams('/home/', '/homepage', null, options)

matchParams('/home/:p/', '/home', null, options)
matchParams('/home/:p/', '/home/', null, options)
matchParams('/home/:p/', '/home/a', null, options)
matchParams('/home/:p/', '/home/a/', { p: 'a' }, options)
matchParams('/home/:p/', '/home/a/b', { p: 'a' }, options)
matchParams('/home/:p/', '/homepage', null, options)
})

it('should not match optional params + static without leading slash', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/router/src/matcher/pathParserRanker.ts
Expand Up @@ -217,7 +217,7 @@ export function tokensToParser(

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

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

Expand Down