how to use control-cache headers?

前端 未结 4 1297
南旧
南旧 2021-01-04 19:43

I downloaded Google speed tracer for Google chrome to see how my site does performance wise and it tells me I need to enable caching for certain files like my style.css, ima

4条回答
  •  感情败类
    2021-01-04 20:20

    You need understand what these Cache-Control header field directives actually mean:

    • public:

      Indicates that the response MAY be cached by any cache, even if it would normally be non-cacheable or cacheable only within a non-shared cache. (See also Authorization, section 14.8, for additional details.)

    • max-age=3600 specified that the response is fresh for 3600 seconds:

      When the max-age cache-control directive is present in a cached response, the response is stale if its current age is greater than the age value given (in seconds) at the time of a new request for that resource. The max-age directive on a response implies that the response is cacheable (i.e., "public") unless some other, more restrictive cache directive is also present.

    • must-revalidate specifies that a cache must revalidate a cached response after it became stale before using that cached response to satisfy the request:

      When the must-revalidate directive is present in a response received by a cache, that cache MUST NOT use the entry after it becomes stale to respond to a subsequent request without first revalidating it with the origin server. (I.e., the cache MUST do an end-to-end revalidation every time, if, based solely on the origin server's Expires or max-age value, the cached response is stale.) […] Servers SHOULD send the must-revalidate directive if and only if failure to revalidate a request on the entity could result in incorrect operation, such as a silently unexecuted financial transaction.

    Now this is the intended meaning of these directives.

    But as always, the reality is a little different: Especially must-revalidate is not interpreted to only revalidate a cached response only after it became stale but that it has to be revalidated on every subsequent request or that it must not be cached at all (that’s probably why it sends Cache-Control: max-age=0 in the subsequent request).

    The “From Cache: false” seems to indicate that the response did not come from a cache but directly from the server.

提交回复
热议问题