ETag标头会使Cache-Control标头过时吗?那么如何确保Cache-Control不会有害呢? [英] Does the ETag header make the Cache-Control header obsolete? How to make sure Cache-Control is not harmful then?

查看:148
本文介绍了ETag标头会使Cache-Control标头过时吗?那么如何确保Cache-Control不会有害呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ETag标头的定义( https:// developer。 mozilla.org/zh-CN/docs/Web/HTTP/Headers/ETag ):

Definition of ETag header (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag):


ETag HTTP响应标头是资源的特定版本
的标识符。它允许缓存更高效,并节省
带宽,因为如果
内容未更改,则Web服务器无需发送完整的响应。另一方面,如果内容已更改
,则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").

缓存控制标头的定义( https://developer.mozilla.org/de/docs/Web/HTTP/Headers/Cache-Control ):

Definition of Cache-Control header (https://developer.mozilla.org/de/docs/Web/HTTP/Headers/Cache-Control):


Cache-Control的常规头字段用于为请求和响应中的缓存机制指定指令

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

因此,ETag标头告诉浏览器中某个资源向服务器发送单个HTTP请求,并询问文件哈希是否已更改。如果是,请下载一个新的。大。因此,如果设置了ETag标头,为什么我还需要缓存控制(在Expires标头旁边,这可能有助于避免此单个请求)?

So the ETag header tells the browser for a resource to send a single HTTP request to the server and ask if the file hash has changed. If yes, download a new one. Great. So if the ETag header is set why should I need Cache-Control any more (beside of the Expires header which may help to avoid this single request)?

如果我无论如何都必须设置Cache-Control标头,这只会是有害的吧?我认为最合适的值是:

So if I have to set the Cache-Control header anyway it can only be harmful right? I think the most appropriate value would be:

Cache-Control: must-revalidate

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

But I am not sure if this triggers unecessary additional actions.

推荐答案

经过研究,我发现了Alex Barashkov撰写的有关 Medium 的出色教程:>您网站的缓存控制设置的最佳做法

After some research, I found a great tutorial on Medium by Alex Barashkov: "Best practices for cache control settings for your website".

亚历克斯写道:


我建议您应用 Cache-Control:no-cache 到html文件。应用
无缓存并不意味着根本没有缓存,只是
告诉浏览器在使用缓存中的
之前先验证服务器上的资源。这就是为什么我们需要将其与Etag一起使用,因此浏览器
将发送一个简单请求,并加载额外的80个字节以验证文件的
状态。

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标头会使Cache-Control标头过时吗?那么如何确保Cache-Control不会有害呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆