http - ETag header 是否使 Cache-Control header 过时?那么如何确保 Cache-Control 无害呢?

标签 http caching http-headers cache-control etag

ETag header 的定义 ( https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag ):

The ETag HTTP response header is an identifier for a specific version of a resource. It allows caches to be more efficient, and saves bandwidth, as a web server does not need to send a full response if the content has not changed. On the other side, if the content has changed, etags are useful to help prevent simultaneous updates of a resource from overwriting each other ("mid-air collisions").



Cache-Control header 的定义 ( https://developer.mozilla.org/de/docs/Web/HTTP/Headers/Cache-Control ):

The Cache-Control general-header field is used to specify directives for caching mechanisms in both requests and responses.



因此 ETag header 告诉浏览器资源向服务器发送单个 HTTP 请求并询问文件哈希是否已更改。如果是,请下载一个新的。伟大的。因此,如果设置了 ETag header ,为什么我还需要 Cache-Control(除了可能有助于避免此单个请求的 Expires header 之外)?

因此,如果我必须设置 Cache-Control header ,那只会有害,对吗?我认为最合适的值是:
Cache-Control: must-revalidate

但我不确定这是否会触发不必要的额外操作。

最佳答案

经过一番研究,我发现了 Alex Barashkov 在 Medium 上的一篇很棒的教程:"Best practices for cache control settings for your website" .

亚历克斯写道:

I recommend you apply Cache-Control: no-cache to html files. Applying “no-cache” does not mean that there is no cache at all, it simply tells the browser to validate resources on the server before use it from the cache. That’s why we need to use it with Etag, so browsers will send a simple request and load the extra 80 bytes to verify the state of the file.

关于http - ETag header 是否使 Cache-Control header 过时?那么如何确保 Cache-Control 无害呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55025948/

相关文章:

html - css @import 相对于它链接到的 HTML 文档?

caching - 通过 HTTPS 向另一个子域发出有条件或无条件请求?

java - 如何通过 post man post 请求在 header 中传递对象?

PHP 内容处置 header : Specialcharacters in the filename

php - 服务器 header 、引荐来源网址等

java - 为什么不成功 “HTTP GET request with socket ” ?

c# - 如何使用 HttpWebRequest 转义 POST 中的 URL 编码数据

sockets - 为什么代码显示 "Error 354 (net::ERR_CONTENT_LENGTH_MISMATCH): The server unexpectedly closed the connection."

C# 文件缓存

python - 如何防止 python urllib3 缓存响应