Skip to content

Commit 66491c1

Browse files
committedJul 27, 2022
fix: avoid restore on cancelled pop navigations
1 parent 301b52c commit 66491c1

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed
 

‎packages/router/e2e/guards-instances/index.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,14 @@ function createTestComponent(key: string) {
9696
}
9797

9898
const Foo = createTestComponent('Foo')
99-
const Bar = createTestComponent('Bar')
10099
const One = createTestComponent('One')
101100
const Two = createTestComponent('Two')
102101
const Aux = createTestComponent('Aux')
103102

103+
const WithId = defineComponent({
104+
template: `<p :id="'with-id-' + $route.params.id">id: {{ $route.params.id }}</p>`,
105+
})
106+
104107
const webHistory = createWebHistory('/guards-instances')
105108
const router = createRouter({
106109
history: webHistory,
@@ -117,7 +120,8 @@ const router = createRouter({
117120
// TODO: test that the onBeforeRouteUpdate isn't kept
118121
{
119122
path: '/b/:id',
120-
component: Bar,
123+
name: 'id',
124+
component: WithId,
121125
},
122126
{
123127
path: '/named-one',
@@ -136,6 +140,17 @@ const router = createRouter({
136140
],
137141
})
138142

143+
router.beforeEach(async (to, from) => {
144+
if (to.name === 'id') {
145+
const toId = Number(to.params.id)
146+
const fromId = Number(from.params.id)
147+
// only do it when we are going backwards
148+
if (!Number.isNaN(toId) && !Number.isNaN(fromId) && toId < fromId) {
149+
await new Promise(r => setTimeout(r, 250))
150+
}
151+
}
152+
})
153+
139154
// preserve existing query
140155
const originalPush = router.push
141156
router.push = to => {
@@ -187,6 +202,9 @@ leaves: {{ state.leave }}
187202
<li><router-link id="update-query" :to="{ query: { n: (Number($route.query.n) || 0) + 1 }}" v-slot="{ route }">{{ route.fullPath }}</router-link></li>
188203
<li><router-link to="/named-one">/named-one</router-link></li>
189204
<li><router-link to="/named-two">/named-two</router-link></li>
205+
<li><router-link to="/b/1">/b/1</router-link></li>
206+
<li><router-link to="/b/2">/b/2</router-link></li>
207+
<li><router-link to="/b/3">/b/3</router-link></li>
190208
</ul>
191209
192210
<template v-if="testCase === 'keepalive'">

‎packages/router/e2e/specs/guards-instances.js

+20
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ function testCase(browser, name) {
3838
)
3939
}
4040

41+
const baseURL = 'http://localhost:3000/guards-instances'
42+
4143
module.exports = {
4244
'@tags': [],
4345

@@ -61,6 +63,24 @@ module.exports = {
6163
browser.end()
6264
},
6365

66+
/** @type {import('nightwatch').NightwatchTest} */
67+
'cancel pending pop navigations': function (browser) {
68+
browser
69+
.url(baseURL + '/')
70+
.waitForElementPresent('#app > *', 1000)
71+
72+
.click('#test-normal')
73+
.click('li:nth-child(11) a')
74+
.click('li:nth-child(12) a')
75+
.click('li:nth-child(13) a')
76+
.back()
77+
.back()
78+
.waitForElementPresent('#app > #with-id-1', 1000)
79+
.assert.urlEquals(baseURL + '/b/1?testCase=')
80+
81+
.end()
82+
},
83+
6484
/** @type {import('nightwatch').NightwatchTest} */
6585
'guards instances transition': function (browser) {
6686
browser

‎packages/router/src/router.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,9 @@ export function createRouter(options: RouterOptions): Router {
10341034
return Promise.reject()
10351035
}
10361036
// do not restore history on unknown direction
1037-
if (info.delta) routerHistory.go(-info.delta, false)
1037+
if (info.delta) {
1038+
routerHistory.go(-info.delta, false)
1039+
}
10381040
// unrecognized error, transfer to the global handler
10391041
return triggerError(error, toLocation, from)
10401042
})
@@ -1050,7 +1052,12 @@ export function createRouter(options: RouterOptions): Router {
10501052

10511053
// revert the navigation
10521054
if (failure) {
1053-
if (info.delta) {
1055+
if (
1056+
info.delta &&
1057+
// a new navigation has been triggered, so we do not want to revert, that will change the current history
1058+
// entry while a different route is displayed
1059+
!isNavigationFailure(failure, ErrorTypes.NAVIGATION_CANCELLED)
1060+
) {
10541061
routerHistory.go(-info.delta, false)
10551062
} else if (
10561063
info.type === NavigationType.pop &&

0 commit comments

Comments
 (0)