I'm wondering how the browser determines whether a cached resource has expired or not.
Assume that I have set the max-age
header to 300. I made a request at 14:00, 3 minutes later I made another request to the same resource. So how can the browser tell the resource haven't expired (the current age
which is 180 is less than the max-age
)? Does the browser hold a "expiry date" or "current age" for every requested resource? If so how can I inspect the "current age" at the time I made the request?
Check what browsers store in their cache
To have a better understanding on how the browser cache works, check what the browsers actually store in their cache:
about:cache
.chrome://cache
.Note that there's a key for each cache entry (requested URL). Associated with the key, you will find the whole response details (status codes, headers and content). With those details, the browser is able to determine the age of a requested resource and whether it's expired or not.
The reference for HTTP caching
The RFC 7234, the current reference for caching in HTTP/1.1, tells you a good part of the story about how cache is supposed to work:
Some rules are defined regarding storing responses in caches:
Usually (but not always) the server providing the resource will provide a
Date
header, indicating the time at which that resource was requested. Caching entities can use thatDate
and the current time to find the resource's age. If theDate
response header does not appear, that the caching entity will probably mark the resource's request time in other metadata, and use that metadata for computing the age. Another possibly helpful response header to look for is theLast-Modified
response header.So first, you should check if the cached resource has the
Date
header for your own age calculation. If not present, it will then depend on which specific browser you are using, and how that browser handles caching forDate
-less resources. More information on HTTP caching and the various factors involved, can be found in this caching tutorial.Hope this helps!