Skip to content

Fine grained URL control

jrburke edited this page Aug 8, 2012 · 4 revisions

Sometimes you may want to control the URLs used to fetch modules in a way that cannot be done using the normal baseUrl, paths, map and/or urlArgs config.

You can override requirejs.load() to do this work. Save off a reference to the original requirejs.load implementation, and then modify the URL as you want before delegating to the original implementation:

<script src="require.js"></script>
<script>
(function () {
  var load = requirejs.load;
  requirejs.load = function (context, moduleId, url) {
    //modify url here, then call original load
    return load(context, moduleId, url);
  };

  //Now load code.
  require(['main']);
}());
</script>

NOTE 1: Loader plugin resources do not go through requirejs.load(), so this approach will not work if you want to catch those resources and associated URLs.

NOTE 2: requirejs.load() is a semi-private API meaning it can change at any time. Traditionally it has been fairly stable, but by modifying it, you are taking on the responsibility of updating your modification in later requirejs releases. There are no guarantees it will stay the same in the future.