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

前端 未结 3 1069
耶瑟儿~
耶瑟儿~ 2020-12-12 23:43

I\'ve tried to understand this, and searched SO for similar questions, but I still don\'t have a 100% understanding on how this is supposed to work.

I get this respo

相关标签:
3条回答
  • 2020-12-13 00:08

    If I understand correctly then the browser is in fact caching for 120 seconds and your server is responding 304 Not Modified to subsequent If-Modified-Since requests. This "IMS" request occurs when the end-user accesses the same URL. At that time the browser can send an If-Modified-Since request. The browser wants to know if it is displaying stale content. This seems normal.

    Upon receiving this request your server should reply 200 OK, 304 Not Modified (or a 4XX, if necessary).

    I do not believe you should set your server to send a Cache-Control header with the 304 response for two reasons:
    1. You do not want any intermediary caches to cache that 304 response (there is a possibility that they could)
    2. The 120 second TTL will not be refreshed by the 304 response. The browser will retain the object for 120 seconds from the 200 OK response. After 120 seconds the browser should send a GET request, not an If-Modified-Since, so your server will respond with the bytes of the file and not just a 304 response.

    Note that the browser will not request the file again automatically after 120 seconds unless the end-user specifically requests it via a page load or directly inputting the URL into their address bar (or unless you have a custom application that controls that functionality somehow).

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

    0 讨论(0)
  • 2020-12-13 00:17

    RFC7232 updates RFC2616 to say:

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

    0 讨论(0)
  • 2020-12-13 00:24

    In theory you shouldn't have to send Cache-Control for a 304 -- the recipient should just continue to use the cache directives that it received from the original 200. However, as you've found, in practice if you don't keep sending Cache-Control, browsers will ignore the cache directives that you sent originally, and revert to their own default heuristics.

    So in practice, you should include the same Cache-Control with a 304 that you would with a 200. The spec only mandates that you send it for a 304 if it's different than what you sent previously (see 10.3.5 304 Not Modified) -- but it certainly doesn't forbid you from repeating it when it's the same.

    And to respond specifically to the wrong-headed points from the other answer (Structure's):

    1. You do want intermediary caches to cache the response (that is, update their cache entry for the resource). They will respond appropriately to requests from clients with a 200 or a 304, depending on whether the client included a conditional header like If-Modified-Since.

    2. The 120-second ttl will be refreshed by the 304 (so the same client shouldn't make another request for the same resource for at least another 120 seconds). And clients, as long as they've still got the content cached, will continue to make conditional requests for the resource, which you can continue to respond to with a 304.

    0 讨论(0)
提交回复
热议问题