Optimized CSS/JS cachebusting using Git

I’m sure we’ve all had times when users have issues because they didn’t refresh their browser cache. Sometimes we’ll program in a quick ?<?PHP print time(); ?> after our css and JS files so cachebusting is ensured, or we’ll do some .htaccess wrangling. While I’m sure there are better ways to tackle this, I’ve pieced together a quick fix to ensure cachebusting on CSS and JS files (other files would be just as easy to implement as well).

Here’s the Gist - gist.github.com/3092857 (Sorry, I guess I’m too new to post links)

In a nutshell, the main config file creates a Yii param ‘git-hash’ set to the hash of the current git commit, essentially a unique version number. Then, in your subclassed Controller.php (or in whatever controller you prefer) simply tack on a ?[hash] and you’re good to go!

Let me know what you think in the comments. I’m a fan of this method because it makes sure that users get the latest version of files without having to download new files every time they visit, saving bandwidth and time.