Skip to content

Commit 193222e

Browse files
committed
[added] StaticLocation, for server-side rendering
1 parent e05e229 commit 193222e

File tree

3 files changed

+32
-22
lines changed

3 files changed

+32
-22
lines changed

modules/createRouter.js

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var ImitateBrowserBehavior = require('./behaviors/ImitateBrowserBehavior');
88
var HashLocation = require('./locations/HashLocation');
99
var HistoryLocation = require('./locations/HistoryLocation');
1010
var RefreshLocation = require('./locations/RefreshLocation');
11+
var StaticLocation = require('./locations/StaticLocation');
1112
var NavigationContext = require('./NavigationContext');
1213
var StateContext = require('./StateContext');
1314
var Scrolling = require('./Scrolling');
@@ -139,6 +140,8 @@ function createRouter(options) {
139140
'You should not use a static location in a DOM environment because ' +
140141
'the router will not be kept in sync with the current URL'
141142
);
143+
144+
location = new StaticLocation(location);
142145
} else {
143146
invariant(
144147
canUseDOM || location.needsDOM === false,
@@ -240,11 +243,6 @@ function createRouter(options) {
240243
* a new URL onto the history stack.
241244
*/
242245
transitionTo: function (to, params, query) {
243-
invariant(
244-
typeof location !== 'string',
245-
'You cannot use transitionTo with a static location'
246-
);
247-
248246
var path = this.makePath(to, params, query);
249247

250248
if (pendingTransition) {
@@ -260,11 +258,6 @@ function createRouter(options) {
260258
* the current URL in the history stack.
261259
*/
262260
replaceWith: function (to, params, query) {
263-
invariant(
264-
typeof location !== 'string',
265-
'You cannot use replaceWith with a static location'
266-
);
267-
268261
location.replace(this.makePath(to, params, query));
269262
},
270263

@@ -280,11 +273,6 @@ function createRouter(options) {
280273
* because we cannot reliably track history length.
281274
*/
282275
goBack: function () {
283-
invariant(
284-
typeof location !== 'string',
285-
'You cannot use goBack with a static location'
286-
);
287-
288276
if (History.length > 1 || location === RefreshLocation) {
289277
location.pop();
290278
return true;
@@ -296,7 +284,7 @@ function createRouter(options) {
296284
},
297285

298286
handleAbort: options.onAbort || function (abortReason) {
299-
if (typeof location === 'string')
287+
if (location instanceof StaticLocation)
300288
throw new Error('Unhandled aborted transition! Reason: ' + abortReason);
301289

302290
if (abortReason instanceof Cancellation) {
@@ -431,17 +419,15 @@ function createRouter(options) {
431419
}
432420
};
433421

434-
if (typeof location === 'string') {
435-
Router.dispatch(location, null);
436-
} else {
422+
if (!(location instanceof StaticLocation)) {
437423
if (location.addChangeListener)
438424
location.addChangeListener(Router.handleLocationChange);
439425

440426
this.isRunning = true;
441-
442-
// Bootstrap using the current path.
443-
this.refresh();
444427
}
428+
429+
// Bootstrap using the current path.
430+
this.refresh();
445431
},
446432

447433
refresh: function () {

modules/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ exports.RouteHandler = require('./components/RouteHandler');
88
exports.HashLocation = require('./locations/HashLocation');
99
exports.HistoryLocation = require('./locations/HistoryLocation');
1010
exports.RefreshLocation = require('./locations/RefreshLocation');
11+
exports.StaticLocation = require('./locations/StaticLocation');
1112

1213
exports.ImitateBrowserBehavior = require('./behaviors/ImitateBrowserBehavior');
1314
exports.ScrollToTopBehavior = require('./behaviors/ScrollToTopBehavior');
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var invariant = require('react/lib/invariant');
2+
3+
function throwCannotModify() {
4+
invariant(false, 'You cannot modify a static location');
5+
}
6+
7+
function StaticLocation(path) {
8+
this.path = path;
9+
}
10+
11+
StaticLocation.prototype.push = throwCannotModify;
12+
StaticLocation.prototype.replace = throwCannotModify;
13+
StaticLocation.prototype.pop = throwCannotModify;
14+
15+
StaticLocation.prototype.getCurrentPath = function () {
16+
return this.path;
17+
};
18+
19+
StaticLocation.prototype.toString = function () {
20+
return '<StaticLocation path="' + this.path + '">';
21+
};
22+
23+
module.exports = StaticLocation;

0 commit comments

Comments
 (0)