Cache-busting static assets

I’d like to know what people who use external asset build tools (grunt etc. rather than Asset Manager) do for cache-busting. To start the discussion, here’s my new scheme. It’s not very DRY but it should work.


[*]Grunt builds js and css files.

[]The web server sets a distant Expires value for ".js" and "*.css" files.

[*]In app root, a file "constants.php" contains YII_DEBUG etc. that the entry scripts include.

[*]“constants.php” also declares my app’s CACHE_BUSTER global const.

[*]When grunt runs, it updates “constants.php” changing the value of CACHE_BUSTER to the current git short hash, e.g. [font=“Courier New”]define(‘CACHE_BUSTER’, ‘8a522dc’)[/font].

[*]Grunt uses the same value in the name of the asset files it makes, e.g. "script.8a522dc.js".

[*]The layout views use CACHE_BUSTER in the URLs to the static files, e.g. "/js/script.8a522dc.js".


What does everyone else do? Do you have a critique of this plan?

And is there anything Yii could do to be more helpful in solving this problem?

I’m just appending filemtime of what grunt generated to the URL i.e. /js/all.js?v=13949213490

So in order to bust cache I’m re-running grunt and that’s it.

How does the mtime get into the URL? Does the layout view read it from the filesystem?

And why it’s better to change the filename over using a query string.

Yes, layout uses custom component which reads mtime from file system. Using query string isn’t better. It just works well for us.

The CACHE_BUSTER constant in my scheme is to avoid the filesystem access every time a layout is used.

Yes, I understand that. Your schema looks OK to me.