Skip to content

Commit

Permalink
Support routing to start below a subdirectory on the server
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielDornhardt committed Aug 29, 2014
1 parent 7a402e4 commit 74e5665
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/client/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ IronRouter = Utils.extend(IronRouter, {

// after the dispatch is complete, set the IronLocation
// path and state which will update the browser's url.

// NOTE / SNEAKY: "path" gets updated later on in this function, *after* checking for it's route, to prefix the URL
// if the app is running in a subdirectory on the server
done = function() {
options = options || {};
self._location.set(path, {
Expand Down Expand Up @@ -205,6 +208,8 @@ IronRouter = Utils.extend(IronRouter, {
Utils.assert(route, 'No route found named ' + routeNameOrPath);
path = route.path(params, options);
controller = route.newController(path, options);
// update path to contain the subdirectory if the app is running in a subdirectory of the domain
path = __meteor_runtime_config__.ROOT_URL_PATH_PREFIX + path;
self.run(controller, done);
}
},
Expand Down
16 changes: 14 additions & 2 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,21 +254,33 @@ IronRouter.prototype = {
var route = this.routes[routeName];
Utils.warn(route,
'You called Router.path for a route named ' + routeName + ' but that route doesn\'t seem to exist. Are you sure you created it?');
return route && route.path(params, options);
return route && (__meteor_runtime_config__.ROOT_URL_PATH_PREFIX + route.path(params, options));
},

url: function (routeName, params, options) {
var route = this.routes[routeName];
Utils.warn(route,
'You called Router.url for a route named "' + routeName + '" but that route doesn\'t seem to exist. Are you sure you created it?');
return route && route.url(params, options);
return route && (__meteor_runtime_config__.ROOT_URL_PATH_PREFIX + route.url(params, options));
},

match: function (path) {
return _.find(this.routes, function(r) { return r.test(path); });
},

dispatch: function (path, options, cb) {
// check if the project is configured to run in a sub-path of a domain, eg. http://www.xyz.com/app1/
// if this is configured via the meteor environment variable ROOT_PATH (eg. ROOT_PATH='http://www.xyz.com/app1'), then
// __meteor_runtime_config__.ROOT_URL_PATH_PREFIX will be /app1 . On the client we need to remove that prefix from the path
// to still match the routes, but now starting below eg. /app1/firstRoute .
//
// this seems only to be required on the client. On the server side the URL seems to get cleaned by some other middleware beforhand?
var rootUrlPathPrefix = __meteor_runtime_config__.ROOT_URL_PATH_PREFIX;
if (Meteor.isClient && rootUrlPathPrefix) {
Utils.assert(path.substring(0, rootUrlPathPrefix.length) === rootUrlPathPrefix, "Unknown path prefix, expected: '" + rootUrlPathPrefix + "'");
path = path.substring(rootUrlPathPrefix.length);
}

var route = this.match(path);

if (! route)
Expand Down

0 comments on commit 74e5665

Please sign in to comment.