Requirement
- I want to maximise the use of caching for JS and CSS files (with long expiry periods set in
.htacess
), but if a file is updated, the newer file should be requested instead of the cached version - I use requireJS to conditionally load modules - these modules require cache busting, but CDN hosted library resources like jquery does not.
- I prefer to keep filenames the same for different builds. My reasoning is that an older version of a revved file (eg. script.123.js instead of script.js) would be deleted when there is a new build (eg. script.124.js), and if there are any cached references somewhere pointing to the older version, the server would return 404. Perhaps this fear is unfounded if .htacess is set up to never cache .html files?
Findings
I found a few half solutions, but unfortunately not one elegant solution that fits all my requirements1. Use RequireJS urlArgs to cache-bust JS files
In requireJS config, append a url parameter withurlArgs
to each URL that requireJS loads. I use @@hashcache
as a placeholder for the build hash, generates the hash in my Gruntfile and then uses grunt-replace to replace it with a generated hash.Good:
- Cache-busts all requireJS conditionally loaded modules
- urlArgs is not recommended for production
- All scripts loaded will have this urlArgs appended and it will cache-bust CDN resources. This means the caching benefit is lost for a widely used CDN-loaded resource like jquery
- Another solution is required for cache-busting CSS files, and also the first file requireJS loads via
data-main=""
inindex.html
2. Rev file references and modify rewrite rules
I took notes from here and here for a solution that revs file references but keep actual filenames unchanged.
1. Use grunt-hashres to rev and replace references to JS & CSS files, but don't rename actual files
Index.html references revved CSS & JS files: 2. Update
.htaccess
rewrite rule to point revved to original files
Good:
- hash is generated for each file - if a file isn't changed, its hash isn't updated - maximises the advantage of caching
- Does not rev JS modules loaded by RequireJS
- I have a feeling my rewrite rule may be a tad too broad? Could this accidentally effect other files?
3. Cache-busting requireJS modules
The above solution is able to cache-bust anything except for conditionally loaded requireJS modules not referenced in index.html. For each of these modules, I need to:1. Configure module path in requireJS config so it'll load a revved file path, with the original path as backup
2. Configure module path in Gruntfile so the optimiser knows where to look for the module
3. Use grunt-replace to replace references to @@cachehash with a hash for each build
Good:
- cache-busts without using urlArgs; Only busts files that require it, and not CDN files
- require fiddly manual configuration for each conditionally-loaded file
- Same hash is used for all files per build. So even if a file hasn't updated since last acess, it will still be reloaded. Not a big issue but not as cache-optimised as the previous solution.
I found solution for hashres in requireJs module. It based on method onModuleBundleComplete which invoked for each bundle after it created.
ReplyDeleteMethods for calc hash copied from hashres.
requirejs: {
compile: {
options: {
...
onModuleBundleComplete: function (data) {
var crypto = require('crypto'),
fs = require('fs');
var utils = require("./node_modules/grunt-hashres/tasks/hashresUtils");
var dest = [
'build/**/*.js-temp' // Bundle temporary file
];
var hsh = utils.md5("build/" + data.path).slice(0, 8);
var bundleName = data.name;
var bundleNewName = data.name + "." + hsh + ".cache";
// Substituting references to the given files with the hashed ones.
grunt.file.expand(dest).forEach(function (f) {
var destContents = fs.readFileSync(f, 'utf8');
destContents = destContents.replace(new RegExp(utils.preg_quote(bundleName) + "(\\?[0-9a-z]+)?", "g"), bundleNewName);
fs.writeFileSync(f, destContents, 'utf8');
});
fs.renameSync("build/" + data.path, "build/" + bundleNewName + ".js");
},
Casino Online: What is the Best Casino Online in India?
ReplyDeleteCasino online is one of the top casinosites.one types of gambling casino-roll.com games to herzamanindir enjoy. If you https://access777.com/ enjoy casino games, we can help you to win 출장샵 big.
If you go 먹튀프렌즈 down the route of using a credit card, the minimal transaction kind of|is sort of} hefty at $45. It drops down to only $20 should you go for one of many crypto choices. People who love a prestigious and professional color scheme will take pleasure in what MYB Casino brings to the desk. The black and gold design gives an opulent feeling to customers. The navigation system is superb, so you don’t have to worry about having bother finding what would possibly be} looking for.
ReplyDeleteSuch attributes have led to the widespread demand for these machines in casinos. AMATIC Industries GmbH - The company presents CX 30 upright slot machines for video games. Nor 배당사이트 is it any less a slot machine as a result of|as a end result of} other than its use or adaptability as such it may additionally sell or deliver something of value on the premise aside from likelihood. In recent years, slot machine designers have worked intently with video game architects to be able to} convey a number of the} visible enchantment found in digital gaming to the playing area. Video screens, 3D graphics, and group competitors are all making their means into slot development .
ReplyDeleteIt provides a huge online on line casino platform for the users to get pleasure from on. Skill-based games are games that include half in} of two gamers in opposition to one another. Some of the commonest online on line casino games that you could play listed here are|listed below are} Poker, Yahtzee, Chess, Backgammon, Rummy, Tonk and Spades. Blackjack has one of the best return to participant rates of any other on line casino game, providing a lot as} 99%. There are 카지노 also lots of totally different variations available, together with multi-hand choices and versions that add side bets.
ReplyDelete