Cache-Control HTTP Headers 优先级

Cache-Control HTTP Headers precedence

我读过这篇关于 Cache-Control HTTP Headers 的优秀文章:https://www.mnot.net/cache_docs/#CACHE-CONTROL

我想知道以下 header 会发生什么:

Cache-Control: no-store, public

public header 会优先于 no-store header 还是相反?

优先的 header 会因浏览器而异吗?

我知道同时拥有 no-store 和 public Cache-Control header 可能是不可取的,但为了争论,如果他们都存在会发生什么.

提前感谢您的指导。

通过the RFC 7234。鉴于:

Cache-Control: no-store, public

no-store 是响应 Cache-Control 指令 (RFC 7234, Section 5.2.2)。它指出不应存储响应。

public 是一个 扩展 .

The Cache-Control header field can be extended through the use of one or more cache-extension tokens, each with an optional value. A cache MUST ignore unrecognized cache directives.

由于 public 不是 no-store 的已知扩展,因此将被忽略。

从下面的 Google Chrome Browser 中找到关于这个问题的一些主要代码。

isPubliclyCacheable: function(resource)
{
    if (this._isExplicitlyNonCacheable(resource))
        return false;

    if (this.responseHeaderMatch(resource, "Cache-Control", "public"))
        return true;

    return resource.url.indexOf("?") == -1 && !this.responseHeaderMatch(resource, "Cache-Control", "private");
}


_isExplicitlyNonCacheable: function(resource)
{
    var hasExplicitExp = this.hasExplicitExpiration(resource);
    return this.responseHeaderMatch(resource, "Cache-Control", "(no-cache|no-store|must-revalidate)") ||
        this.responseHeaderMatch(resource, "Pragma", "no-cache") ||
        (hasExplicitExp && !this.freshnessLifetimeGreaterThan(resource, 0)) ||
        (!hasExplicitExp && resource.url && resource.url.indexOf("?") >= 0) ||
        (!hasExplicitExp && !this.isCacheableResource(resource));
}

根据代码,某些指令比其他指令具有更高的优先级,"no-store" 就在其中,因此在您的情况下(Cache-Control:"public, no-store" 或 "no-store, public" ) no-store 将获得更高的优先级。

我已经缩短了与您的用例相关的 HTTP 缓存规范部分(参见 https://www.rfc-editor.org/rfc/rfc7234#section-3):

A cache MUST NOT store a response to any request, unless:

the "no-store" cache directive (see Section 5.2) does not appear in request or response header fields, and

the response either:

contains a Cache Control Extension (see Section 5.2.3) that allows it to be cached, or contains a public response directive (see Section 5.2.2.5).

contains a public response directive (see Section 5.2.2.5).

简而言之,no-store 优先于 public