http得到304但仍然下载内容

gzc*_*gzc 4 google-chrome

我请求了一个html文件,我得到了http状态码304。我知道这意味着内容没有被修改。所以浏览器可以使用缓存。但是我注意到在请求时间信息内容下载中使用了 2.63s。

我使用 Chrome 开发工具进行了调试。

既然浏览器有缓存,为什么还要下载内容?

myD*_*ode 5

Checkout the difference between freshness and stale state of cache.

Stage 1: Requesting for very 1st time. Made request to server and got resources. Each resource is fetched freshly 在此处输入图片说明

In my case, headers for my js files is cache-control:public, max-age=3, meaning, cache becomes stale from fresh in 3 seconds(pretty less time taken to demonstrate difference)

State 2: Again requesting (Ctrl+Refresh). Now, since my cache is stale, it sends requests to server, which tells that resource are not modified. Hence those resources AREN'T fetched entirely. Only the headers are fetched. Carefully note the bytes(~220bytes for each) transfer in each case which is only for headers of those resources. Hence 304(Not Modified) saved the lot of kbs in not transferring body again. Freshness of my resources was STALE which is why it sent request to server 在此处输入图片说明

And when you check the body, the response in 2nd is same as 1st, BUT body is taken from stale cache and the resource cache is again marked FRESH for 3 seconds.

Note, in my case, I can optimise by setting cache max-age to be 1 year(some high no.) because I'm using chunkhash for each file which would tell browser to not make request to server and directly take from cache which gives response 200 (from cache) as you can see analytics file in Stage2 whose headers are cache-control:public, max-age=7200


小智 3

Chrome 正在发出请求,询问服务器是否应该使用缓存。服务器响应“HTTP/1.1 304 Not Modified”和一些额外信息。您可以在此处阅读有关此内容的更多信息。

总响应大小通常低于 1KB,因此 2.63 要么以毫秒为单位,要么以该请求的总时间为单位。您可以在此处阅读有关资源网络计时的更多信息。

更新:

正如阿里正确指出的那样,内容下载将导致缓存检索。根据文件大小和系统配置,这可能需要一段时间。

关于缓存的更详细解释请参见此处

Chrome 扩展程序的速度也可能会变慢 -此处的速度可能会变慢。