Search code examples
httpcachingbrowserbrowser-cache

How browser cache determine that request is same or not?


I understand that the browser save the response, to return quickly at next same request.
But the question here is, what is same request? Does it mean that same resource uri in the case of Get request?

What about the values ​​of headers included in the request or the content of the body?

I've seen that the Cache-Control header is also used for Post requests.

If so, is the standard for the same Post request determined by comparing the contents of the body?


Solution

  • RFC 7234 defines the cache key:

    The primary cache key consists of the request method and target URI.... If a request target is subject to content negotiation, its cache entry might consist of multiple stored responses, each differentiated by a secondary key for the values of the original request's selecting header fields.

    The secondary key uses the headers listed in the Vary response header. See section 4.1 for more detail.

    When a cache receives a request that can be satisfied by a stored response that has a Vary header field, it MUST NOT use that response unless all of the selecting header fields nominated by the Vary header field match in both the original request (i.e., that associated with the stored response), and the presented request.

    To answer your specific questions:

    But the question here is, what is the same request? Does it mean the same resource URI in the case of a GET request?

    Yes.

    What about the values ​​of headers included in the request or the content of the body?

    The content of the body doesn't matter, and the headers only matter if they're listed in the Vary header.

    I've seen that the Cache-Control header is also used for POST requests. If so, is the standard for the same Post request determined by comparing the contents of the body?

    Since the primary cache key consists of the URI and the method, two POST requests will have the same key if they have the same URI. However, as the standard notes, "since HTTP caches in common use today are typically limited to caching responses to GET, many caches simply decline other methods and use only the URI as the primary cache key."