Skip to content

Commit a4ec3e2

Browse files
committed
fix: preserve history state when reloading
1 parent 8fdd9c5 commit a4ec3e2

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

examples/basic/app.js

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ new Vue({
4848
<a :href="props.href" @click="props.navigate">{{ props.route.path }} (with v-slot).</a>
4949
</li>
5050
</router-link>
51+
<li><router-link to="/foo" replace>/foo (replace)</router-link></li>
5152
</ul>
5253
<button id="navigate-btn" @click="navigateAndIncrement">On Success</button>
5354
<pre id="counter">{{ n }}</pre>

src/util/scroll.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import type Router from '../index'
44
import { assert } from './warn'
55
import { getStateKey, setStateKey } from './state-key'
6+
import { extend } from './misc'
67

78
const positionStore = Object.create(null)
89

@@ -14,7 +15,10 @@ export function setupScroll () {
1415
// location.host contains the port and location.hostname doesn't
1516
const protocolAndPath = window.location.protocol + '//' + window.location.host
1617
const absolutePath = window.location.href.replace(protocolAndPath, '')
17-
window.history.replaceState({ key: getStateKey() }, '', absolutePath)
18+
// preserve existing history state as it could be overriden by the user
19+
const stateCopy = extend({}, window.history.state)
20+
stateCopy.key = getStateKey()
21+
window.history.replaceState(stateCopy, '', absolutePath)
1822
window.addEventListener('popstate', e => {
1923
saveScrollPosition()
2024
if (e.state && e.state.key) {

test/e2e/specs/basic.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ module.exports = {
99
browser
1010
.url('http://localhost:8080/basic/')
1111
.waitForElementVisible('#app', 1000)
12-
.assert.count('li', 8)
13-
.assert.count('li a', 8)
12+
.assert.count('li', 9)
13+
.assert.count('li a', 9)
1414
// assert correct href with base
1515
.assert.attributeContains('li:nth-child(1) a', 'href', '/basic/')
1616
.assert.attributeContains('li:nth-child(2) a', 'href', '/basic/foo')

test/e2e/specs/history-state.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const bsStatus = require('../browserstack-send-status')
2+
3+
module.exports = {
4+
...bsStatus(),
5+
6+
'@tags': ['history'],
7+
8+
'history state': function (browser) {
9+
browser
10+
.url('http://localhost:8080/scroll-behavior/')
11+
.waitForElementVisible('#app', 1000)
12+
13+
.execute(function () {
14+
window.scrollTo(0, 100)
15+
const key = window.history.state.key
16+
window.history.replaceState({ key, foo: 'foo' }, '', window.location.pathname)
17+
})
18+
.url('http://localhost:8080/scroll-behavior/')
19+
.waitForElementVisible('#app', 1000)
20+
.assert.evaluate(function () {
21+
return window.history.state.foo === 'foo'
22+
}, null, 'keeps existing state when reloading')
23+
24+
// check on navigation
25+
.url('http://localhost:8080/basic/')
26+
.click('li:nth-child(2) a')
27+
.assert.urlEquals('http://localhost:8080/basic/foo')
28+
.execute(function () {
29+
window.scrollTo(0, 100)
30+
const key = window.history.state.key
31+
window.history.replaceState({ key, foo: 'foo' }, '', window.location.pathname)
32+
})
33+
.click('li:nth-child(3) a')
34+
.assert.urlEquals('http://localhost:8080/basic/bar')
35+
.execute(function () {
36+
window.history.back()
37+
})
38+
.assert.evaluate(function () {
39+
return window.history.state.foo === 'foo'
40+
}, null, 'keeps existing state when navigating back')
41+
.click('li:nth-child(3) a')
42+
.assert.urlEquals('http://localhost:8080/basic/bar')
43+
.execute(function () {
44+
window.scrollTo(0, 100)
45+
const key = window.history.state.key
46+
window.history.replaceState({ key, bar: 'bar' }, '', window.location.pathname)
47+
})
48+
.click('li:nth-child(9) a')
49+
.assert.urlEquals('http://localhost:8080/basic/foo')
50+
.assert.evaluate(function () {
51+
return window.history.state.bar === 'bar'
52+
}, null, 'keeps existing state when replacing')
53+
54+
.end()
55+
}
56+
}

0 commit comments

Comments
 (0)