Skip to content

Commit

Permalink
fix(history): do not call onReady on initial redirection
Browse files Browse the repository at this point in the history
Fix #3331

This bug existed since v3.3.0 and could create problems when doing SSR
  • Loading branch information
posva committed Sep 26, 2020
1 parent 8ae5d0f commit a1a290e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/history/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
})
}
}
}
Expand Down
39 changes: 39 additions & 0 deletions test/unit/specs/onReady.spec.js
Original file line number Diff line number Diff line change
@@ -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()
})
})
})

0 comments on commit a1a290e

Please sign in to comment.