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

Warn about root paths without a leading slash (fix #2550) #2591

Merged
merged 7 commits into from
Aug 30, 2019
12 changes: 12 additions & 0 deletions src/create-route-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ export function createRouteMap (
}
}

if (process.env.NODE_ENV === 'development') {
// warn if routes do not include leading slashes
const found = pathList
// check for missing leading slash
.filter(path => path && path.charAt(0) !== '*' && path.charAt(0) !== '/')

if (found.length > 0) {
const pathNames = found.map(path => `- ${path}`).join('\n')
warn(false, `Non-nested routes must include a leading slash character. Fix the following routes: \n${pathNames}`)
}
}

return {
pathList,
pathMap,
Expand Down
25 changes: 25 additions & 0 deletions test/unit/specs/create-map.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,31 @@ describe('Creating Route Map', function () {
expect(console.warn.calls.argsFor(0)[0]).toMatch('vue-router] Duplicate param keys in route with path: "/foo/:id/bar/:id"')
})

it('in development, warn if a path is missing a leading slash', function () {
process.env.NODE_ENV = 'development'
maps = createRouteMap([
{ path: 'bar', name: 'bar', component: Bar }
])
expect(console.warn).toHaveBeenCalledTimes(1)
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')
})

it('in development, it does not log the missing leading slash when routes are valid', function () {
process.env.NODE_ENV = 'development'
maps = createRouteMap([
{ path: '/bar', name: 'bar', component: Bar }
])
expect(console.warn).not.toHaveBeenCalled()
})

it('in production, it does not log the missing leading slash warning', function () {
process.env.NODE_ENV = 'production'
maps = createRouteMap([
{ path: 'bar', name: 'bar', component: Bar }
])
expect(console.warn).not.toHaveBeenCalled()
})

describe('path-to-regexp options', function () {
const routes = [
{ path: '/foo', name: 'foo', component: Foo },
Expand Down