ETag header 是否会使 Cache-Control header 过时?那么如何确保 Cache-Control 无害呢?
Does the ETag header make the Cache-Control header obsolete? How to make sure Cache-Control is not harmful then?
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-Controlheader(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.
ETag
header 的存在不会告诉浏览器做任何事情。浏览器根据它在请求和缓存响应中收到的 Cache-Control
header 来决定要做什么。如果它确定资源已过时或需要 re-validated,则它可以使用 ETag
值创建对服务器的条件请求并获取新资源(状态代码 200),或者事情没有改变的通知(状态代码 304)
两个 header 都是您的缓存最佳工作所必需的。
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-Controlheader(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.
ETag
header 的存在不会告诉浏览器做任何事情。浏览器根据它在请求和缓存响应中收到的 Cache-Control
header 来决定要做什么。如果它确定资源已过时或需要 re-validated,则它可以使用 ETag
值创建对服务器的条件请求并获取新资源(状态代码 200),或者事情没有改变的通知(状态代码 304)
两个 header 都是您的缓存最佳工作所必需的。