Skip to content

Commit 5ba726c

Browse files
committed
feat(optimized matcher): Handle custom regexp options as dynamic routes.
1 parent 44f4bf5 commit 5ba726c

File tree

4 files changed

+59
-5
lines changed

4 files changed

+59
-5
lines changed

src/create-matcher.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,13 @@ function resolveRecordPath (path: string, record: RouteRecord): string {
225225
return resolvePath(path, record.parent ? record.parent.path : '/', true)
226226
}
227227

228-
function isStaticPath (path) {
229-
// Dynamic paths have /:placeholder or anonymous placeholder /(.+) or wildcard *.
230-
return !/[:(*]/.exec(path)
228+
function isStatic (route) {
229+
return (
230+
// Custom regex options are dynamic.
231+
Object.keys(route.pathToRegexpOptions).length === 0 &&
232+
// Dynamic paths have /:placeholder or anonymous placeholder /(.+) or wildcard *.
233+
!/[:(*]/.exec(route.path)
234+
)
231235
}
232236

233237
function optimizedMatcher (
@@ -239,7 +243,7 @@ function optimizedMatcher (
239243

240244
pathList.forEach((path, index) => {
241245
const route = pathMap[path]
242-
if (isStaticPath(path)) {
246+
if (isStatic(route)) {
243247
staticMap[path] = { index, route }
244248
if (route.regex.ignoreCase) {
245249
staticMap[path.toLowerCase()] = { index, route }

src/create-route-map.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ function addRouteRecord (
9292
const record: RouteRecord = {
9393
path: normalizedPath,
9494
regex: compileRouteRegex(normalizedPath, pathToRegexpOptions),
95+
pathToRegexpOptions,
9596
components: route.components || { default: route.component },
9697
alias: route.alias
9798
? typeof route.alias === 'string'

test/unit/specs/create-matcher.spec.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ describe('Creating Matcher', function () {
174174
expect(matcher.match('/p/staticafter').name).toBe('dynamic')
175175
})
176176

177-
it('static can be sensitive', function () {
177+
it('static can use sensitive flag', function () {
178178
const matcher = createMatcher([
179179
{
180180
path: '/p/sensitive',
@@ -197,4 +197,52 @@ describe('Creating Matcher', function () {
197197
expect(matcher.match('/p/SENSITIVE').name).toBe('not-found')
198198
expect(matcher.match('/p/INSENSITIVE').name).toBe('insensitive')
199199
})
200+
201+
it('static can use strict flag', function () {
202+
const matcher = createMatcher([
203+
{
204+
path: '/p/strict',
205+
name: 'strict',
206+
pathToRegexpOptions: {
207+
strict: true
208+
},
209+
component: { name: 'strict' }
210+
},
211+
{
212+
path: '/p/unstrict',
213+
name: 'unstrict',
214+
component: { name: 'unstrict' }
215+
},
216+
{
217+
path: '*', name: 'not-found', component: { name: 'not-found ' }
218+
}
219+
])
220+
221+
expect(matcher.match('/p/strict/').name).toBe('not-found')
222+
expect(matcher.match('/p/unstrict/').name).toBe('unstrict')
223+
})
224+
225+
it('static can use end flag', function () {
226+
const matcher = createMatcher([
227+
{
228+
path: '/p/end',
229+
name: 'end',
230+
component: { name: 'end' }
231+
},
232+
{
233+
path: '/p/not-end',
234+
name: 'not-end',
235+
pathToRegexpOptions: {
236+
end: false
237+
},
238+
component: { name: 'not-end' }
239+
},
240+
{
241+
path: '*', name: 'not-found', component: { name: 'not-found ' }
242+
}
243+
])
244+
245+
expect(matcher.match('/p/end/foo').name).toBe('not-found')
246+
expect(matcher.match('/p/not-end/foo').name).toBe('not-end')
247+
})
200248
})

types/router.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ export type RouteConfig = RouteConfigSingleView | RouteConfigMultipleViews
147147
export interface RouteRecord {
148148
path: string
149149
regex: RegExp
150+
pathToRegexpOptions: PathToRegexpOptions
150151
components: Dictionary<Component>
151152
instances: Dictionary<Vue>
152153
name?: string

0 commit comments

Comments
 (0)