This question has been asked several times but not specific to this example.
Here is an overview of our application:
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:
Could the solution:
RequireJS is a JavaScript library and file loader which manages the dependencies between JavaScript files and in modular programming. It also helps to improve the speed and quality of the code.
Cache busting is a way to prevent browsers from caching your ad content. Cache busting adds a random string to your ad tag each time the tag fires — typically a random number. Because the tag has a different number each time, the browser sends a new request each time.
RequireJs is basically a JavaScript for the specific module. RequireJs use AMD Standards. RequireJs don't allow to run global JavaScript. If you have to use JavaScript then add into RequireJS configuration file. RequireJs share code and data between modules and programs.
The 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'
},
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With