Home » Php » php – Force refresh of modified JS & CSS file in cakephp

php – Force refresh of modified JS & CSS file in cakephp

Posted by: admin July 12, 2020 Leave a comment

Questions:

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.

How to&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.