Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cache Busting with Laravel

I've been developing a system and every time I update anything on the public folder, each computers must always clear their cache in order for it to take effect.

I've only been coding directly on to the public folder for the js and css files. When I push it to my git repository and pull it to our shared hosting through ssh after that I copy it to the public_html folder through ssh as well, all client computers still need to clear their caches manually in order for new public files to take effect. I've been studying a bit of laravel mix however I am still unsure how to actually use it in a live hosting.

I am unsure if cache busting is the right term at this time but I wanted my client computers to use the latest assets every time I update our system.

like image 947
LEAD IT Avatar asked May 29 '19 01:05

LEAD IT


2 Answers

Another quick and dirty way of cache busting is using the last modified timestamp of a file as a version string. This way if you update the file, the timestamp changes too. PHP has the filemtime method for this.

I use this all the time in Wordpress, but in a Laravel blade template it looks something like this:

<script src="{{ asset('js/scripts.js') }}?ver={{ filemtime(public_path('js/scripts.js')) }}"></script>

I do recommend going the built-in Laravel way because it comes with a lot of other advantages. But the principle of this trick can be useful in many other situations.

like image 96
bramchi Avatar answered Oct 20 '22 15:10

bramchi


Laravel has a built-in system for compiling assets like CSS and JavaScript, which includes a versioning system that ensures when you push a new version, users receive those updated assets.

mix.js('resources/js/app.js', 'public/js')
   .version();
like image 3
ceejayoz Avatar answered Oct 20 '22 14:10

ceejayoz