Should HTTP 304 Not Modified-response contain cache control headers?

I tried to figure this out and looked for SO for similar questions, but I still don't have a 100% understanding of how this should work.

I get this response on request for the image resource:

Response Headers Server Apache-Coyote/1.1 Date Mon, 19 Oct 2009 09:04:04 GMT Expires Mon, 19 Oct 2009 09:06:05 GMT Cache-Control public, max-age=120 Etag image_a70703fb393a60b6da346c112715a0abd54a3236 Content-Disposition inline;filename="binary-216-420" Content-Type image/jpg;charset=UTF-8 Content-Length 4719 

The desired behavior is that the client must cache it for 120 seconds and then request it again from the server. No request is sent to the server for 120 seconds.

Then, after 120 seconds, the request is sent and the response 304 is received:

 Response Headers Server Apache-Coyote/1.1 Date Mon, 19 Oct 2009 09:06:13 GMT Request Headers Host localhost:8080 User-Agent Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 Accept image/png,image/*;q=0.8,*/*;q=0.5 Accept-Language en-us,no;q=0.8,sq;q=0.7,en;q=0.5,sv;q=0.3,nn;q=0.2 Accept-Encoding gzip,deflate Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive 300 Connection keep-alive Referer http://localhost:8080/cms/site/0/en/home Cookie JSESSIONID=768ABBE1A3BFABE3B535900233330650; versionsCssDisplayState=block; iceInfo=iceOn:false,activePortletKey:,icePagePanelX:1722,icePagePanelY:3 If-None-Match image_a70703fb393a60b6da346c112715a0abd54a3236 

So far, all is well. But then, at the next request (within 120 seconds), I would think that the resource should be cached for 120 new seconds. What I see in the browser (Firefox), on the other hand, is that from that moment on, it always requests a resource and receives a 304 response.

Do I suppose to attach cache control headers in a 304 response? From what I can read in the specification, does it seem that cache controls should be omitted and that the cache should cache it for 120 new seconds automatically?

+52
html etag
Oct 19 '09 at 9:11
source share
3 answers

In theory, you don’t need to send Cache-Control for 304 - the recipient should just continue to use the cache directives received from the original 200. However, as you have found, in practice, if you do not send Cache-Control, browsers will ignore the cache directives that you sent initially, and return their own heuristics by default.

Thus, in practice, you should enable the same Cache-Control with 304 as with 200. The specification only requires that you send it for 304 if it differs from what you sent earlier (see 10.3.5 304 Not changed ) - but this, of course, does not forbid you to repeat it when it is the same.

And to answer specifically the wrong points from another answer (Structure):

  • You want the intermediate caches to cache the response (i.e. update your cache entry for the resource). They will respond appropriately to requests from clients with 200 or 304, depending on whether the client included a conditional header, for example, If-Modified-Since.

  • A 120 second ttl will be updated 304 (so the same client should not make another request for the same resource for at least another 120 seconds). And customers, while they still store cached content, will continue to make conditional requests for the resource, which you can continue to respond with 304.

+37
Dec 23 '09 at 22:19
source

RFC7232 updates RFC2616 to say:

The server generating the 304 response MUST create any of the following header fields that were sent in a 200 (OK) response to the same request: Cache-Control, Content-Location, Date, ETag, Expires, and Vary.

+45
Dec 08 '10 at 23:45
source

If I understand correctly, the browser actually caches 120 seconds, and your server responds with 304 Not Modified to subsequent If-Modified-Since requests. This IMS request occurs when the end user accesses the same URL. At this time, the browser may send an If-Modified-Since request. The browser wants to know if it displays outdated content. This seems normal.

After receiving this request, your server should respond with 200 OK, 304 Not Modified (or 4XX, if necessary).

I do not believe that you should configure the server to send a Cache-Control header with a response of 304 for two reasons:
1. You do not want intermediate caches to cache this answer 304 (chances are they can)
2. A 120-second TTL will not be updated with a response of 304. The browser will save the object for 120 seconds after a response of 200 OK. After 120 seconds, the browser should send a GET request, not If-Modified-Since, so your server will respond with file bytes, not just 304.

Please note that the browser will not request the file again automatically after 120 seconds, unless the end user requests it directly through the page load or directly enters the URL into its address bar (or if you do not have a custom application that controls this functionality as that).

Edited the first paragraph to read a little better (hopefully)

+2
19 Oct. '09 at 10:13
source