Skip to content

Instantly share code, notes, and snippets.

@alexw23
Created October 31, 2013 11:19
Show Gist options
  • Save alexw23/7248070 to your computer and use it in GitHub Desktop.
Save alexw23/7248070 to your computer and use it in GitHub Desktop.
Bust caches in Laravel with Grunt. This will generate urls like "assets/css/style-cc673fcda9ae387ae82c83913f8b43a4.css"
{{ HTML::style(asset_bust('assets/css/style.css')) }}
# will generate as assets/css/style-cc673fcda9ae387ae82c83913f8b43a4.css
module.exports = function(grunt) {
var changedCoffee, onChange;
grunt.initConfig({
// Read project settings
pkg: grunt.file.readJSON("package.json"),
cachebuster: {
build: {
options: {
basedir: 'public/',
format: 'php',
banner:
'/**\n' +
' * GENERATED FILE, DO NOT EDIT. This file is simply a collection of generated hashes for static assets in \n' +
' * the project. It is generated by grunt, see Gruntfile.js for details.\n' +
' */',
complete: function(hashes) {
return {
md5: hashes
};
}
},
src: ['public/assets/**/*'],
dest: 'app/config/cachebuster.php'
}
},
});
};
if ( ! function_exists('asset_bust'))
{
function asset_bust($path, $secure = null)
{
$busts = Config::get('cachebuster.md5');
if(!is_null($busts) && is_array($busts) && array_key_exists($path, $busts))
{
$md5 = $busts[$path];
$parts = pathinfo($path);
$dirname = ends_with($parts['dirname'], '/') ? $parts['dirname'] : $parts['dirname'] . '/';
$path = "{$dirname}{$parts['filename']}-$md5.{$parts['extension']}";
}
return asset($path, $secure);
}
}
location ~* (.+)\-(?:[0-9a-f]\{32\})\.(js|css|png|jpg|jpeg|gif)$ {
try_files $uri $1.$2;
}
#before any other location blocks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment