Skip to content

Instantly share code, notes, and snippets.

@kyleguate
Last active May 28, 2016 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyleguate/7445c580661d3f62b01c50b7d637f539 to your computer and use it in GitHub Desktop.
Save kyleguate/7445c580661d3f62b01c50b7d637f539 to your computer and use it in GitHub Desktop.
Gulp task for cache busting angular template files
/*
PURPOSE
The purpose of this gist is to show how you can dynamically bust the cache of your angular template files only when the files have been changed.
PREREQUISITES:
1) You use Gulp to build/organize your front end application
2) In your front end build process you have already run the 'app:scripts' and 'app:templates' tasks:
'app:scripts' - This takes all of your separate app js files and combines them into one file to deliver to the browser.
'app:templates' - If you are using Jade or another html templating library/framework then this task would convert those files into browser-readable html files
I specify these two tasks as dependencies to the cache busting task defined below. They can easily be modified or omitted to match your specific build process
3) Install the node modules listed below with npm
HOW IT WORKS
1) Given a glob pattern or array of files/patterns for all template files this script will hash the file contents of each one
2) The app JS file is then parsed and each occurence of a template file has the file hash appended to it with a query string.
This means you keep your template filenames clean and IDE-friendly and you hide the caching complexity in your dist app JS file.
THINGS TO CHECK:
1) I need to test with minifying
REFERENCES
https://www.npmjs.com/package/gulp-cache-bust
http://meta.stackexchange.com/questions/112182/how-does-se-determine-the-css-and-js-version-parameter
*/
/* Node modules you need to install with npm */
var gulp = require('gulp'),
crypto = require('crypto'),
tap = require('gulp-tap'),
replaceBatch = require('gulp-batch-replace'),
async = require('async');
gulp.task('angularCacheBuster', ['app:scripts', 'app:templates'], function() {
var hashedFilenames = [];
// Change these to match your filepaths
var templatePathGlob = 'path/to/modules/**/*.jade',
relativeTemplatePathPattern = 'relative/path/to/your/templates',
pathToAppJSDir = 'path/to/app/js/dir',
pathToAppJSFile = pathToAppJSDir + '/appJSFilename.js';
return async.series([
hashAppTemplateFilenames,
appendHashesToFilenamesInAppScript
]);
function hashAppTemplateFilenames(next) {
gulp.src(templatePathGlob)
.pipe(tap(function(file, t) {
oldFilePath = file.path.substr(file.path.indexOf(relativeTemplatePathPattern));
newFilePath = oldFilePath + '?h=' + checksum(file.contents);
hashedFilenames.push([oldFilePath, newFilePath]);
}))
.on('end', next);
}
function appendHashesToFilenamesInAppScript(next) {
gulp.src(pathToAppJSFile)
.pipe(replaceBatch(hashedFilenames))
.pipe(gulp.dest(pathToAppJSDir));
}
});
function checksum(str, algorithm, encoding) {
return crypto
.createHash(algorithm || 'md5')
.update(str, 'utf8')
.digest(encoding || 'hex')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment