Skip to content

Commit 91d4380

Browse files
committed
[fixed] Abort pending transition on user navigation
Previously, navigating (by clicking Back button or a link) while a transition in progress would put router into an inconsistent state. Now, a pending transition will be aborted and ignored by the router if user navigates away while it was pending. Fixes #479
1 parent 64f0f17 commit 91d4380

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

modules/utils/Cancellation.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Represents a cancellation caused by navigating away
3+
* before the previous transition has fully resolved.
4+
*/
5+
function Cancellation() { }
6+
7+
module.exports = Cancellation;

modules/utils/Transition.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ function Transition(path, retry) {
9898
assign(Transition.prototype, {
9999

100100
abort: function (reason) {
101+
if (this.isAborted) {
102+
// First abort wins.
103+
return;
104+
}
105+
101106
this.abortReason = reason;
102107
this.isAborted = true;
103108
},

modules/utils/createRouter.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var supportsHistory = require('./supportsHistory');
1616
var Transition = require('./Transition');
1717
var PropTypes = require('./PropTypes');
1818
var Redirect = require('./Redirect');
19+
var Cancellation = require('./Cancellation');
1920
var Path = require('./Path');
2021

2122
/**
@@ -43,7 +44,9 @@ function defaultAbortHandler(abortReason, location) {
4344
if (typeof location === 'string')
4445
throw new Error('Unhandled aborted transition! Reason: ' + abortReason);
4546

46-
if (abortReason instanceof Redirect) {
47+
if (abortReason instanceof Cancellation) {
48+
return;
49+
} else if (abortReason instanceof Redirect) {
4750
location.replace(this.makePath(abortReason.to, abortReason.params, abortReason.query));
4851
} else {
4952
location.pop();
@@ -141,6 +144,7 @@ function createRouter(options) {
141144
var onAbort = options.onAbort || defaultAbortHandler;
142145
var state = {};
143146
var nextState = {};
147+
var pendingTransition = null;
144148

145149
function updateState() {
146150
state = nextState;
@@ -212,7 +216,14 @@ function createRouter(options) {
212216
'You cannot use transitionTo with a static location'
213217
);
214218

215-
location.push(this.makePath(to, params, query));
219+
var path = this.makePath(to, params, query);
220+
221+
if (pendingTransition) {
222+
// Replace so pending location does not stay in history.
223+
location.replace(path);
224+
} else {
225+
location.push(path);
226+
}
216227
},
217228

218229
/**
@@ -265,6 +276,11 @@ function createRouter(options) {
265276
* hooks wait, the transition is fully synchronous.
266277
*/
267278
dispatch: function (path, action, callback) {
279+
if (pendingTransition) {
280+
pendingTransition.abort(new Cancellation());
281+
pendingTransition = null;
282+
}
283+
268284
var prevPath = state.path;
269285
if (prevPath === path)
270286
return; // Nothing to do!
@@ -306,6 +322,7 @@ function createRouter(options) {
306322
}
307323

308324
var transition = new Transition(path, this.replaceWith.bind(this, path));
325+
pendingTransition = transition;
309326

310327
transition.from(fromRoutes, components, function (error) {
311328
if (error || transition.isAborted)
@@ -335,6 +352,8 @@ function createRouter(options) {
335352
*/
336353
run: function (callback) {
337354
function dispatchHandler(error, transition) {
355+
pendingTransition = null;
356+
338357
if (error) {
339358
onError.call(router, error);
340359
} else if (transition.isAborted) {

0 commit comments

Comments
 (0)