设置 Cache-Control max-age=0 和 s-maxage= 不为零有意义吗?

Does it make sense to set Cache-Control max-age=0 and s-maxage= not zero?

有人评论 关于缓存:

...using a Cache-Control value of: max-age=0, s-maxage=604800 seems to get my desired behavior of instant client updates on new page contents, but still caching at the CDN level

我真的会在 CDN 级别获得缓存并为我的用户即时更新吗?

有意义吗?该组合如何工作?

是的,有道理。

使用该评论中提到的配置,您的用户将得到 instant 过时的响应,因此他们在下次发出请求时必须验证它。 CDN 会将有效响应缓存 604800 秒。所以重复的请求将主要由 CDN 提供服务,而不是 Origin 服务器。

但是如果您更新您的应用程序呢? CDN 上的陈旧缓存会怎样?

新部署后,您需要确保清除/清除来自 CDN 的所有陈旧缓存。

例如,请参阅 Purging cached resources from Cloudflare:它为您提供了多种选择。

  • 单个文件清除(URL)
  • 通过您的 Cloudflare 仪表板按单个文件清除
  • 清除一切
  • 通过API
  • 清除缓存资源
  • 等等

Firebase Hosting,比如新部署后会清空所有CDN缓存:

Any requested static content is automatically cached on the CDN. If you redeploy your site's content, Firebase Hosting automatically clears all your cached static content across the CDN until the next request.

就评论中建议的设置而言,我认为 Cache-Control: no-cache 会做得更好。

来自MDN - Cache Control

no-cache

The response may be stored by any cache, even if the response is normally non-cacheable. However, the stored response MUST always go through validation with the origin server first before using it, therefore, you cannot use no-cache in-conjunction with immutable. If you mean to not store the response in any cache, use no-store instead. This directive is not effective in preventing caches from storing your response.