Skip to content

Instantly share code, notes, and snippets.

@davidworkman9
Last active August 29, 2015 14:07
Show Gist options
  • Save davidworkman9/4f52c8029a7b3d3fbebb to your computer and use it in GitHub Desktop.
Save davidworkman9/4f52c8029a7b3d3fbebb to your computer and use it in GitHub Desktop.
Drop in support for subpaths in iron-router.
(function () {
var url = Meteor.absoluteUrl();
url = url.replace(/https?:\/\//, '');
var slash = url.indexOf('/');
var subpath = url.substring(slash);
subpath = subpath.substring(0, subpath.length-1);
var oldRoute = Router.route;
Router.route = function (name, options) {
arguments[1].path = subpath + options.path;
return oldRoute.apply(this, arguments);
};
var oldGo = Router.go;
Router.go = function (route) {
var regex = new RegExp('^' + escapeRegex(subpath));
if (route.indexOf('/') > -1 && !regex.test(route)) {
arguments[0] = subpath + route;
}
return oldGo.apply(this, arguments);
};
function escapeRegex(str) {
return str.replace(/([/\\.?*()^${}|[\]])/g, '\\$1');
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment