适用于不同类型资源的理想 HTTP 缓存控制标头 [英] Ideal HTTP cache control headers for different types of resources

查看:10
本文介绍了适用于不同类型资源的理想 HTTP 缓存控制标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到一组最小的标头,它们适用于所有"缓存和浏览器(在使用 HTTPS 时也是如此!)

在我的网站上,我将拥有三种资源:

(1) 永久可缓存(公共/对所有用户均等)

示例:0A470E87CC58EE133616F402B5DDFE1C.cache.html(自动生成GWT)

  • 这些文件在更改内容时会自动分配一个新名称(基于 MD5).

  • 即使使用 HTTPS,它们也应该尽可能地被缓存(所以我假设,我应该设置 Cache-Control: public,尤其是对于 Firefox?)

  • 如果内容发生了变化,它们不应要求客户端往返于服务器进行验证.

(2) 偶尔更改(公共/所有用户平等)

示例:index.html、mymodule.nocache.js

  • 在部署新版本的网站时,这些文件会更改其内容而不更改 URL.

  • 它们可以被缓存,但可能需要每次来回重新验证.

(3) 每个请求的个体(私人/用户特定)

示例:JSON 响应

  • 在任何情况下都不应将这些资源未加密地缓存到磁盘中.(也许我会有一些可以缓存的特定请求.)
<小时>

我大致了解每种类型可能使用哪些标头,但总有一些我可能会遗漏的东西.

解决方案

我可能会使用这些设置:

  1. Cache-Control: max-age=31556926 – 表示可以被任何缓存缓存.缓存的表示将被视为新鲜 1 年:<块引用>

    为了将响应标记为永不过期",源服务器发送一个Expires 日期从响应时间算起大约一年发送.HTTP/1.1 服务器不应发送超过一个 Expires 日期未来一年.

  2. Cache-Control: no-cache – 表示可以被任何缓存缓存.但是缓存必须在释放缓存副本之前将请求提交给源服务器进行验证.
  3. Cache-Control: no-store – 缓存在任何情况下都不得缓存表示.

有关详细信息,请参阅 Mark Nottingham 的 缓存教程.

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.

解决方案

I would probably use these settings:

  1. Cache-Control: max-age=31556926 – Representations may be cached by any cache. The cached representation is to be considered fresh for 1 year:

    To mark a response as "never expires," an origin server sends an Expires date approximately one year from the time the response is sent. HTTP/1.1 servers SHOULD NOT send Expires dates more than one year in the future.

  2. 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.
  3. Cache-Control: no-store – Caches must not cache the representation under any condition.

See Mark Nottingham’s Caching Tutorial for further information.

这篇关于适用于不同类型资源的理想 HTTP 缓存控制标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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