Skip to content

Commit

Permalink
[added] StaticLocation, for server-side rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
mjackson committed Feb 13, 2015
1 parent e05e229 commit 193222e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
30 changes: 8 additions & 22 deletions modules/createRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var ImitateBrowserBehavior = require('./behaviors/ImitateBrowserBehavior');
var HashLocation = require('./locations/HashLocation');
var HistoryLocation = require('./locations/HistoryLocation');
var RefreshLocation = require('./locations/RefreshLocation');
var StaticLocation = require('./locations/StaticLocation');
var NavigationContext = require('./NavigationContext');
var StateContext = require('./StateContext');
var Scrolling = require('./Scrolling');
Expand Down Expand Up @@ -139,6 +140,8 @@ function createRouter(options) {
'You should not use a static location in a DOM environment because ' +
'the router will not be kept in sync with the current URL'
);

location = new StaticLocation(location);
} else {
invariant(
canUseDOM || location.needsDOM === false,
Expand Down Expand Up @@ -240,11 +243,6 @@ function createRouter(options) {
* a new URL onto the history stack.
*/
transitionTo: function (to, params, query) {
invariant(
typeof location !== 'string',
'You cannot use transitionTo with a static location'
);

var path = this.makePath(to, params, query);

if (pendingTransition) {
Expand All @@ -260,11 +258,6 @@ function createRouter(options) {
* the current URL in the history stack.
*/
replaceWith: function (to, params, query) {
invariant(
typeof location !== 'string',
'You cannot use replaceWith with a static location'
);

location.replace(this.makePath(to, params, query));
},

Expand All @@ -280,11 +273,6 @@ function createRouter(options) {
* because we cannot reliably track history length.
*/
goBack: function () {
invariant(
typeof location !== 'string',
'You cannot use goBack with a static location'
);

if (History.length > 1 || location === RefreshLocation) {
location.pop();
return true;
Expand All @@ -296,7 +284,7 @@ function createRouter(options) {
},

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

if (abortReason instanceof Cancellation) {
Expand Down Expand Up @@ -431,17 +419,15 @@ function createRouter(options) {
}
};

if (typeof location === 'string') {
Router.dispatch(location, null);
} else {
if (!(location instanceof StaticLocation)) {
if (location.addChangeListener)
location.addChangeListener(Router.handleLocationChange);

this.isRunning = true;

// Bootstrap using the current path.
this.refresh();
}

// Bootstrap using the current path.
this.refresh();
},

refresh: function () {
Expand Down
1 change: 1 addition & 0 deletions modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ exports.RouteHandler = require('./components/RouteHandler');
exports.HashLocation = require('./locations/HashLocation');
exports.HistoryLocation = require('./locations/HistoryLocation');
exports.RefreshLocation = require('./locations/RefreshLocation');
exports.StaticLocation = require('./locations/StaticLocation');

exports.ImitateBrowserBehavior = require('./behaviors/ImitateBrowserBehavior');
exports.ScrollToTopBehavior = require('./behaviors/ScrollToTopBehavior');
Expand Down
23 changes: 23 additions & 0 deletions modules/locations/StaticLocation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var invariant = require('react/lib/invariant');

function throwCannotModify() {
invariant(false, 'You cannot modify a static location');
}

function StaticLocation(path) {
this.path = path;
}

StaticLocation.prototype.push = throwCannotModify;
StaticLocation.prototype.replace = throwCannotModify;
StaticLocation.prototype.pop = throwCannotModify;

StaticLocation.prototype.getCurrentPath = function () {
return this.path;
};

StaticLocation.prototype.toString = function () {
return '<StaticLocation path="' + this.path + '">';
};

module.exports = StaticLocation;

0 comments on commit 193222e

Please sign in to comment.