In my js files I have references to HTML files, like window.location. I would like grunt cache bust to update that reference and add the hash data, so the loaded page is the
I can't find any configuration of the cache bust that allows me to write within the js file
...I also couldn't get it to do that.
Finally, I opted for a custom grunt solution to achieve this. This entailed:
$ npm install randomstring --save-dev
options.hash in my cacheBust task..js file/s for '.html' and replacing any instances found with the newly generated random string plus '.html'. E.g. '.a5G5p7QdOE6DF1St4k.html'.$ npm install grunt-text-replace --save-dev
module.exports = function(grunt) {
var randomstring = require("randomstring");
grunt.initConfig({
randomString: randomstring.generate(),
cacheBust: {
myTarget: {
options: {
// <-- Your options here
hash: '<%= randomString %>' //<-- This template references the random generated string.
},
src: [/* Your settings here */]
}
},
replace: {
js: {
src: './src/**/*.js',
dest: './dist/', //<-- creates a copy
replacements: [{
from: /\.html'/, // matches all instances of .html'
to: '.<%= randomString %>.html\'' //<-- Note the dot separator at the start.
}]
}
}
});
require('load-grunt-tasks')(grunt);
grunt.registerTask('myCacheBust', ['cacheBust:myTarget', 'replace:js']);
grunt.registerTask('default', ['myCacheBust']);
};
Notes:
$ npm install load-grunt-tasks --save-dev
replace:js task searches for all instances of the characters .html' in the .js files.randomstring.generate(7)