Skip to content

Commit

Permalink
feat(warn): warn multiple leading slashes
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed May 12, 2020
1 parent e72e4ba commit 87c5e53
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
20 changes: 20 additions & 0 deletions __tests__/warnings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,24 @@ describe('warnings', () => {
await expect(router.push('/foo')).resolves.toBe(undefined)
expect('with path "/foo" is a function').toHaveBeenWarned()
})

it('should warn if multiple leading slashes with raw location', async () => {
const router = createRouter({
history: createMemoryHistory(),
routes: [{ path: '/foo', component }],
})

await expect(router.push('//not-valid')).resolves.toBe(undefined)
expect('cannot start with multiple slashes').toHaveBeenWarned()
})

it('should warn if multiple leading slashes with object location', async () => {
const router = createRouter({
history: createMemoryHistory(),
routes: [{ path: '/foo', component }],
})

await expect(router.push({ path: '//not-valid' })).resolves.toBe(undefined)
expect('cannot start with multiple slashes').toHaveBeenWarned()
})
})
16 changes: 16 additions & 0 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,14 @@ export function createRouter(options: RouterOptions): Router {
currentLocation
)

if (__DEV__) {
let href = routerHistory.base + locationNormalized.fullPath
if (href.startsWith('//'))
warn(
`Location "${rawLocation}" resolved to "${href}". A resolved location cannot start with multiple slashes.`
)
}

return {
// fullPath: locationNormalized.fullPath,
// query: locationNormalized.query,
Expand Down Expand Up @@ -303,6 +311,14 @@ export function createRouter(options: RouterOptions): Router {
path: matchedRoute.path,
})

if (__DEV__) {
let href = routerHistory.base + fullPath
if (href.startsWith('//'))
warn(
`Location "${rawLocation}" resolved to "${href}". A resolved location cannot start with multiple slashes.`
)
}

return {
fullPath,
// keep the hash encoded so fullPath is effectively path + encodedQuery +
Expand Down

0 comments on commit 87c5e53

Please sign in to comment.