如果没有etag,缓存控制无法正常工作 [英] cache-control not working without etag

查看:205
本文介绍了如果没有etag,缓存控制无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在响应中发送以下标题。 Cache-Control:public,max-age = 300,但每次点击刷新时我都会得到 200 响应(再次向服务器发出请求)。如果我添加Expires标题也会出现同样的情况。

I am sending the following header in the reponse. "Cache-Control: public, max-age=300", but still every time I hit refresh I get a 200 response(the request is made to he server again). Same happens if I add the "Expires" header.

但如果我在标题中添加ETag,那么我得到 304 on refresh(请求进入服务器,服务器准备响应,然后匹配ETag并返回 204 响应)。

But if I add a ETag to the headers, then I get 304 on refresh(the request goes to the server, the server prepares the response, then matches the ETag and returns a 204 response).

我应该更改什么,以便使用Cache-Control标头,
内容从本地缓存提供,没有请求被发送到服务器,直到年龄超过max-age

What should I change so that "Cache-Control" header is used and the content is served from local cache and no request is sent to the server until the age becomes more than "max-age"?

编辑:这是一个未缓存的图像 https ://image-dev-dot-quizizz-dev.appspot.com/resource/gs/quizizz-image/rejected.jpeg

Here is an image that doesn't get cached https://image-dev-dot-quizizz-dev.appspot.com/resource/gs/quizizz-image/rejected.jpeg

推荐答案

您的图片 在代理缓存中 - 请注意您在回复中获得年龄标头。此外,300秒内的每个下一个请求都需要更少的时间。为什么状态不是304?根据这篇文章

Your image is in proxy cache - notice that in a response you get Age header. Furthermore, every next request in 300 seconds takes much less time. Why is the status not 304? According to this article:


200(来自缓存)vs 304

200 (from cache) vs 304

现在执行网站的另一天性能审计我注意到
我们的很多资产都返回了304状态。在比较
另一个网站时,我注意到它返回了200(来自缓存)状态
代码。这引起了我的兴趣,我想深入挖掘。

Now the other day while performing a site performance audit I noticed that a lot of our assets were returning 304 statuses. While comparing another site I noticed that it was returning a 200 (from cache) status code. This intrigued me and I wanted to dig deeper.

事实证明,当给出200(来自缓存)响应时,它意味着
表示未来的到期日期在内容上设置。 实质上,
浏览器甚至没有真正与服务器通信以检查文件的
。它知道在到期日期已过期
之前不会这样做。

It turns out that when a 200 (from cache) response is given it means that a future expiration date on the content is set. In essence the browser doesn’t even really communicate with the server to check on the file. It knows not to do it until the expiration date has expired.

相比之下,304进入服务器并收到回复的响应
数据没有变化。 服务器告诉浏览器使用
缓存作为结果。

By contrast a 304 goes to the server and receives a response back that the data has not change. The server is telling the browser to use the cache as a result.

这篇关于如果没有etag,缓存控制无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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