From 972e1288c958095e351548bb296aca8adb11897e Mon Sep 17 00:00:00 2001 From: Devin Brenton Date: Mon, 11 Jun 2018 11:35:57 -0400 Subject: [PATCH] Fix "#" placed incorrectly when query string is present on load (#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. --- src/history/hash.js | 11 +++++++++-- test/e2e/specs/hash-mode.js | 10 +++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/history/hash.js b/src/history/hash.js index f4a2f3828..578e8d87c 100644 --- a/src/history/hash.js +++ b/src/history/hash.js @@ -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) { diff --git a/test/e2e/specs/hash-mode.js b/test/e2e/specs/hash-mode.js index d6b74369f..9839dbff0 100644 --- a/test/e2e/specs/hash-mode.js +++ b/test/e2e/specs/hash-mode.js @@ -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) @@ -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