Skip to content

Commit 2a89fad

Browse files
committed
fix empty path matching (fix #587)
1 parent 0e74b86 commit 2a89fad

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed

src/history/base.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@ export class History {
1111
base: string;
1212
current: Route;
1313
pending: ?Route;
14-
cb: Function;
14+
cb: (r: Route) => void;
1515

1616
// implemented by sub-classes
17-
go: Function;
18-
push: Function;
19-
replace: Function;
20-
onInit: Function;
17+
go: (n: number) => void;
18+
push: (loc: RawLocation) => void;
19+
replace: (loc: RawLocation) => void;
20+
onInit: (cb: Function) => void;
21+
getLocation: () => string;
2122

2223
constructor (router: VueRouter, base: ?string) {
2324
this.router = router
24-
this.base = normalizeBae(base)
25+
this.base = normalizeBase(base)
2526
// start with a route object that stands for "nowhere"
2627
this.current = createRoute(null, {
2728
path: '__vue_router_init__'
@@ -87,13 +88,9 @@ export class History {
8788
hook && hook(route)
8889
})
8990
}
90-
91-
getLocation (): string {
92-
return '/'
93-
}
9491
}
9592

96-
function normalizeBae (base: ?string): string {
93+
function normalizeBase (base: ?string): string {
9794
if (!base) {
9895
if (inBrowser) {
9996
// respect <base> tag

src/history/hash.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ export class HashHistory extends History {
1212
if (fallback && this.checkFallback()) {
1313
return
1414
}
15+
ensureSlash()
1516
window.addEventListener('hashchange', () => {
1617
this.onHashChange()
1718
})
1819
}
1920

2021
onInit () {
21-
ensureSlash()
2222
// possible redirect on start
2323
if (getHash() !== this.current.fullPath) {
2424
replaceHash(this.current.fullPath)

src/history/html5.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export function getLocation (base: string): string {
112112
if (base && path.indexOf(base) === 0) {
113113
path = path.slice(base.length)
114114
}
115-
return path + window.location.search + window.location.hash
115+
return (path || '/') + window.location.search + window.location.hash
116116
}
117117

118118
function pushState (url: string, replace?: boolean) {

test/unit/specs/location.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,22 @@ describe('Location utils', () => {
1313
}))
1414
})
1515

16+
it('empty string', function () {
17+
const loc = normalizeLocation('', { path: '/abc' })
18+
expect(loc._normalized).toBe(true)
19+
expect(loc.path).toBe('/abc')
20+
expect(loc.hash).toBe('')
21+
expect(JSON.stringify(loc.query)).toBe(JSON.stringify({}))
22+
})
23+
24+
it('undefined', function () {
25+
const loc = normalizeLocation({}, { path: '/abc' })
26+
expect(loc._normalized).toBe(true)
27+
expect(loc.path).toBe('/abc')
28+
expect(loc.hash).toBe('')
29+
expect(JSON.stringify(loc.query)).toBe(JSON.stringify({}))
30+
})
31+
1632
it('relative', () => {
1733
const loc = normalizeLocation('abc?foo=bar&baz=qux#hello', {
1834
path: '/root/next'

0 commit comments

Comments
 (0)