Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force refresh of modified JS & CSS file in cakephp

I've only read about two options for cakephp caching files:

  • Rather you cache them all
  • Or you don't cache any.

I want something in between. Something equivalent as what we've all been doing in any other site, using a version parameter:

script.js?v=2

This way we can manually or even automatically force the load of modified files and at the same time have it cached when no change is made on them.

like image 530
Alvaro Avatar asked Sep 15 '15 16:09

Alvaro


People also ask

How do I force the browser to reload cached .JS .CSS files?

It works, all the time, for everyone. But, if you're not using it, and you just need to reload that one CSS or JS file occasionally in your own browser... just open it in its own tab and hit SHIFT-reload (or CTRL-F5)!

How do I force a reload of a stylesheet?

In Chrome, you can do a hard reload by opening the console (F12 key) and then right-clicking on the reload button to get a menu with additional reload options.

Why JS file is not reflected in browser?

Answers. if you make a change in the js file, after the page is displayed in the browser (it'll no doubt use the old js), hit ctrl + f5. It's not VS's fault, the browser caches the js. If you hit ctrl+f5 in the browser, it'll refresh the js and your new one will get loaded.


Video Answer


1 Answers

CakePHP provides a way of applying timestamps to assets like CSS and JS files that only refreshes when the file changes. This is an equivalent of doing 'script.js?v=2', Cake would generate URLs like 'script.js?1442387965'.

In CakePHP 3.x (in config/app.php):-

'Asset' => [
    'force'
]

In CakePHP 2.x (in app/Config/core.php):-

 Configure::write('Asset.timestamp', 'force');

If you only want to apply the timestamps whilst debug mode is enabled set the above to true instead of force.

like image 142
drmonkeyninja Avatar answered Sep 23 '22 20:09

drmonkeyninja