Skip to content

Commit 4d484bf

Browse files
committed
fix(history): initial redirect call onReady's onSuccess
Fix #3225
1 parent 2336dcb commit 4d484bf

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

src/history/base.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import {
1515
createNavigationDuplicatedError,
1616
createNavigationCancelledError,
1717
createNavigationRedirectedError,
18-
createNavigationAbortedError
18+
createNavigationAbortedError,
19+
NavigationFailureType
1920
} from './errors'
2021

2122
export class History {
@@ -33,8 +34,8 @@ export class History {
3334

3435
// implemented by sub-classes
3536
+go: (n: number) => void
36-
+push: (loc: RawLocation) => void
37-
+replace: (loc: RawLocation) => void
37+
+push: (loc: RawLocation, onComplete?: Function, onAbort?: Function) => void
38+
+replace: (loc: RawLocation, onComplete?: Function, onAbort?: Function) => void
3839
+ensureURL: (push?: boolean) => void
3940
+getCurrentLocation: () => string
4041
+setupListeners: Function
@@ -102,9 +103,17 @@ export class History {
102103
}
103104
if (err && !this.ready) {
104105
this.ready = true
105-
this.readyErrorCbs.forEach(cb => {
106-
cb(err)
107-
})
106+
// Initial redirection should still trigger the onReady onSuccess
107+
// https://github.com/vuejs/vue-router/issues/3225
108+
if (!isRouterError(err, NavigationFailureType.redirected)) {
109+
this.readyErrorCbs.forEach(cb => {
110+
cb(err)
111+
})
112+
} else {
113+
this.readyCbs.forEach(cb => {
114+
cb(route)
115+
})
116+
}
108117
}
109118
}
110119
)

test/unit/specs/error-handling.spec.js

+33
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,37 @@ describe('error handling', () => {
150150
done()
151151
})
152152
})
153+
154+
// https://github.com/vuejs/vue-router/issues/3225
155+
it('should trigger onReady onSuccess when redirecting', done => {
156+
const router = new VueRouter({
157+
routes: [{ path: '/', component: {}}, { path: '/foo', component: {}}]
158+
})
159+
160+
const onError = jasmine.createSpy('onError')
161+
const onReadySuccess = jasmine.createSpy('onReadySuccess')
162+
const onReadyFail = jasmine.createSpy('onReadyFail')
163+
router.onError(onError)
164+
router.onReady(onReadySuccess, onReadyFail)
165+
166+
router.beforeEach((to, from, next) => {
167+
if (to.path === '/') next('/foo')
168+
else next()
169+
})
170+
171+
const pushCatch = jasmine.createSpy('pushCatch')
172+
173+
// initial navigation
174+
router
175+
.push('/')
176+
.catch(pushCatch)
177+
.finally(() => {
178+
expect(onReadyFail).not.toHaveBeenCalled()
179+
// in 3.2.0 it was called with undefined
180+
// expect(pushCatch).not.toHaveBeenCalled()
181+
expect(onError).not.toHaveBeenCalled()
182+
expect(onReadySuccess).toHaveBeenCalled()
183+
done()
184+
})
185+
})
153186
})

0 commit comments

Comments
 (0)