最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - URL cache-busting parameters with RequireJS? - Stack Overflow

programmeradmin2浏览0评论

I'm using RequireJS (the jQuery version) and I want to append GET parameters to my scripts to prevent unwanted caching.

I'm using the urlArgs parameter, as suggested in the docs. This is my app-build.js file:

({
  appDir: "../",
  baseUrl: "scripts/",
  urlArgs: "cache=v2",
  ...

Then I build the project as follows:

$ node ../../r.js -o app.build.js

The output in app-build directory now contains both require-jquery.js, which is the same file as previously, and require-jquery.js?cache=v2, which is blank.

The index.html file doesn't seem to have any references to cache=v2. And when I load the page in a browser, I don't see any cache=v2 parameters appended to any of the scripts.

Am I doing something wrong?

I'm using RequireJS (the jQuery version) and I want to append GET parameters to my scripts to prevent unwanted caching.

I'm using the urlArgs parameter, as suggested in the docs. This is my app-build.js file:

({
  appDir: "../",
  baseUrl: "scripts/",
  urlArgs: "cache=v2",
  ...

Then I build the project as follows:

$ node ../../r.js -o app.build.js

The output in app-build directory now contains both require-jquery.js, which is the same file as previously, and require-jquery.js?cache=v2, which is blank.

The index.html file doesn't seem to have any references to cache=v2. And when I load the page in a browser, I don't see any cache=v2 parameters appended to any of the scripts.

Am I doing something wrong?

Share Improve this question edited Feb 15, 2013 at 1:16 madth3 7,34412 gold badges52 silver badges74 bronze badges asked Mar 8, 2012 at 12:55 RichardRichard 33k30 gold badges111 silver badges146 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 1

The docs on urlArgs:

“During development it can be useful to use this, however be sure to remove it before deploying your code”

and this issue from Github, James Burke: “do not try to use urlArgs during build”

The urlArgs parameter is more of a runtime configuration (i.e., only understood by RequireJS, not the r.js optimizer), seemingly due to its author's stated belief that it is only suited to development (and "bad" dev servers that don't send proper headers). So you'd either need to configure it in your require.config call (in a .js file loaded by require.js, typically main.js or config.js):

require.config({
    // other config, like paths and shim

    urlArgs: "cache=v2"
});

Or, per this other SO answer, you'd define it in directly in a <script> block before loading require.js.

I would try using a different build.js file for the optimizer vs the build.js file you use running the live app. Based on your description, the optimizer script doesn't seem to properly handle the urlArgs parameter (since it's outputting a file called require-jquery.js?cache=v2).

I wouldn't expect cache=v2 to show up in index.html (why would it?), but you're right to expect it in the network activity log.

发布评论

评论列表(0)

  1. 暂无评论