不同类型资源的理想 HTTP 缓存控制标头
我想找到一组最小的标头,适用于“所有”缓存和浏览器(也适用于使用 HTTPS!)
在我的网站上,我将拥有三种资源:
(1)永久可缓存(公共/对所有用户均等)
示例:0A470E87CC58EE133616F402B5DDFE1C.cache.html (由 GWT 自动生成)
当这些文件更改内容时(基于 MD5),它们会自动分配一个新名称。
即使使用 HTTPS,它们也应该被尽可能多地缓存(所以我认为,我应该设置
Cache-Control: public
,特别是对于 Firefox?)他们不应该要求客户端进行一轮循环-如果内容已更改,则访问服务器进行验证。
(2) 偶尔更改(公共/对所有用户均等)
示例:index.html、mymodule.nocache.js
当新版本的站点已部署。
它们可以被缓存,但可能每次都需要往返重新验证。
它们可以被缓存
(3) 每个请求单独(私有/用户特定)
示例:JSON 响应
- 在任何情况下,这些资源都不应以未加密的方式缓存到磁盘。 (除非我有一些可以缓存的特定请求。)
我对每种类型可能使用哪些标头有一个总体想法,但总有一些东西我可能会丢失。
I want to find a minimal set of headers, that work with "all" caches and browsers (also when using HTTPS!)
On my web site, I'll have three kinds of resources:
(1) Forever cacheable (public / equal for all users)
Example: 0A470E87CC58EE133616F402B5DDFE1C.cache.html (auto generated by GWT)
These files are automatically assigned a new name, when they change content (based on the MD5).
They should get cached as much as possible, even when using HTTPS (so I assume, I should set
Cache-Control: public
, especially for Firefox?)They shouldn't require the client to make a round-trip to the server to validate, if the content has changed.
(2) Changing occasionally (public / equal for all users)
Examples: index.html, mymodule.nocache.js
These files change their content without changing the URL, when a new version of the site is deployed.
They can be cached, but probably need a round-trip to be revalidated every time.
(3) Individual for each request (private / user specific)
Example: JSON responses
- These resources should never be cached unencrypted to disk under no circumstances. (Except maybe I'll have a few specific requests that could be cached.)
I have a general idea on which headers I would probably use for each type, but there's always something I could be missing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可能会使用这些设置:
Cache-Control: max-age=31556926
– 表示可以由任何缓存进行缓存。缓存的表示被认为是新鲜的一年:<块引用>
要将响应标记为“永不过期”,源服务器会发送一个
到期日期约为响应发出后一年
发送。 HTTP/1.1 服务器不应发送超过一个的过期日期
未来一年。
Cache-Control: no-cache
– 允许任何缓存对表示进行缓存。但缓存必须在释放缓存副本之前将请求提交到源服务器进行验证。Cache-Control: no-store
– 缓存在任何情况下都不得缓存表示。有关详细信息,请参阅 Mark Nottingham 的缓存教程。
I would probably use these settings:
Cache-Control: max-age=31556926
– Representations may be cached by any cache. The cached representation is to be considered fresh for 1 year:Cache-Control: no-cache
– Representations are allowed to be cached by any cache. But caches must submit the request to the origin server for validation before releasing a cached copy.Cache-Control: no-store
– Caches must not cache the representation under any condition.See Mark Nottingham’s Caching Tutorial for further information.
案例一和案例二实际上是同一个场景。
您应该设置
Cache-Control: public
,然后生成一个包含网站的内部版本号/版本的 URL,以便您拥有可能永远持续的不可变资源。您还希望将
Expires
标头设置为一年或更长时间,以便客户端不需要发出新鲜度检查。对于情况 3,您可以采取以下所有措施以获得最大的灵活性:
Cases one and two are actually the same scenario.
You should set
Cache-Control: public
and then generate a URL with includes the build number / version of the site so that you have immutable resources that could potentially last forever.You also want to set the
Expires
header a year or more in the future so that the client will not need to issue a freshness check.For case 3, you could all of the following for maximum flexibility: