diff --git a/src/history/base.js b/src/history/base.js index 70158e395..05a5652f9 100644 --- a/src/history/base.js +++ b/src/history/base.js @@ -94,10 +94,10 @@ export class History { // Exception should still be thrown throw e } + const prev = this.current this.confirmTransition( route, () => { - const prev = this.current this.updateRoute(route) onComplete && onComplete(route) this.ensureURL() @@ -118,17 +118,15 @@ export class History { onAbort(err) } if (err && !this.ready) { - this.ready = true - // Initial redirection should still trigger the onReady onSuccess + // Initial redirection should not mark the history as ready yet + // because it's triggered by the redirection instead // https://github.com/vuejs/vue-router/issues/3225 - if (!isNavigationFailure(err, NavigationFailureType.redirected)) { + // https://github.com/vuejs/vue-router/issues/3331 + if (!isNavigationFailure(err, NavigationFailureType.redirected) || prev !== START) { + this.ready = true this.readyErrorCbs.forEach(cb => { cb(err) }) - } else { - this.readyCbs.forEach(cb => { - cb(route) - }) } } } diff --git a/test/unit/specs/onReady.spec.js b/test/unit/specs/onReady.spec.js new file mode 100644 index 000000000..1590151dd --- /dev/null +++ b/test/unit/specs/onReady.spec.js @@ -0,0 +1,39 @@ +import Vue from 'vue' +import VueRouter from '../../../src/index' + +Vue.use(VueRouter) + +describe('onReady order', () => { + function factory () { + const router = new VueRouter({ + mode: 'abstract', + routes: [ + { path: '/', component: {}}, + { path: '/foo', component: {}} + ] + }) + + return { router } + } + + it('should trigger onReady after push with redirect', done => { + const { router } = factory() + + let n = 0 + const count = 2 + router.onReady(() => { + expect(router.currentRoute.path).toBe('/foo') + if (++n === count) done() + }) + + router.beforeEach((to, from, next) => { + if (to.path === '/') next('/foo') + else next() + }) + + router.push('/').catch(() => { + expect(router.currentRoute.path).toBe('/foo') + if (++n === count) done() + }) + }) +})