Skip to content

Instantly share code, notes, and snippets.

@amcdnl
Created September 10, 2012 17:13
Show Gist options
  • Save amcdnl/3692224 to your computer and use it in GitHub Desktop.
Save amcdnl/3692224 to your computer and use it in GitHub Desktop.
Steal and Cache Busting

Steal and Cache Busting

When using steal in production, you sometimes want to 'cache bust' your images in your CSS. Below is a script that you can add to your build script to accomplish this.

var window = (function() {
	return this;
}).call(null, 0);
	
var modifyCss = function(css, str) {
	str = str || Date.now().toString();
	
	css = css.replace(/url\(['"]*([^"')]*)['"]*\)/g, function(match) {
	  return match.substring(0, match.length - 1) + '?v=' + str + ')';
	});

	return css;
},
fingerprint = function(path, str) {
	var text = modifyCss( readFile(path), str );
	steal.File(path).save(text);
}

var dir = new java.io.File('packages'),
	files = (dir.isDirectory() && dir.listFiles().slice(0)) || [],
	file, i;
if (dir) {
	files.unshift('production.css');
	for (i = 0; i < files.length && (file = files[i]); i++) {
		if (/\.css$/.test(file)) {
			steal.print('Fingerprinting ' + file);
			fingerprint(file);
		}
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment