Skip to content

Commit 972e128

Browse files
committed
Fix "#" placed incorrectly when query string is present on load (vuejs#2125)
When navigating to a URL with a query string like myapp.com?foo=bar in hash mode, the hash is currently placed after the query string myapp.com?foo=bar#/ This fix correctly parses URLs with query strings so that the above URL will be myapp.com/#/?foo=bar after loading.
1 parent 4cb6699 commit 972e128

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

Diff for: src/history/hash.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,15 @@ export function getHash (): string {
108108
function getUrl (path) {
109109
const href = window.location.href
110110
const i = href.indexOf('#')
111-
const base = i >= 0 ? href.slice(0, i) : href
112-
return `${base}#${path}`
111+
let base = i >= 0 ? href.slice(0, i) : href
112+
let query = ''
113+
// If query string is present in URL, place it after the hash path
114+
if (href.indexOf('?') >= 0) {
115+
const j = href.indexOf('?')
116+
base = base.slice(0, j)
117+
query = href.slice(j)
118+
}
119+
return `${base}#${path}${query}`
113120
}
114121

115122
function pushHash (path) {

Diff for: test/e2e/specs/hash-mode.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = {
22
'Hash mode': function (browser) {
33
browser
4-
.url('http://localhost:8080/hash-mode/')
4+
.url('http://localhost:8080/hash-mode/?foo=bar')
55
.waitForElementVisible('#app', 1000)
66
.assert.count('li', 4)
77
.assert.count('li a', 3)
@@ -11,19 +11,19 @@ module.exports = {
1111
.assert.containsText('.view', 'home')
1212

1313
.click('li:nth-child(2) a')
14-
.assert.urlEquals('http://localhost:8080/hash-mode/#/foo')
14+
.assert.urlEquals('http://localhost:8080/hash-mode/#/foo?foo=bar')
1515
.assert.containsText('.view', 'foo')
1616

1717
.click('li:nth-child(3) a')
18-
.assert.urlEquals('http://localhost:8080/hash-mode/#/bar')
18+
.assert.urlEquals('http://localhost:8080/hash-mode/#/bar?foo=bar')
1919
.assert.containsText('.view', 'bar')
2020

2121
.click('li:nth-child(1) a')
22-
.assert.urlEquals('http://localhost:8080/hash-mode/#/')
22+
.assert.urlEquals('http://localhost:8080/hash-mode/#/?foo=bar')
2323
.assert.containsText('.view', 'home')
2424

2525
.click('li:nth-child(4)')
26-
.assert.urlEquals('http://localhost:8080/hash-mode/#/bar')
26+
.assert.urlEquals('http://localhost:8080/hash-mode/#/bar?foo=bar')
2727
.assert.containsText('.view', 'bar')
2828

2929
// check initial visit

0 commit comments

Comments
 (0)