'cache-control: public'真的有什么作用吗?

Does 'cache-control: public' actually have any effect?

任何已知缓存对 cache-control: public, max-age=60 的处理方式是否与 cache-control: max-age=60 不同?

我一直在努力验证它,但我假设如果响应中存在任何 cache-control 指令,则假设该响应可由浏览器和任何中间缓存缓存 除非 cache-control: private 已设置。

这是否意味着 cache-control: public 是多余的?这不是你会得到的行为吗?

仔细阅读 MDN,我想我找到了自己问题的答案。

TL;DR: cache-control: public 将显式覆盖哪些类型的响应被认为是可缓存的默认规则,因此不应轻易使用。许多响应通常不应该被缓存——例如POSTs 或 302 重定向。请参阅下面的完整规则集。

来自the cache-control page

public The response may be stored by any cache, even if the response is normally non-cacheable (emphasis mine).

那么“可缓存”是什么意思?来自 the "cacheable" page on the MDN glossary:

A cacheable response is an HTTP response that can be cached, that is stored to be retrieved and used later, saving a new request to the server. Not all HTTP responses can be cached, these are the following constraints for an HTTP response to be cached:

  • The method used in the request is itself cacheable, that is either a GET or a HEAD method. A response to a POST or PATCH request can also be cached if freshness is indicated and the Content-Location header is set, but this is rarely implemented. (For example, Firefox does not support it per https://bugzilla.mozilla.org/show_bug.cgi?id=109553.) Other methods, like PUT or DELETE are not cacheable and their result cannot be cached.
  • The status code of the response is known by the application caching, and it is considered cacheable. The following status code are cacheable: 200, 203, 204, 206, 300, 301, 404, 405, 410, 414, and 501.
  • There are (I assume this should be aren't) specific headers in the response, like Cache-Control, that prevents caching.

所以看起来只有当他们明确想要覆盖这些可缓存性规则时才应该使用 cache-control: public,这通常可能不是一个好主意。