Search code examples
http-headerscache-control

what does setting Cache-Control: max-age=20 in request header do?


When i set Cache-Control: 'max-age=0' in request header,this request will revalidate its cache entry (eg. with the If-Not-Modified header) all the way to the origin server. If the reply is then 304 (Not Modified), the cached entity can be used.

When i set Cache-Control: 'max-age=20' in response header,It,s mean is whilein 20 s , Request at the same address will use the cache.

But if i set Cache-Control: 'max-age=20' in request header,It doesn't work as well as the former,So i want to know what will happen when i set max-age=xxx(xxx != 0) in request header?


Solution

  • The specification tells us:

    The max-age request directive indicates that the client is unwilling to accept a response whose age is greater than the specified number of seconds.

    So max-age=20 means that the cache shouldn't return a cached response older than 20 seconds.

    The key difference here is that the client is choosing the desired freshness time of the resource, while in the usual case it's the server determining that.