Skip to content

Commit b271ee0

Browse files
committed
only abort previous transition when the new one is validated.
1 parent 73b9913 commit b271ee0

File tree

3 files changed

+22
-27
lines changed

3 files changed

+22
-27
lines changed

src/router/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export default class Router {
4646
this._started = false
4747
this._currentRoute = {}
4848
this._currentTransition = null
49+
this._previousTransition = null
4950
this._notFoundHandler = null
5051
this._beforeEachHook = null
5152

src/router/internal.js

+14-11
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,6 @@ export default function (Vue, Router) {
124124
let prevRoute = this._currentRoute
125125
let prevTransition = this._currentTransition
126126

127-
// abort ongoing transition
128-
if (prevTransition && path !== prevTransition.to.path) {
129-
prevTransition.aborted = true
130-
}
131-
132127
// do nothing if going to the same route.
133128
// the route only changes when a transition successfully
134129
// reaches activation; we don't need to do anything
@@ -140,8 +135,9 @@ export default function (Vue, Router) {
140135

141136
// construct new route and transition context
142137
let route = new Route(path, this)
143-
let transition = this._currentTransition =
144-
new RouteTransition(this, route, prevRoute)
138+
let transition = new RouteTransition(this, route, prevRoute)
139+
this._prevTransition = prevTransition
140+
this._currentTransition = transition
145141

146142
if (!this.app) {
147143
// initial render
@@ -175,15 +171,22 @@ export default function (Vue, Router) {
175171
}
176172

177173
/**
178-
* Switch the current route to a new one.
174+
* Set current to the new transition.
179175
* This is called by the transition object when the
180176
* validation of a route has succeeded.
181177
*
182-
* @param {Route} route
178+
* @param {RouteTransition} transition
183179
*/
184180

185-
Router.prototype._updateRoute = function (route) {
186-
this._currentRoute = route
181+
Router.prototype._onTransitionValidated = function (transition) {
182+
// now that this one is validated, we can abort
183+
// the previous transition.
184+
let prevTransition = this._prevTransition
185+
if (prevTransition) {
186+
prevTransition.aborted = true
187+
}
188+
// set current route
189+
let route = this._currentRoute = transition.to
187190
// update route context for all children
188191
if (this.app.$route !== route) {
189192
this.app.$route = route

src/transition.js

+7-16
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ import {
44
canActivate, canDeactivate, canReuse
55
} from './pipeline'
66

7-
// avoid infinite redirect loops on error
8-
const MAX_ERROR_REDIRECTS = 10
9-
let errorCount = 0
10-
117
/**
128
* A RouteTransition object manages the pipeline of a
139
* router-view switching process. This is also the object
@@ -48,14 +44,15 @@ export default class RouteTransition {
4844

4945
/**
5046
* Abort current transition and return to previous location.
51-
*
52-
* @param {Boolean} back
5347
*/
5448

55-
abort (back) {
49+
abort () {
5650
if (!this.aborted) {
5751
this.aborted = true
58-
if (back !== false) {
52+
// if the root path throws an error during validation
53+
// on initial load, it gets caught in an infinite loop.
54+
let abortingOnLoad = !this.from.path && this.to.path === '/'
55+
if (!abortingOnLoad) {
5956
this.router.replace(this.from.path || '/')
6057
}
6158
}
@@ -133,7 +130,7 @@ export default class RouteTransition {
133130
// 3. Activation phase
134131

135132
// Update router current route
136-
transition.router._updateRoute(transition.to)
133+
transition.router._onTransitionValidated(transition)
137134

138135
// trigger reuse for all reused views
139136
reuseQueue && reuseQueue.forEach(function (view) {
@@ -213,16 +210,10 @@ export default class RouteTransition {
213210

214211
// handle errors
215212
let onError = (err) => {
216-
// prevent infinite error redirects
217-
errorCount++
218-
let canGoBack = errorCount < MAX_ERROR_REDIRECTS
219-
if (!canGoBack) {
220-
errorCount = 0
221-
}
222213
// cleanup indicates an after-activation hook,
223214
// so instead of aborting we just let the transition
224215
// finish.
225-
cleanup ? next() : abort(canGoBack)
216+
cleanup ? next() : abort()
226217
if (err && !transition.router._suppress) {
227218
warn('Uncaught error during transition: ')
228219
throw err instanceof Error ? err : new Error(err)

0 commit comments

Comments
 (0)