Skip to content

Commit 08ab48d

Browse files
authored
feat(experimental): add caseSensitive option (#2967)
* feat(experimental): add caseSensitive option * refactor: simplify * docs: note about encoding * chore: declare for smaller size
1 parent 9593a7a commit 08ab48d

8 files changed

Lines changed: 72 additions & 9 deletions

File tree

‎packages/docs/experimental/param-parsers.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ The regexp is inserted as a group inside the regexp of the whole path, so it mus
235235
- It must not contain capturing groups, use `(?:...)` instead.
236236
- It must not match an empty value (e.g. `/a*/`).
237237
- Do not use anchors like `^` or `$`: they apply to the whole path, not to the param value.
238-
- Its flags are ignored: paths are always matched case-insensitively.
238+
- Its flags are ignored: paths are matched case-insensitively unless you set the `caseSensitive` option of the plugin.
239239

240240
For repeatable params, each value must match the regexp, so the regexp should not match `/` (e.g. use `[^/]+` instead of `.+`). Set `re` to `null` to use the default regexp.
241241

‎packages/docs/file-based-routing/configuration.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ VueRouter({
5252
// disabled by default when the `CI` env variable is set
5353
watch: !process.env.CI,
5454

55+
/*
56+
* match paths case sensitively (experimental resolver only)
57+
* percent-encoded characters must use uppercase hex (`%C3%A9`)
58+
*/
59+
caseSensitive: false,
60+
5561
// modify routes individually
5662
async extendRoute(route) {
5763
// ...

‎packages/router/src/experimental/route-resolver/matchers/matcher-pattern.spec.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ describe('MatcherPatternPathStatic', () => {
3333
expect(pattern.match('/tEAm')).toEqual({})
3434
})
3535

36+
it('can be case sensitive', () => {
37+
const pattern = new MatcherPatternPathStatic('/Team', true)
38+
expect(pattern.match('/Team')).toEqual({})
39+
expect(pattern.match('/team')).toBe(null)
40+
})
41+
3642
it('keeps a trailing slash', () => {
3743
const pattern = new MatcherPatternPathStatic('/team/')
3844
expect(pattern.match('/team/')).toEqual({})
@@ -135,6 +141,17 @@ describe('MatcherPatternPathStar', () => {
135141
describe('MatcherPatternPathDynamic', () => {
136142
mockWarn()
137143

144+
it('is case sensitive without the i flag', () => {
145+
const pattern = new MatcherPatternPathDynamic(
146+
/^\/Teams\/([^/]+?)$/,
147+
{ teamId: [] },
148+
['Teams', 1]
149+
)
150+
151+
expect(pattern.match('/Teams/A')).toEqual({ teamId: 'A' })
152+
expect(pattern.match('/teams/A')).toBe(null)
153+
})
154+
138155
it('can have a trailing slash after a single param', () => {
139156
const pattern = new MatcherPatternPathDynamic(
140157
/^\/teams\/([^/]+?)\/$/i,

‎packages/router/src/experimental/route-resolver/matchers/matcher-pattern.ts‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,27 +65,40 @@ export interface MatcherPatternPath<
6565
* matcher.match('/team/123') // null
6666
* matcher.build() // '/team'
6767
* ```
68+
*
69+
* @param path - path to match
70+
* @param caseSensitive - match the path case sensitively
6871
*/
6972
export class MatcherPatternPathStatic implements MatcherPatternPath<EmptyParams> {
7073
/**
71-
* lowercase version of the path to match against.
72-
* This is used to make the matching case insensitive.
74+
* Normalizes a path before comparing it.
75+
*/
76+
declare private n: (path: string) => string
77+
78+
/**
79+
* Normalized version of the path to match against.
7380
*/
74-
private pathi: string
81+
declare private _path: string
7582

76-
constructor(readonly path: string) {
77-
this.pathi = path.toLowerCase()
83+
constructor(
84+
readonly path: string,
85+
caseSensitive?: boolean
86+
) {
87+
this.n = caseSensitive ? identityFn : toLowerCase
88+
this._path = this.n(path)
7889
}
7990

8091
match(path: string): EmptyParams | null {
81-
return path.toLowerCase() === this.pathi ? {} : null
92+
return this.n(path) === this._path ? {} : null
8293
}
8394

8495
build(): string {
8596
return this.path
8697
}
8798
}
8899

100+
const toLowerCase = (path: string) => path.toLowerCase()
101+
89102
/**
90103
* Options for param parsers in {@link MatcherPatternPathDynamic}.
91104
*/

‎packages/router/src/unplugin/codegen/generateRouteResolver.spec.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,22 @@ describe('generateRouteResolver', () => {
496496
`)
497497
})
498498

499+
it('generates case sensitive matchers with caseSensitive', () => {
500+
const options = resolveOptions({ caseSensitive: true })
501+
const tree = new PrefixTree(options)
502+
tree.insert('a', 'a.vue')
503+
tree.insert('[id]', '[id].vue')
504+
const resolver = generateRouteResolver(
505+
tree,
506+
options,
507+
new ImportsMap(),
508+
new Map()
509+
)
510+
511+
expect(resolver).toContain(`new MatcherPatternPathStatic('/a', true)`)
512+
expect(resolver).toContain(`/^\\/([^/]+?)$/,`)
513+
})
514+
499515
it('reuses parent path matcher when possible', () => {
500516
const tree = new PrefixTree(DEFAULT_OPTIONS)
501517
const importsMap = new ImportsMap()

‎packages/router/src/unplugin/codegen/generateRouteResolver.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ function generatePathCode(
351351
${node.endsWithSplat ? 'null,' : '/* trailingSlash */'}
352352
),`
353353
} else {
354-
return `path: new MatcherPatternPathStatic(${toStringLiteral(node.fullPath)}),`
354+
return `path: new MatcherPatternPathStatic(${toStringLiteral(node.fullPath)}${node.options.caseSensitive ? ', true' : ''}),`
355355
}
356356
}
357357

‎packages/router/src/unplugin/core/tree.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ export class TreeNode {
497497
re += '\\/?'
498498
}
499499

500-
return '/^' + re + '$/i'
500+
return '/^' + re + (this.options.caseSensitive ? '$/' : '$/i')
501501
}
502502

503503
/**

‎packages/router/src/unplugin/options.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,16 @@ export interface Options {
230230
*/
231231
pathParser?: ParseSegmentOptions
232232

233+
/**
234+
* Match paths case sensitively in the experimental resolver. Without the
235+
* experimental resolver, use the `sensitive` option of `createRouter()`.
236+
* Percent-encoded characters must use uppercase hex digits (`%C3%A9`, not
237+
* `%c3%a9`), as browsers do when they encode URLs.
238+
*
239+
* @default `false`
240+
*/
241+
caseSensitive?: boolean
242+
233243
/**
234244
* Whether to watch the files for changes.
235245
*
@@ -316,6 +326,7 @@ export const DEFAULT_OPTIONS = {
316326
dotNesting: true,
317327
},
318328
watch: !process.env.CI,
329+
caseSensitive: false,
319330
experimental: {},
320331
} satisfies Options
321332

0 commit comments

Comments
 (0)