Skip to content

Commit b2fae05

Browse files
committed
extract ensureURL logic
1 parent d6e80cb commit b2fae05

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

src/history/base.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export class History {
1717
go: (n: number) => void;
1818
push: (loc: RawLocation) => void;
1919
replace: (loc: RawLocation) => void;
20+
ensureURL: () => void;
2021

2122
constructor (router: VueRouter, base: ?string) {
2223
this.router = router
@@ -37,11 +38,13 @@ export class History {
3738
this.confirmTransition(route, () => {
3839
this.updateRoute(route)
3940
cb && cb(route)
41+
this.ensureURL()
4042
})
4143
}
4244

4345
confirmTransition (route: Route, cb: Function) {
4446
if (isSameRoute(route, this.current)) {
47+
this.ensureURL()
4548
return
4649
}
4750

src/history/hash.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@ export class HashHistory extends History {
1515
}
1616

1717
ensureSlash()
18-
this.transitionTo(getHash(), route => {
19-
// possible redirect on start
20-
if (getHash() !== route.fullPath) {
21-
replaceHash(route.fullPath)
22-
}
23-
})
18+
this.transitionTo(getHash())
2419

2520
window.addEventListener('hashchange', () => {
2621
this.onHashChange()
@@ -37,6 +32,12 @@ export class HashHistory extends History {
3732
}
3833
}
3934

35+
ensureURL () {
36+
if (getHash() !== this.current.fullPath) {
37+
replaceHash(this.current.fullPath)
38+
}
39+
}
40+
4041
onHashChange () {
4142
if (!ensureSlash()) {
4243
return

src/history/html5.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,7 @@ 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-
})
22+
this.transitionTo(getLocation(this.base))
3023

3124
const expectScroll = router.options.scrollBehavior
3225
window.addEventListener('popstate', e => {
@@ -66,6 +59,12 @@ export class HTML5History extends History {
6659
})
6760
}
6861

62+
ensureURL () {
63+
if (getLocation(this.base) !== this.current.fullPath) {
64+
replaceState(cleanPath(this.base + this.current.fullPath))
65+
}
66+
}
67+
6968
handleScroll (to: Route, from: Route, isPop: boolean) {
7069
const router = this.router
7170
if (!router.app) {

0 commit comments

Comments
 (0)