No real secret but maybe not every developer knows it: The JS /CSS is cached and the users do not clear their cache after you update your application :) There is a simple but effective way to handle it:
define('CACHE_STYLE_KEY', '3243jadfafas');
define('CACHE_JS_KEY', '3243jadfafas');
// ...
<link href="style/mystyle.css?<?php echo CACHE_STYLE_KEY; ?>" rel="stylesheet" type="text/css" /
<script src="javascript/app.js?<?php echo CACHE_JS_KEY; ?>" type="text/javascript"></script>
If you change your CSS/JS files, just also change the associated constant and the GET parameter will also be updated. Your users’ browsers will reload the files, and there’s no need to change the filename or anything else. And: The customers won’t call you and ask how to empty their browser cache :)
With the same way you could handle caching of images and other “static” data.


May 14th, 2007 at 13:22
Or you can just append the mtime on the file :-) That way, you don’t have to update your code at all. The penalty is two stat()s on every request, but it much easier.
May 14th, 2007 at 13:32
@ccsalvesen: Yes, you can. But that’s not the intention of this post because then the files will never be cached.
May 14th, 2007 at 14:35
mtime means modification time.. that should work just fine..
Alternatively you can make the browser explicitly check if there was an update with the proper caching headers
May 14th, 2007 at 14:44
@Evert: Oh yes, we accidentely mixed it up with atime()/ctime() . mtime() makes sense. The stat()’s should be buffered by the stat cache AFAIK. So this would really make the constants redundant. We’ll try it. Thanks!
Using Cache headers is an alternative, too, but you always have to set a cache period and you don’t know when the next update of your file occures. The GET parameter approach is more pracmatic and pratical IMHO.
May 14th, 2007 at 15:39
No, you can actually add in must-revalidate..
It will always first do a HEAD request to see whats up..
May 14th, 2007 at 15:48
Yeah that’s why I prefer the GET parameter method. It works without pain ;)
May 14th, 2007 at 17:03
That seems like a real hack for what you should be using Expires and Cache-control headers. You can definitely configure your headers to require the browser to check back to see whether a resource is modified. While this certainly works, I think it encourages people to not bother actually understanding the nuances of the cache control headers. For those that would like to better understand the browser controls, I found this to be a quite useful article: http://www.port80software.com/products/cacheright/cachingandcachecontrol
May 14th, 2007 at 17:34
Hi Hans,
you should code Propel, not blame us! :D Nice to meet you here ;)
But you’re right, it is somehow a hack, but it’s mainstream (many sites use it) and useable in 5 minutes… and I remember that relying on and trusting the browsers cache behavior can be a pain in the ass …
May 14th, 2007 at 20:44
Well, this ‘hack’ is not bad for CSS/JS files, but you should really set up caching headers correctly.
The following articel tells us that question mark shoul be avoided in image urls (and i guess thats the
case for CSS/JS stuff too) because it may not be cachen by some instances. The ’safer’ solution is
to have and directory or file that has some unique key.
http://www.die.net/musings/page_load_time/
May 15th, 2007 at 15:17
We append the current version of the code. That way once something is committed into CVS, and it goes live (via make install) this is the code in the html:
prototype.js?[% constants.cvs_tag_currentcode %]
May 16th, 2007 at 04:54
Unfortunately, my experience with using the headers (in particular must-revalidate) is that IE 6 doesn’t handle them properly. If you send back a 200 from a validation request, IE silently ignores the new content and uses the cached copy instead. Sending back a 304 (Not modified) with no content works as expected though.
How would the headers get properly set? The file request doesn’t pass through any of our code. Is there something like an apache setting that can add headers automatically based on a URL pattern?
Brandon
March 31st, 2010 at 07:04
Specialists state that loans aid people to live their own way, because they can feel free to buy needed stuff. Furthermore, different banks give bank loan for different persons.