Skip to content

Commit 7d7e048

Browse files
Sayegh7posva
authored andcommitted
feat: warn about root paths without a leading slash (#2591)
* fix(create-matcher): warn about root paths without a leading slash close #2550 * fix(create-route-map): warn about root paths without a leading slash close #2550 * fix(create-route-map): only warn about first route without slash * fix(create-route-map): handle case of '' route * fix(create-route-map): remove leftover console.log * fix(create-route-map): warn about root paths only * fix(create-route-map): show prettier warning message
1 parent 638278b commit 7d7e048

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/create-route-map.js

+12
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ export function createRouteMap (
3434
}
3535
}
3636

37+
if (process.env.NODE_ENV === 'development') {
38+
// warn if routes do not include leading slashes
39+
const found = pathList
40+
// check for missing leading slash
41+
.filter(path => path && path.charAt(0) !== '*' && path.charAt(0) !== '/')
42+
43+
if (found.length > 0) {
44+
const pathNames = found.map(path => `- ${path}`).join('\n')
45+
warn(false, `Non-nested routes must include a leading slash character. Fix the following routes: \n${pathNames}`)
46+
}
47+
}
48+
3749
return {
3850
pathList,
3951
pathMap,

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

+25
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,31 @@ describe('Creating Route Map', function () {
138138
)
139139
})
140140

141+
it('in development, warn if a path is missing a leading slash', function () {
142+
process.env.NODE_ENV = 'development'
143+
maps = createRouteMap([
144+
{ path: 'bar', name: 'bar', component: Bar }
145+
])
146+
expect(console.warn).toHaveBeenCalledTimes(1)
147+
expect(console.warn.calls.argsFor(0)[0]).toEqual('[vue-router] Non-nested routes must include a leading slash character. Fix the following routes: \n- bar')
148+
})
149+
150+
it('in development, it does not log the missing leading slash when routes are valid', function () {
151+
process.env.NODE_ENV = 'development'
152+
maps = createRouteMap([
153+
{ path: '/bar', name: 'bar', component: Bar }
154+
])
155+
expect(console.warn).not.toHaveBeenCalled()
156+
})
157+
158+
it('in production, it does not log the missing leading slash warning', function () {
159+
process.env.NODE_ENV = 'production'
160+
maps = createRouteMap([
161+
{ path: 'bar', name: 'bar', component: Bar }
162+
])
163+
expect(console.warn).not.toHaveBeenCalled()
164+
})
165+
141166
describe('path-to-regexp options', function () {
142167
const routes = [
143168
{ path: '/foo', name: 'foo', component: Foo },

0 commit comments

Comments
 (0)