How to change name of minified files in W3 Total Cache plugin for WordPress

Questo articolo รจ stato scritto oltre 1 years, il contenuto potrebbe essere datato.

In my web agency Codeat.co we don’t only develop plugins for living but we do also WordPress maintenance and custom development.
As we manage also high traffic websites and personally I love W3 Total Cache (because has a lot of settings, so we can tune as we want/need with CDN integrations).

The code of this plugin is also on GitHub so if you have technical issues or you need to investigate is very easy, an approach that is very important for our job.

Anyway there is a problem with minified assets like CSS/JS because the file name is always the same (also when they are combined).

Basically at this function when W3TC need to generate a filename create a md5 hash of the file name(s) (also when combined). This is an issue if you modify one of those files, purge the cache and the file cached by browser is the same. This happens because the file name is the same, but not the content, so an easy workaround as example in WordPress is the usage of query string like &ver=5.4 in the assets url so the browser will cache it again, just because the file has a different url (this parameter that is version based).

In W3TC you cannot do this, also is tricky because this plugin remove those to improve the caching or security plugins can remove or block the access to this resources for query string filtering.

The best way is to generate a new file name based on the checksum of the content of the files, so if the files are changed (also combined) is different otherwise is the same/previous.

Very easy, no?

You can find a code snippet (uses a filter) for W3TC that enable this behavior on this page.

Liked it? Take a second to support Mte90 on Patreon!
Become a patron at Patreon!

Leave a Reply

Your email address will not be published. Required fields are marked *