1

I know this is actually a old thing but I really got questions about this and how it was supposed to work when it started. So I got at least these four important cache headers I can send back to the client (Last-Modified, Cache-Control, Expires and ETag)

Example situation for question 2.2:

So let's say I got some site with articles. There might be a new article every 15 minutes to 7 days. So I set Cache-Control and Expires to cache for 15 min so the client always got the newest version of it.

What I think about ETag and Last-Modified at the moment:

I just put some hash of the content in there and I can send the client 304 if If-None-Match == ETag.

I can send the client 304 if If-Modified-Since >= Last-Modified.

Questions

  1. Do I need Cache-Control and Expires to support all browsers or not ?
  2. It looks like Cache-Control and Expires only tells my browser how long the content should be cached on the computer right?
    • So I can only use ETag and Last-Modified to find out when I should send 304 right?
    • So I could just set Cache-Control and Expires to forever and just send the client the new version if ETag or Last-Modified changed that?
      • Because this works in my browser but will this work in all browsers?
  3. Do I need ETag and Last-Modified to support all browsers or not ?
  4. Pragma looks like another cache header similar to Cache-Control, which browsers are using Pragma and do I need it?
noob
  • 8,982
  • 4
  • 37
  • 65

1 Answers1

3

Do I need Cache-Control and Expires to support all browsers or not ?

Cache-Control is introduced in HTTP 1.1 and hence only supported on clients supporting HTTP 1.1. Expires is supported since HTTP 1.0. If both are specified, then Cache-Control takes precedence and Expires is ignored altogether.


It looks like Cache-Control and Expires only tells my browser how long the content should be cached on the computer right?

Basically, yes. If the Cache-Control is absent (and thus Expires will be used) or set right with max-age.


So I can only use ETag and Last-Modified to find out when I should send 304 right?

Here, your question starts to be confusing. You actually need to check If-None-Match and If-Modified-Since headers for that. Usually, browser sends only one of them on a conditional GET request and if it matches with the current value of ETag or Last-Modified in the server, then return 304 along with the ETag header. Note that if browser sends them both for some reason, then you can just ignore If-Modified-Since.


So I could just set Cache-Control and Expires to forever and just send the client the new version if ETag or Last-Modified changed that?

No. If you set it to forever, the client will never send a (conditional) request until the enduser pressed (Ctrl)+F5. You need to set the Cache-Control: max-age or Expires to the desired cache time (to 15 minutes in the future maybe?).


Because this works in my browser but will this work in all browsers?

Watch out how you test: pressing F5 will always send a conditional GET request. But a simple page-to-page navigation and form submits (like as in real world) won't send a conditional GET request! Pressing Ctrl+F5 will ignore the cache and send a brand new GET request.


Do I need ETag and Last-Modified to support all browsers or not ?

All browsers support both. Browsers are required to send ETag back via If-None-Match if it has been received from the server and browser needs to perform a conditional GET request. The Last-Modified is for them optional and usually only sent if ETag is absent on the resource. See also RFC 2616 section 13.3.4.


Pragma looks like another cache header similar to Cache-Control, which browsers are using Pragma and do I need it?

You don't need it if you want to cache. You only need it if you want to not cache (in order to cover HTTP 1.0 clients/proxies, whose population is currently steadily decreasing, though).


If you're familiar with Java, or can at least decipher it, then you may find this article helpful: FileServiet supporting resume and caching and GZIP.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • So the browser won't even send a HTTP request if `Cache-Control` says that the cached version can be used right? So a HTTP request will only be send if `Cache-Control` says that the cached version is outdated but if the server responds with 304 the browser will **still** use the cached version right? If both questions are right then will the respond get cached (local) when `max-age=0` or only if it is at least `max-age=1`? (So that there's sth. that can be used when the server responds with 304.) – noob Aug 26 '13 at 21:04
  • That's correct. Note that the browser will keep sending conditional GET requests unless you send an updated `Cache-Control` or `Expires` header back along with the 304 which basically postpones the cache more. – BalusC Aug 26 '13 at 21:16