Skip to content

Commit

Permalink
fix(history): mark redundant navigation as pending
Browse files Browse the repository at this point in the history
Fix #3133
  • Loading branch information
posva committed Sep 24, 2020
1 parent 4da7021 commit 893d86b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
12 changes: 12 additions & 0 deletions examples/basic/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ const router = new VueRouter({
]
})

router.beforeEach((to, from, next) => {
if (to.query.delay) {
setTimeout(() => {
next()
}, Number(to.query.delay))
} else {
next()
}
})

// 4. Create and mount root instance.
// Make sure to inject the router.
// Route components will be rendered inside <router-view>.
Expand All @@ -73,6 +83,8 @@ const vueInstance = new Vue({
</li>
</router-link>
<li><router-link to="/foo" replace>/foo (replace)</router-link></li>
<li><router-link to="/?delay=200">/ (delay of 500ms)</router-link></li>
<li><router-link to="/foo?delay=200">/foo (delay of 500ms)</router-link></li>
</ul>
<button id="navigate-btn" @click="navigateAndIncrement">On Success</button>
<pre id="counter">{{ n }}</pre>
Expand Down
2 changes: 1 addition & 1 deletion src/history/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export class History {

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

this.pending = route
const iterator = (hook: NavigationGuard, next) => {
if (this.pending !== route) {
return abort(createNavigationCancelledError(current, route))
Expand Down
18 changes: 16 additions & 2 deletions test/e2e/specs/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ module.exports = {
browser
.url('http://localhost:8080/basic/')
.waitForElementVisible('#app', 1000)
.assert.count('li', 9)
.assert.count('li a', 9)
.assert.count('li', 11)
.assert.count('li a', 11)
// assert correct href with base
.assert.attributeContains('li:nth-child(1) a', 'href', '/basic/')
.assert.attributeContains('li:nth-child(2) a', 'href', '/basic/foo')
Expand Down Expand Up @@ -76,5 +76,19 @@ module.exports = {
.assert.containsText('#popstate-count', '0 popstate listeners')

.end()
},

'cancelling ongoing navigations': function (browser) {
browser
.url('http://localhost:8080/basic/?delay=200')
.waitForElementVisible('#app', 1000)
.assert.containsText('.view', 'home')
// go to foo with a delay
.click('li:nth-child(11) a')
.click('li:nth-child(10) a')
.waitFor(300)
// we should stay at /basic after the delay
.assert.urlEquals('http://localhost:8080/basic/?delay=200')
.assert.containsText('.view', 'home')
}
}

0 comments on commit 893d86b

Please sign in to comment.