Cache Busting CSS

If you maintain your own site you’ve probably run into overzealous caching of CSS by either the web server, ISPs, the browser or some combination of all of all three. Here’s a quick-n-dirty way to bust the caching of CSS without compromising performance:

<?php echo '<link rel="stylesheet" type="text/css" href="/css/index.css?<?php echo(time());?>"/>

The above results in:

<link rel="stylesheet" type="text/css" href="/css/index.css?1492810248" />

The highlighted portion tricks whatever’s caching the .css file into thinking that it has been updated and forces download of a new one to the client. Using the current time (seconds since the Unix Epoch [January 1 1970 00:00:00 GMT]) keeps you from having to generate your own versioning scheme. You can do the same for .js files as well.

And that’s all I have to say about that.

Leave a Reply