inicio sindicaci;ón

Handling Browser-Caching of JS / CSS

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.

12 Kommentare to “Handling Browser-Caching of JS / CSS”

  1. ccsalvesen Says:

    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.

  2. Springub Says:

    @ccsalvesen: Yes, you can. But that’s not the intention of this post because then the files will never be cached.

  3. Evert Says:

    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

  4. Soenke Says:

    @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.

  5. Evert Says:

    No, you can actually add in must-revalidate..

    It will always first do a HEAD request to see whats up..

  6. Soenke Says:

    Yeah that’s why I prefer the GET parameter method. It works without pain ;)

  7. Hans Says:

    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

  8. Soenke Says:

    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 …

  9. elias Says:

    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/

  10. tf23 Says:

    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 %]

  11. Brandon Says:

    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

  12. TrudyTerrell26 Says:

    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.

Leave a Reply

*
To prove that you're not a bot, enter this code
Anti-Spam Image