Show Posts
← Back to homepage

There’s a very useful PHP function called filemtime, that returns the timestamp of the modification time of the file. This is similar to how the HTTP 1.1 ETag header is generated. The strategy is basically to append the modification date to the script or CSS URI in order to refresh the user’s cache when you’ve modified them.

This is an extract from Devthought header.php WordPress template file:




All you have to do is change the routes to match your files. If you’re not using wordpress, you’ll have to remove the get_stylesheet_directory* and get_template_directory* function calls and replace with your paths.

3 Comments

Claude said

Why would you want to prevent caching of your JS ans CSS files?

Is this post more about busting the cache of an old version of the file when a new is available?

    Guillermo Rauch said

    Of modified CSS & JS files :D

    It’s more ‘refresh users’ cache without waiting for the user agent to perform ETag comparison’, but it’s not a good title.

Your thoughts?

About Guillermo Rauch:

CTO and co-founder of LearnBoost, developer, open source enthusiast, blogger.