admin管理员组文章数量:1384663
This question has been asked several times but not specific to this example.
Here is an overview of our application:
- Simple server routing with Express on Node
- Single page backbone application
- Core modules and libraries (JS/CSS) that don't change
- Widget JS/LESS/HTML files that frequently change
During development, I am looking to cache bust files that change but not those core libraries to speed up my page reloads and quicken my development.
I have found explanations on:
- Cache Busting on RequireJS - here
- Explanation on Cache Busting Specific Modules - here
Could the solution:
- Explain how the requireJS caching busting works?
- Provide a solution for this specific scenario.
This question has been asked several times but not specific to this example.
Here is an overview of our application:
- Simple server routing with Express on Node
- Single page backbone application
- Core modules and libraries (JS/CSS) that don't change
- Widget JS/LESS/HTML files that frequently change
During development, I am looking to cache bust files that change but not those core libraries to speed up my page reloads and quicken my development.
I have found explanations on:
- Cache Busting on RequireJS - here
- Explanation on Cache Busting Specific Modules - here
Could the solution:
- Explain how the requireJS caching busting works?
- Provide a solution for this specific scenario.
2 Answers
Reset to default 4The cache-busting works by appending an always-unique query string to the end of every file which is required. It makes use of RequireJS's urlArgs
config value; RequireJS takes care of appending it for you:
urlArgs: "bust=" + (new Date()).getTime()
The(new Date()).getTime()
part is just a simple way to get a unique string out of JavaScript. You could do some variation on Math.random()
, but using the number of milliseconds since the epoch guarantees uniqueness, for optimum cache-bustage.
I think Mr Burke is suggesting something like:
require.config({
baseUrl: '/base/path',
paths: {
'fileAlias': 'fileLikelyToChange?bust=' + (new Date()).getTime(),
'anotherFileAlias': 'anotherFileLikelyToChange?bust=' + (new Date()).getTime(),
'jQuery': 'jQuery'
},
});
So, instead of the ubiquitous urlArgs
cache-busting, you apply it specifically to each file which is likely to change; hence, excluding any libraries.
I haven't tested it, but I'd probably tidy it up to something like:
function bust(path) {
return path + '?bust=' + (new Date()).getTime();
}
require.config({
baseUrl: '/base/path',
paths: {
'fileAlias': bust('fileLikelyToChange'),
'anotherFileAlias': bust('anotherFileLikelyToChange'),
'jQuery': 'jQuery'
},
});
Just remember that if you really need to rely on some external script that you can use $.getScript instead of require to ensure that it's included. I have some non-amd scripts that are for 3rd party integration (e.g. amazon payments), which I used getScript for instead of require. If you can use this method, it would avoid sending the cache busting parameters from urlArgs to the external server.
本文标签: javascriptCache busting specific modules with RequireJSStack Overflow
版权声明:本文标题:javascript - Cache busting specific modules with RequireJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744516760a2610210.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论