为什么即使设置了缓存标头,Google Pagespeed为何仍要求指定ETag [英] Why does google pagespeed asks to specify ETag even when cache headers are set
问题描述
我已将缓存标头设置为将来(从现在开始算起一年),并按照YSlow(
I have set cache headers to be far in future (1 year from now) and have disabled the ETags as advised by the YSlow (http://developer.yahoo.com/performance/rules.html#etags) but Google pagespeed seems to require ETag (or last-modified) even after the cached headers are set.
为所有可缓存资源指定Expires或Cache-Control max-age之一,以及Last-Modified或ETag之一,这一点很重要."
"It is important to specify one of Expires or Cache-Control max-age, and one of Last-Modified or ETag, for all cacheable resources."
这两个规则似乎相互冲突.
The two rules seems to be conflicting each other.
推荐答案
YSlow通常不建议删除ETag,但对于某些环境,建议不要这样做.不使用ETag时,应改为使用Last-Modified
.
YSlow does not advise to remove ETags in general but for some environments. When not using ETags then you should use Last-Modified
instead.
ETag
和Last-Modified
用于有条件的GET请求,当重新请求已缓存且可能已过期的资源时.
Cache-Control max-age
用于确定缓存项有效的时间,无需再次询问. (当该规则过期时,浏览器将执行条件GET ...)
Cache-Control max-age
is for defining how long a cached item is valid for sure without asking again. (When expired by this rule then the browser will make a conditional GET ...)
所以在您的情况下:
- 浏览器将资源缓存一年.在这一年内,根本没有任何对此资源的请求.它是直接从本地缓存中提供的. (使用
Cache-Control
标头设置.) - 浏览器在一年到期后会执行有条件的请求,以检查是否有任何更改.如果没有任何更改,服务器将以
HTTP 304
响应并显示为空.在这种情况下,浏览器将继续使用其缓存的项目,而无需重新传输. (使用ETag
和/或Last-Modified
标头设置)
- Browser is caching the resource for one year. Within that year no request for this resource is done at all. It's directly served from local cache. (uses
Cache-Control
header settings.) - Browser does conditional Request after one year expired to check if something changed. The server responds with
HTTP 304
and empty body when nothing changed. The browser continues to use its cached item in that case without the need of retransmission. (usesETag
and/orLast-Modified
header settings)
(浏览器可能会或可能不会尊重您的数据.例如,即使一年还没有到期,浏览器也可能会执行条件请求.)
(The browser may or may not respect your data. For example it is possible that a browser will do a conditional request even when one year has not been expired yet.)
对于高度优化的站点,Cache-Control
更为重要,因为您将其设置为将来的过期标头,并只需更改资源的URL(以防万一).尽管这阻止了使用条件请求,但它使您能够在定义expires标头时表现出极大的进取心,同时又可以同时向每个人立即提供资源的新版本.这是因为有了新的URL,它似乎是浏览器视图中的新资源.
For highly optimized sites the Cache-Control
is far more important, because you set it faaaar future expire headers and simply change the URL for the resource in case it changed. While this prevents the use of conditional Requests it gives you the ability to be extremly aggressive when defining the expires header while being able to serve new versions of the resource immediatly to everybody at the same time. This is because of the new URL it seems to be a new resource in browser's view.
对于Java,存在一个名为 jawr 的框架,该框架利用了这些概念以及其他概念您的网站开发.
For Java there exists a framework called jawr which makes use of these and other concepts without having negative impact to your site development.
这篇关于为什么即使设置了缓存标头,Google Pagespeed为何仍要求指定ETag的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!