Skip to content

Commit a1a290e

Browse files
committed
fix(history): do not call onReady on initial redirection
Fix #3331 This bug existed since v3.3.0 and could create problems when doing SSR
1 parent 8ae5d0f commit a1a290e

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

src/history/base.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ export class History {
9494
// Exception should still be thrown
9595
throw e
9696
}
97+
const prev = this.current
9798
this.confirmTransition(
9899
route,
99100
() => {
100-
const prev = this.current
101101
this.updateRoute(route)
102102
onComplete && onComplete(route)
103103
this.ensureURL()
@@ -118,17 +118,15 @@ export class History {
118118
onAbort(err)
119119
}
120120
if (err && !this.ready) {
121-
this.ready = true
122-
// Initial redirection should still trigger the onReady onSuccess
121+
// Initial redirection should not mark the history as ready yet
122+
// because it's triggered by the redirection instead
123123
// https://github.com/vuejs/vue-router/issues/3225
124-
if (!isNavigationFailure(err, NavigationFailureType.redirected)) {
124+
// https://github.com/vuejs/vue-router/issues/3331
125+
if (!isNavigationFailure(err, NavigationFailureType.redirected) || prev !== START) {
126+
this.ready = true
125127
this.readyErrorCbs.forEach(cb => {
126128
cb(err)
127129
})
128-
} else {
129-
this.readyCbs.forEach(cb => {
130-
cb(route)
131-
})
132130
}
133131
}
134132
}

test/unit/specs/onReady.spec.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import Vue from 'vue'
2+
import VueRouter from '../../../src/index'
3+
4+
Vue.use(VueRouter)
5+
6+
describe('onReady order', () => {
7+
function factory () {
8+
const router = new VueRouter({
9+
mode: 'abstract',
10+
routes: [
11+
{ path: '/', component: {}},
12+
{ path: '/foo', component: {}}
13+
]
14+
})
15+
16+
return { router }
17+
}
18+
19+
it('should trigger onReady after push with redirect', done => {
20+
const { router } = factory()
21+
22+
let n = 0
23+
const count = 2
24+
router.onReady(() => {
25+
expect(router.currentRoute.path).toBe('/foo')
26+
if (++n === count) done()
27+
})
28+
29+
router.beforeEach((to, from, next) => {
30+
if (to.path === '/') next('/foo')
31+
else next()
32+
})
33+
34+
router.push('/').catch(() => {
35+
expect(router.currentRoute.path).toBe('/foo')
36+
if (++n === count) done()
37+
})
38+
})
39+
})

0 commit comments

Comments
 (0)