Created
October 31, 2013 11:19
-
-
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"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{ HTML::style(asset_bust('assets/css/style.css')) }} | |
# will generate as assets/css/style-cc673fcda9ae387ae82c83913f8b43a4.css |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | |
} | |
}, | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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