Skip to content

Commit

Permalink
fix(history): initial redirect call onReady's onSuccess
Browse files Browse the repository at this point in the history
Fix #3225
  • Loading branch information
posva committed Jun 12, 2020
1 parent 2336dcb commit 4d484bf
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/history/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
createNavigationDuplicatedError,
createNavigationCancelledError,
createNavigationRedirectedError,
createNavigationAbortedError
createNavigationAbortedError,
NavigationFailureType
} from './errors'

export class History {
Expand All @@ -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
Expand Down Expand Up @@ -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)
})
}
}
}
)
Expand Down
33 changes: 33 additions & 0 deletions test/unit/specs/error-handling.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
})

0 comments on commit 4d484bf

Please sign in to comment.