Renames a file with its checksum and can update template file content with the new name. Useful for for preventing browser to cache old .js or .css files.
npm install grunt-checksumrenametpl --save-dev
Once the plugin has been installed, it may be enabled inside your .Gruntfile
with this line of JavaScript:
grunt.loadNpmTasks('grunt-checksumrenametpl');
checksumrenametpl: {
js: {
srcFile: 'build/js/js.min.js',
destFileTpl: 'www/scripts/js-[CHECKSUM].min.js',
replaceFile: ['php/templates/main.tpl'],
replaceTpl: 'src="/scripts/js-[CHECKSUM].min.js"></script>',
}
- Calculate md5 checksum of
build/js/js.min.js
(let's say that checksum isad6466b2aaf7e9c9ba0494df9c813c13
). - Move
build/js/js.min.js
towww/scripts/js-ad6466b2aaf7e9c9ba0494df9c813c13.min.js
- In
php/templates/main.tpl
file, replace all occurrences of
src="/scripts/js-OLDCHECKSUM.min.js"></script>
with
src="/scripts/js-ad6466b2aaf7e9c9ba0494df9c813c13.min.js"></script>
OLDCHECKSUM here can be any md5 checksum which will be replaced with the new one.
Note that the file name in the HTML page must contain some checksum that will be updated. For the first time you run this task you can put any md5 checksum into the HTML. - Delete all files with an invalid checksum from
www/scripts/
directory.
-
srcFile
- File to be renamed. -
destFileTpl
- Template string which define a new name for thesrcFile
.[CHECKSUM]
will be replaced with md5 hash of the file. -
replaceFile
- Files which content needs to be updated with the new file name. You can specify more files here and use grunt globing patterns. -
replaceTpl
- Template string which define a partition ofreplaceFile
content in which[CHECKSUM]
will be replaced with md5 hash of the file. This can be any string which contains[CHECKSUM]
.