Monday, July 21, 2008

Cache Control Settings

I've always get burnt for setting the wrong HTTP headers for cache control. So here's the braindump of what these headers are and what to set them to. The RFC for HTTP header field definitions is located here.

Note that it's always better to set the headers both in the HTTP-EQUIV META tags as well as to set it via the headers as some browsers have problems parsing the HTTP-EQUIV META tags.

There are 3 HTTP headers that control browser caching behaviour. These are:
  • expires
  • pragma
  • cache-control
The "expires" directive gives the date/time after which the response is considered stale. The value 0 (zero) indicates that the response is has immediately expired while any negative value indicates that the response had already expired.

The "pragma" directive indicates that the page should not be cached. The common use is to set the directive to "pragma: no-cache".

The "cache-control" directive is a HTTP 1.1 specification. It instructs the browser how the page should be cached. Commonly set values for this directives are:
  • no-cache - tells the browser not to use a cached copy of the page
  • no-store - tells the browser never to cache the page
At the end of the day, the proper headers should be as follows:
expires: -1
cache-control: no-store
pragma: no-cache

No comments: