Skip to content

Commit b7715dc

Browse files
authored
fix(hash): correctly place query if placed before hash (#2851)
Fixes #2125 Closes #2262
1 parent d8499b6 commit b7715dc

File tree

2 files changed

+61
-32
lines changed

2 files changed

+61
-32
lines changed

src/history/hash.js

+49-32
Original file line numberDiff line numberDiff line change
@@ -28,38 +28,49 @@ export class HashHistory extends History {
2828
setupScroll()
2929
}
3030

31-
window.addEventListener(supportsPushState ? 'popstate' : 'hashchange', () => {
32-
const current = this.current
33-
if (!ensureSlash()) {
34-
return
35-
}
36-
this.transitionTo(getHash(), route => {
37-
if (supportsScroll) {
38-
handleScroll(this.router, route, current, true)
39-
}
40-
if (!supportsPushState) {
41-
replaceHash(route.fullPath)
31+
window.addEventListener(
32+
supportsPushState ? 'popstate' : 'hashchange',
33+
() => {
34+
const current = this.current
35+
if (!ensureSlash()) {
36+
return
4237
}
43-
})
44-
})
38+
this.transitionTo(getHash(), route => {
39+
if (supportsScroll) {
40+
handleScroll(this.router, route, current, true)
41+
}
42+
if (!supportsPushState) {
43+
replaceHash(route.fullPath)
44+
}
45+
})
46+
}
47+
)
4548
}
4649

4750
push (location: RawLocation, onComplete?: Function, onAbort?: Function) {
4851
const { current: fromRoute } = this
49-
this.transitionTo(location, route => {
50-
pushHash(route.fullPath)
51-
handleScroll(this.router, route, fromRoute, false)
52-
onComplete && onComplete(route)
53-
}, onAbort)
52+
this.transitionTo(
53+
location,
54+
route => {
55+
pushHash(route.fullPath)
56+
handleScroll(this.router, route, fromRoute, false)
57+
onComplete && onComplete(route)
58+
},
59+
onAbort
60+
)
5461
}
5562

5663
replace (location: RawLocation, onComplete?: Function, onAbort?: Function) {
5764
const { current: fromRoute } = this
58-
this.transitionTo(location, route => {
59-
replaceHash(route.fullPath)
60-
handleScroll(this.router, route, fromRoute, false)
61-
onComplete && onComplete(route)
62-
}, onAbort)
65+
this.transitionTo(
66+
location,
67+
route => {
68+
replaceHash(route.fullPath)
69+
handleScroll(this.router, route, fromRoute, false)
70+
onComplete && onComplete(route)
71+
},
72+
onAbort
73+
)
6374
}
6475

6576
go (n: number) {
@@ -81,9 +92,7 @@ export class HashHistory extends History {
8192
function checkFallback (base) {
8293
const location = getLocation(base)
8394
if (!/^\/#/.test(location)) {
84-
window.location.replace(
85-
cleanPath(base + '/#' + location)
86-
)
95+
window.location.replace(cleanPath(base + '/#' + location))
8796
return true
8897
}
8998
}
@@ -112,20 +121,28 @@ export function getHash (): string {
112121
const searchIndex = href.indexOf('?')
113122
if (searchIndex < 0) {
114123
const hashIndex = href.indexOf('#')
115-
if (hashIndex > -1) href = decodeURI(href.slice(0, hashIndex)) + href.slice(hashIndex)
116-
else href = decodeURI(href)
124+
if (hashIndex > -1) {
125+
href = decodeURI(href.slice(0, hashIndex)) + href.slice(hashIndex)
126+
} else href = decodeURI(href)
117127
} else {
118-
if (searchIndex > -1) href = decodeURI(href.slice(0, searchIndex)) + href.slice(searchIndex)
128+
if (searchIndex > -1) {
129+
href = decodeURI(href.slice(0, searchIndex)) + href.slice(searchIndex)
130+
}
119131
}
120132

121133
return href
122134
}
123135

124136
function getUrl (path) {
125137
const href = window.location.href
126-
const i = href.indexOf('#')
127-
const base = i >= 0 ? href.slice(0, i) : href
128-
return `${base}#${path}`
138+
const hashPos = href.indexOf('#')
139+
let base = hashPos > -1 ? href.slice(0, hashPos) : href
140+
141+
const searchPos = base.indexOf('?')
142+
const query = searchPos > -1 ? base.slice(searchPos) : ''
143+
base = query ? base.slice(0, searchPos) : base
144+
145+
return `${base}#${path + query}`
129146
}
130147

131148
function pushHash (path) {

test/e2e/specs/hash-mode.js

+12
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ module.exports = {
5757
.waitForElementVisible('#app', 1000)
5858
.assert.containsText('.view', 'unicode: ñ')
5959
.assert.containsText('#query-t', '%')
60+
61+
// correctly placing query
62+
// https://github.com/vuejs/vue-router/issues/2125
63+
.url('http://localhost:8080/hash-mode/?key=foo')
64+
.waitForElementVisible('#app', 1000)
65+
.assert.urlEquals('http://localhost:8080/hash-mode/#/?key=foo')
66+
.url('http://localhost:8080/hash-mode?key=foo')
67+
.waitForElementVisible('#app', 1000)
68+
.assert.urlEquals('http://localhost:8080/hash-mode/#/?key=foo')
69+
.url('http://localhost:8080/hash-mode?key=foo#other')
70+
.waitForElementVisible('#app', 1000)
71+
.assert.urlEquals('http://localhost:8080/hash-mode/#/other?key=foo')
6072
.end()
6173
}
6274
}

0 commit comments

Comments
 (0)