Skip to content

Fix "#" placed incorrectly when query string is present on load (#2125) #2253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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