Skip to content

Commit 14e1eb9

Browse files
miyaokaposva
authored andcommitted
feat(warn): help migrating catch all routes
1 parent bae61ce commit 14e1eb9

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

__tests__/matcher/addingRemoving.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,24 @@ describe('Matcher: adding and removing records', () => {
1515
})
1616
})
1717

18+
it('throws when adding *', () => {
19+
const matcher = createRouterMatcher([], {})
20+
expect(() => {
21+
matcher.addRoute({ path: '*', component })
22+
}).toThrowError('Catch all')
23+
})
24+
25+
it('does not throw when adding * in children', () => {
26+
const matcher = createRouterMatcher([], {})
27+
expect(() => {
28+
matcher.addRoute({
29+
path: '/something',
30+
component,
31+
children: [{ path: '*', component }],
32+
})
33+
}).not.toThrow()
34+
})
35+
1836
it('adds children', () => {
1937
const matcher = createRouterMatcher([], {})
2038
matcher.addRoute({ path: '/parent', component, name: 'home' })

__tests__/matcher/pathParser.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ describe('Path parser', () => {
1313
expect(tokenizePath('')).toEqual([[]])
1414
})
1515

16+
it('not start with /', () => {
17+
expect(() => tokenizePath('a')).toThrowError(`"a" should be "/a"`)
18+
})
19+
1620
it('escapes :', () => {
1721
expect(tokenizePath('/\\:')).toEqual([
1822
[{ type: TokenType.Static, value: ':' }],

src/matcher/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ export function createRouterMatcher(
116116
parent.record.path + (path && connectingSlash + path)
117117
}
118118

119+
if (__DEV__ && normalizedRecord.path === '*') {
120+
throw new Error(
121+
'Catch all routes ("*") must now be defined using a param with a custom regexp.\n' +
122+
'See more at https://next.router.vuejs.org/guide/migration/#removed-star-or-catch-all-routes.'
123+
)
124+
}
125+
119126
// create the object before hand so it can be passed to children
120127
matcher = createRouteRecordMatcher(normalizedRecord, parent, options)
121128

src/matcher/pathTokenizer.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,18 @@ const VALID_PARAM_RE = /[a-zA-Z0-9_]/
4646
export function tokenizePath(path: string): Array<Token[]> {
4747
if (!path) return [[]]
4848
if (path === '/') return [[ROOT_TOKEN]]
49+
// // v3 catchAll must be renew
50+
// if (/^\/?\*/.test(path))
51+
// throw new Error(
52+
// `Catch all routes (/*) must now be defined using a parameter with a custom regex: /:catchAll(.*)`
53+
// )
4954
// remove the leading slash
50-
if (!path.startsWith('/'))
51-
throw new Error(`Route "${path}" should be "/${path}".`)
55+
if (__DEV__ && !path.startsWith('/')) {
56+
throw new Error(
57+
`Route path should start with a "/": "${path}" should be "/${path}". This will break in production.`
58+
)
59+
path = '/' + path
60+
}
5261

5362
// if (tokenCache.has(path)) return tokenCache.get(path)!
5463

0 commit comments

Comments
 (0)