Skip to content

Commit

Permalink
Fix "#" placed incorrectly when query string is present on load (vuej…
Browse files Browse the repository at this point in the history
…s#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.
  • Loading branch information
devin-brenton committed Jun 11, 2018
1 parent 4cb6699 commit 972e128
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
11 changes: 9 additions & 2 deletions src/history/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,15 @@ export function getHash (): string {
function getUrl (path) {
const href = window.location.href
const i = href.indexOf('#')
const base = i >= 0 ? href.slice(0, i) : href
return `${base}#${path}`
let base = i >= 0 ? href.slice(0, i) : href
let query = ''
// If query string is present in URL, place it after the hash path
if (href.indexOf('?') >= 0) {
const j = href.indexOf('?')
base = base.slice(0, j)
query = href.slice(j)
}
return `${base}#${path}${query}`
}

function pushHash (path) {
Expand Down
10 changes: 5 additions & 5 deletions test/e2e/specs/hash-mode.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
'Hash mode': function (browser) {
browser
.url('http://localhost:8080/hash-mode/')
.url('http://localhost:8080/hash-mode/?foo=bar')
.waitForElementVisible('#app', 1000)
.assert.count('li', 4)
.assert.count('li a', 3)
Expand All @@ -11,19 +11,19 @@ module.exports = {
.assert.containsText('.view', 'home')

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

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

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

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

// check initial visit
Expand Down

0 comments on commit 972e128

Please sign in to comment.