Skip to content

Commit 893d86b

Browse files
committed
fix(history): mark redundant navigation as pending
Fix #3133
1 parent 4da7021 commit 893d86b

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

examples/basic/app.js

+12
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ const router = new VueRouter({
4848
]
4949
})
5050

51+
router.beforeEach((to, from, next) => {
52+
if (to.query.delay) {
53+
setTimeout(() => {
54+
next()
55+
}, Number(to.query.delay))
56+
} else {
57+
next()
58+
}
59+
})
60+
5161
// 4. Create and mount root instance.
5262
// Make sure to inject the router.
5363
// Route components will be rendered inside <router-view>.
@@ -73,6 +83,8 @@ const vueInstance = new Vue({
7383
</li>
7484
</router-link>
7585
<li><router-link to="/foo" replace>/foo (replace)</router-link></li>
86+
<li><router-link to="/?delay=200">/ (delay of 500ms)</router-link></li>
87+
<li><router-link to="/foo?delay=200">/foo (delay of 500ms)</router-link></li>
7688
</ul>
7789
<button id="navigate-btn" @click="navigateAndIncrement">On Success</button>
7890
<pre id="counter">{{ n }}</pre>

src/history/base.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ export class History {
137137

138138
confirmTransition (route: Route, onComplete: Function, onAbort?: Function) {
139139
const current = this.current
140+
this.pending = route
140141
const abort = err => {
141142
// changed after adding errors with
142143
// https://github.com/vuejs/vue-router/pull/3047 before that change,
@@ -183,7 +184,6 @@ export class History {
183184
resolveAsyncComponents(activated)
184185
)
185186

186-
this.pending = route
187187
const iterator = (hook: NavigationGuard, next) => {
188188
if (this.pending !== route) {
189189
return abort(createNavigationCancelledError(current, route))

test/e2e/specs/basic.js

+16-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', 9)
13-
.assert.count('li a', 9)
12+
.assert.count('li', 11)
13+
.assert.count('li a', 11)
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')
@@ -76,5 +76,19 @@ module.exports = {
7676
.assert.containsText('#popstate-count', '0 popstate listeners')
7777

7878
.end()
79+
},
80+
81+
'cancelling ongoing navigations': function (browser) {
82+
browser
83+
.url('http://localhost:8080/basic/?delay=200')
84+
.waitForElementVisible('#app', 1000)
85+
.assert.containsText('.view', 'home')
86+
// go to foo with a delay
87+
.click('li:nth-child(11) a')
88+
.click('li:nth-child(10) a')
89+
.waitFor(300)
90+
// we should stay at /basic after the delay
91+
.assert.urlEquals('http://localhost:8080/basic/?delay=200')
92+
.assert.containsText('.view', 'home')
7993
}
8094
}

0 commit comments

Comments
 (0)