Skip to content

Instantly share code, notes, and snippets.

@stinoga
Last active December 20, 2015 10:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stinoga/6119015 to your computer and use it in GitHub Desktop.
Save stinoga/6119015 to your computer and use it in GitHub Desktop.
Busting cache with Grunt and Apache.
# To understand why this is important and a better idea than `*.css?v231`, read:
# http://stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.(\d+)\.(js|css|png|jpg|gif)$ $1.$3 [L]
</IfModule>
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
// Replace object
replace: {
build_replace: {
options: {
variables: {
// Generate a truly random number by concatenating the current date with a random number
// The variable name corresponds with the same in our HTML file
'hash': '<%= ((new Date()).valueOf().toString()) + (Math.floor((Math.random()*1000000)+1).toString()) %>'
}
},
// Source and destination files
files: [
{
src: ['public/index.html'],
dest: 'public/build/index.html'
}
]
}
}
});
grunt.loadNpmTasks('grunt-replace');
// Default task, you can obviously name this whatever you like
grunt.registerTask('default', ['replace']);
};
<html>
<head>
<!-- This variable corresponds with the var in our gruntfile -->
<script src="build/index.@@hash.js"></script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment