Pragmatic Cache Invalidation

Server-side rendering is now standard practice for websites and applications. Browsers like Google Chrome also tend to cache as much as possible to minimize loading times for subsequent requests. Cache invalidation of CSS, JS, and image files can be achieved using `mod_pagespeed` , the `Expires` / `Cache` control header, a cache manifest , or, more pragmatically and directly, by adding custom parameters to the filename.


There are different strategies for when a file should be reloaded or not.

The simplest way is to reload on every request by appending a random string to the file to be embedded and thus force the reload (here an example of an image file):

2e634273f316c54b39828f057f7c7d9c

If that's too much of a good thing and you only want to reload when the file has changed, you can use the date of the last file modification (here using a JS file as an example).:

2e634273f316c54b39828f057f7c7d9c

If you use a version control system like Git , you can also use the unique hash value of the last commit (the abbreviated version is sufficient) (here using the example of a CSS file).:

2e634273f316c54b39828f057f7c7d9c

WordPress attaches the current WordPress version to all files integrated via enqueue_style and enqueue_script by default . This behavior can either be changed directly in the function calls as an argument, or you can hook yourself in globally and use one of the above methods:

2e634273f316c54b39828f057f7c7d9c

Back