Force refresh of cached CSS data

Viewed 56223

Is it possible force the browser to fresh the cached CSS?

This is not as simple as every request. We have a site that has had stable CSS for a while.

Now we need to make some major updates to the CSS; however, browsers that have cached the CSS will not receive the new CSS for a couple of days causing rendering issues.

Is there a way to force refresh of the CSS or are we better just opting for version specific CSS URLs?

4 Answers

Please go read Tim Medora's answer first, as this is a really knowledgeable and great effort post.

Now I'll tell you how I do it in PHP. I don't want to bother with the traditional versioning or trying to maintain 1000+ pages but I want to ensure that the user always gets the latest version of my CSS and caches that.

So I use the query string technique and PHP filemtime() which is going to return the last modified timestamp.

This function returns the time when the data blocks of a file were being written to, that is, the time when the content of the file was changed.

In my webapps I use a config.php file to store my settings, so in here I'll make a variable like this:

$siteCSS = "/css/standard.css?" .filemtime($_SERVER['DOCUMENT_ROOT']. "/css/standard.css");

and then in all of my pages I will reference my CSS like this:

<link rel="stylesheet" type="text/css" media="all" href="<?php echo $siteCSS?>" />

This has been working great for me so far on PHP/IIS.

Related