Skip to content

Commit 0eac747

Browse files
committed
adjust history implementations
1 parent d62a152 commit 0eac747

File tree

5 files changed

+20
-44
lines changed

5 files changed

+20
-44
lines changed

flow/declarations.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ declare module 'path-to-regexp' {
55
}
66
}
77

8-
declare var __VUE_SSR_CONTEXT__: any;
9-
108
declare type RouterOptions = {
119
routes?: Array<RouteConfig>;
1210
mode?: string;

src/history/abstract.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/* @flow */
2-
/* globals __VUE_SSR_CONTEXT__ */
32

43
import type VueRouter from '../index'
54
import { History } from './base'
@@ -14,10 +13,6 @@ export class AbstractHistory extends History {
1413
this.index = 0
1514
}
1615

17-
onInit () {
18-
this.stack = [this.current]
19-
}
20-
2116
push (location: RawLocation) {
2217
super.transitionTo(location, route => {
2318
this.stack = this.stack.slice(0, this.index + 1).concat(route)
@@ -43,11 +38,4 @@ export class AbstractHistory extends History {
4338
this.updateRoute(location)
4439
})
4540
}
46-
47-
getLocation () {
48-
return (
49-
typeof __VUE_SSR_CONTEXT__ !== 'undefined' &&
50-
__VUE_SSR_CONTEXT__.url
51-
) || '/'
52-
}
5341
}

src/history/base.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ export class History {
1717
go: (n: number) => void;
1818
push: (loc: RawLocation) => void;
1919
replace: (loc: RawLocation) => void;
20-
onInit: (cb: Function) => void;
21-
getLocation: () => string;
2220

2321
constructor (router: VueRouter, base: ?string) {
2422
this.router = router
@@ -28,9 +26,6 @@ export class History {
2826
path: '__vue_router_init__'
2927
})
3028
this.pending = null
31-
this.transitionTo(this.getLocation(), route => {
32-
this.onInit(route)
33-
})
3429
}
3530

3631
listen (cb: Function) {

src/history/hash.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,25 @@ import { cleanPath } from '../util/path'
88
export class HashHistory extends History {
99
constructor (router: VueRouter, base: ?string, fallback: boolean) {
1010
super(router, base)
11+
1112
// check history fallback deeplinking
1213
if (fallback && this.checkFallback()) {
1314
return
1415
}
16+
1517
ensureSlash()
18+
this.transitionTo(getHash(), route => {
19+
// possible redirect on start
20+
if (getHash() !== route.fullPath) {
21+
replaceHash(route.fullPath)
22+
}
23+
})
24+
1625
window.addEventListener('hashchange', () => {
1726
this.onHashChange()
1827
})
1928
}
2029

21-
onInit () {
22-
// possible redirect on start
23-
if (getHash() !== this.current.fullPath) {
24-
replaceHash(this.current.fullPath)
25-
}
26-
}
27-
2830
checkFallback () {
2931
const location = getLocation(this.base)
3032
if (!/^\/#/.test(location)) {
@@ -39,7 +41,7 @@ export class HashHistory extends History {
3941
if (!ensureSlash()) {
4042
return
4143
}
42-
this.transitionTo(this.getLocation(), route => {
44+
this.transitionTo(getHash(), route => {
4345
replaceHash(route.fullPath)
4446
})
4547
}
@@ -59,10 +61,6 @@ export class HashHistory extends History {
5961
go (n: number) {
6062
window.history.go(n)
6163
}
62-
63-
getLocation () {
64-
return getHash()
65-
}
6664
}
6765

6866
function ensureSlash (): boolean {

src/history/html5.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,20 @@ export class HTML5History extends History {
1919
constructor (router: VueRouter, base: ?string) {
2020
super(router, base)
2121

22+
const initialLocation = getLocation(this.base)
23+
this.transitionTo(initialLocation, route => {
24+
// possible redirect on start
25+
const url = cleanPath(this.base + this.current.fullPath)
26+
if (initialLocation !== url) {
27+
replaceState(url)
28+
}
29+
})
30+
2231
const expectScroll = router.options.scrollBehavior
2332
window.addEventListener('popstate', e => {
2433
_key = e.state && e.state.key
2534
const current = this.current
26-
this.transitionTo(this.getLocation(), next => {
35+
this.transitionTo(getLocation(this.base), next => {
2736
if (expectScroll) {
2837
this.handleScroll(next, current, true)
2938
}
@@ -37,14 +46,6 @@ export class HTML5History extends History {
3746
}
3847
}
3948

40-
onInit () {
41-
// possible redirect on start
42-
const url = cleanPath(this.base + this.current.fullPath)
43-
if (this.getLocation() !== url) {
44-
replaceState(url)
45-
}
46-
}
47-
4849
go (n: number) {
4950
window.history.go(n)
5051
}
@@ -65,10 +66,6 @@ export class HTML5History extends History {
6566
})
6667
}
6768

68-
getLocation (): string {
69-
return getLocation(this.base)
70-
}
71-
7269
handleScroll (to: Route, from: Route, isPop: boolean) {
7370
const router = this.router
7471
if (!router.app) {

0 commit comments

Comments
 (0)