Support » Plugin: W3 Total Cache » Query String ID for Javascript/Stylesheet Caching

  • Hey Fredrick,

    In noticed that in /lib/W3/Plugin/BrowserCache.php you’re generating the $cache_id by creating an MD5 hash of all the options that are set. It’s this variable that’s used to append to javascript and stylesheet calls (e.g. styles.css?fdb553). Practically speaking, a user has to change some of the W3TC options in order to have a new query string generated.

    Have you ever considered creating the $cache_id with an MD5 hash of the included file (e.g. md5_file($file)) instead? That way any time the stylesheet or javascript changes, the browser (and/or CDN) is forced to pick up the change?

    Related question:

Viewing 2 replies - 1 through 2 (of 2 total)
  • After thinking about it for a bit, perhaps a combination of the two might work best.

    I’m thinking that if you appended the linked files with an MD5 hash of the W3TC options and and MD5 hash of the linked file size, you’d force browsers to download the new file when the options change or when the file changes. Seems like a win-win to me.

    For what it’s worth, here’s how I solved this.

    First, I disabled the W3TC option to add query strings to the tail of JS/CSS files.

    Then, I dropped the following code into my functions.php, specifically where I’m registering my main stylesheet and javascript files:

    Nutshell: when registering m primary stylesheet and javascript, instead of passing a version number, I’m passing a CRC32-hashed string of the file. That way, when the size of the file changes (i.e. you make an update), so does this hash string.

    Thus, browsers and proxies are forced to download the updated file, no matter the caching policy.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Query String ID for Javascript/Stylesheet Caching’ is closed to new replies.