Simple Cache buster for Laravel

Laravel is an amazing framework and comes with something called Elixir that handles front end processing. Unfortunately, my front-end requirements have some specific needs. Combined with Docker builds and proxy caching there wasn’t a simple way that I could reliably regenerate a hash for each Docker Deploy.

Elixer does indeed have a way to version public assets but as I mentioned would not work in my situation.

I opted to add a task to my gulpfile.js that will generate a file in ‘config/cacheBuster.php’ that simply contains a timestamp of the build and is returned in a Laravel Config file.

First you will need to install the File System package found here by typing npm install file-system --save

Next add the following task to your Gulp build.

gulp.task('cacheBuster', function(cb){
    fs.writeFile('config/cacheBuster.php', '<?php return ' + new Date().getTime() + ';', cb);
});

You can call the cache buster in your templates with {{Config::get('cacheBuster')}} Just make sure that you add ‘cacheBuster’ to your production build!

Example
file.css?{{Config::get('cacheBuster')}}



Signup for my mailing list

Receive other rambings like this on design, code, and some times food.