From 4d484bf81d733769d54091455a4a6e5dbafd62a1 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Fri, 12 Jun 2020 10:34:11 +0200 Subject: [PATCH] fix(history): initial redirect call onReady's onSuccess Fix #3225 --- src/history/base.js | 21 +++++++++++----- test/unit/specs/error-handling.spec.js | 33 ++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/src/history/base.js b/src/history/base.js index 4eb85a55e..c31d4d496 100644 --- a/src/history/base.js +++ b/src/history/base.js @@ -15,7 +15,8 @@ import { createNavigationDuplicatedError, createNavigationCancelledError, createNavigationRedirectedError, - createNavigationAbortedError + createNavigationAbortedError, + NavigationFailureType } from './errors' export class History { @@ -33,8 +34,8 @@ export class History { // implemented by sub-classes +go: (n: number) => void - +push: (loc: RawLocation) => void - +replace: (loc: RawLocation) => void + +push: (loc: RawLocation, onComplete?: Function, onAbort?: Function) => void + +replace: (loc: RawLocation, onComplete?: Function, onAbort?: Function) => void +ensureURL: (push?: boolean) => void +getCurrentLocation: () => string +setupListeners: Function @@ -102,9 +103,17 @@ export class History { } if (err && !this.ready) { this.ready = true - this.readyErrorCbs.forEach(cb => { - cb(err) - }) + // Initial redirection should still trigger the onReady onSuccess + // https://github.com/vuejs/vue-router/issues/3225 + if (!isRouterError(err, NavigationFailureType.redirected)) { + this.readyErrorCbs.forEach(cb => { + cb(err) + }) + } else { + this.readyCbs.forEach(cb => { + cb(route) + }) + } } } ) diff --git a/test/unit/specs/error-handling.spec.js b/test/unit/specs/error-handling.spec.js index 24a31145c..d0d6f6e1d 100644 --- a/test/unit/specs/error-handling.spec.js +++ b/test/unit/specs/error-handling.spec.js @@ -150,4 +150,37 @@ describe('error handling', () => { done() }) }) + + // https://github.com/vuejs/vue-router/issues/3225 + it('should trigger onReady onSuccess when redirecting', done => { + const router = new VueRouter({ + routes: [{ path: '/', component: {}}, { path: '/foo', component: {}}] + }) + + const onError = jasmine.createSpy('onError') + const onReadySuccess = jasmine.createSpy('onReadySuccess') + const onReadyFail = jasmine.createSpy('onReadyFail') + router.onError(onError) + router.onReady(onReadySuccess, onReadyFail) + + router.beforeEach((to, from, next) => { + if (to.path === '/') next('/foo') + else next() + }) + + const pushCatch = jasmine.createSpy('pushCatch') + + // initial navigation + router + .push('/') + .catch(pushCatch) + .finally(() => { + expect(onReadyFail).not.toHaveBeenCalled() + // in 3.2.0 it was called with undefined + // expect(pushCatch).not.toHaveBeenCalled() + expect(onError).not.toHaveBeenCalled() + expect(onReadySuccess).toHaveBeenCalled() + done() + }) + }) })