Skip to content

Commit

Permalink
Fixes requirejs#1481, allow urlArgs to be a function
Browse files Browse the repository at this point in the history
  • Loading branch information
jrburke committed Mar 13, 2016
1 parent 684128f commit 39e682b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
14 changes: 14 additions & 0 deletions docs/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,20 @@ <h2>
<pre><code>urlArgs: "bust=" + (new Date()).getTime()
</code></pre>

<p>As of RequireJS 2.1.23, urlArgs can be a function. If a function, it will receive the module ID and the URL as parameters, and it should return a string that will be added to the end of the URL. Return an empty string if no args. Be sure to take care of adding the '?' or '&' depending on the existing state of the URL. Example:</p>

<pre><code>requirejs.config({
urlArgs: function(id, url) {
var args = 'v=1';
if (url.indexOf('view.html') !== -1) {
args = 'v=2'
}

return (url.indexOf('?') === -1 ? '?' : '&') + args;
}
});
</code></pre>

<p>During development it can be useful to use this, however <strong>be sure</strong> to remove it before deploying your code.</p>

<p id="config-scriptType"><strong><a href="#config-baseUrl">scriptType</a></strong>: Specify the value for the type="" attribute used for script tags inserted into the document by RequireJS. Default is "text/javascript". To use Firefox's JavaScript 1.8 features, use "text/javascript;version=1.8".</p>
Expand Down
13 changes: 10 additions & 3 deletions require.js
Original file line number Diff line number Diff line change
Expand Up @@ -1291,6 +1291,14 @@ var requirejs, require, define;
}
}

// Convert old style urlArgs string to a function.
if (typeof cfg.urlArgs === 'string') {
var urlArgs = cfg.urlArgs;
cfg.urlArgs = function(id, url) {
return (url.indexOf('?') === -1 ? '?' : '&') + urlArgs;
};
}

//Save off the paths since they require special processing,
//they are additive.
var shim = config.shim,
Expand Down Expand Up @@ -1671,9 +1679,8 @@ var requirejs, require, define;
url = (url.charAt(0) === '/' || url.match(/^[\w\+\.\-]+:/) ? '' : config.baseUrl) + url;
}

return config.urlArgs ? url +
((url.indexOf('?') === -1 ? '?' : '&') +
config.urlArgs) : url;
return config.urlArgs ? url + config.urlArgs(moduleName, url) :
url;
},

//Delegates to req.load. Broken out as a separate function to
Expand Down
1 change: 1 addition & 0 deletions tests/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ doh.registerUrl("baseUrl", "../baseUrl.html");

doh.registerUrl("toUrl", "../toUrl/toUrl.html");
doh.registerUrl("urlArgsToUrl", "../urlArgsToUrl.html");
doh.registerUrl("urlArgsToUrlFunction", "../urlArgsToUrlFunction.html");

doh.registerUrl("config", "../config.html");
doh.registerUrl("configRequirejs", "../configRequirejs.html");
Expand Down
39 changes: 39 additions & 0 deletions tests/urlArgsToUrlFunction.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<html>
<head>
<title>require.js: urlArgs Function + toUrl Test</title>
<script type="text/javascript" src="doh/runner.js"></script>
<script type="text/javascript" src="doh/_browserRunner.js"></script>
<script type="text/javascript" src="../require.js"></script>
<script>
require.config({
baseUrl: 'js',
urlArgs: function(id, url) {
var args = 'v=1';
if (url.indexOf('view.html') !== -1) {
args = 'v=2'
}

return (url.indexOf('?') === -1 ? '?' : '&') + args;
}
});

doh.register(
'urlArgsToUrlFunction',
[
function urlArgsToUrlFunction(t){
t.is('js/view.html?v=2', require.toUrl('view.html'));
t.is('js/view?v=1', require.toUrl('view'));
}
]
);
doh.run();
</script>
</head>
<body>
<h1>require.js: urlArgs Function + toUrl Test</h1>
<p>Makes sure urlArgs can be a function.
<a href="https://github.com/requirejs/requirejs/issues/1481">More info</a>.</p>
<p>Check console for messages.</p>
</body>
</html>

0 comments on commit 39e682b

Please sign in to comment.