HTTP 状态代码 200(缓存)与状态代码 304 之间有什么区别? [英] What is the difference between HTTP status code 200 (cache) vs status code 304?

查看:25
本文介绍了HTTP 状态代码 200(缓存)与状态代码 304 之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用适用于 Firefox 的 GooglePage Speed"插件来访问我的网站.

我页面上的某些组件显示为 HTTP 状态:

200200(缓存)304

由 Google 的Page Speed"提供.

让我感到困惑的是 200(缓存)和 304 之间的区别.

我多次刷新页面(但没有清除我的缓存),而且我的 favicon.ico 和一些图像似乎总是 status=200(缓存),而其他一些图像是 http status 304.

我不明白为什么会有不同.

更新:

使用 Google 的Page Speed",我收到 http://example.com 的200(缓存)"/favicon.ico 以及 http://cdn.example.com/js/ga.js

但是,我收到 http://cdn.example 的 http 状态304".com/js/combined.min.js

我不明白为什么在同一目录/js/中有两个 JavaScript 文件,一个返回 http 状态 304,另一个返回 200(缓存)状态代码.

解决方案

带有代码200(缓存)"的项目直接从您的浏览器缓存中完成,这意味着对项目的原始请求返回的标头表明浏览器可以缓存它们(例如未来日期的 ExpiresCache-Control: max-age 标头),并且在您触发新请求时,这些缓存的对象是仍然存储在本地缓存中,尚未过期.

另一方面,304 是服务器在浏览器检查文件自上次缓存后是否被修改后的响应(答案为否").

为了获得最佳的网络性能,最好为所有资产设置一个远期的 Expires:Cache-Control: max-age 标头,然后当需要更改资产时,更改资产的实际文件名或将版本字符串附加到对该资产的请求.这消除了发出任何请求的需要,除非资产确实从缓存中的版本改变了(不需要那个 304 响应).Google 有有关正确使用长期缓存的更多详细信息.

I'm using the Google "Page Speed" plug-in for Firefox to access my web site.

Some of the components on my page is indicated as HTTP status:

200 200 (cache) 304

By Google's "Page Speed".

What I'm confused about is the difference between 200 (cache) and 304.

I've refreshed the page multiple times (but have not cleared my cache) and it always seems that my favicon.ico and a few images are status=200 (cache) while some other images are http status 304.

I don't understand why the difference.

UPDATE:

Using Google "Page Speed", I receive a "200 (cache)" for http://example.com/favicon.ico as well as http://cdn.example.com/js/ga.js

But, I receive a http status "304" for http://cdn.example.com/js/combined.min.js

I don't understand why I have two JavaScript files located in the same directory /js/, one returning a http status 304 and the other returning a 200 (cache) status code.

解决方案

The items with code "200 (cache)" were fulfilled directly from your browser cache, meaning that the original requests for the items were returned with headers indicating that the browser could cache them (e.g. future-dated Expires or Cache-Control: max-age headers), and that at the time you triggered the new request, those cached objects were still stored in local cache and had not yet expired.

304s, on the other hand, are the response of the server after the browser has checked if a file was modified since the last version it had cached (the answer being "no").

For most optimal web performance, you're best off setting a far-future Expires: or Cache-Control: max-age header for all assets, and then when an asset needs to be changed, changing the actual filename of the asset or appending a version string to requests for that asset. This eliminates the need for any request to be made unless the asset has definitely changed from the version in cache (no need for that 304 response). Google has more details on correct use of long-term caching.

这篇关于HTTP 状态代码 200(缓存)与状态代码 304 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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