HTTP cache
validation
Give browsers a way to check whether a saved response is still current before they download it again.
Test a URLGET /manual.css HTTP/1.1
Host: example.com
If-None-Match: "style-v7"
HTTP/1.1 304 Not Modified
ETag: "style-v7"
Cache-Control: no-cache
How it works
- Store the response
- The first request receives a 200 response, the file, and an
ETag. A cache can save the body and its tag if the cache policy permits storage. - Send the saved tag
- When the cache revalidates the file, the next request includes the tag in
If-None-Match. - Use the result
- A matching tag lets the server return 304 with no body. The cache can use its saved body. A changed file normally returns 200 with the new body and tag.
Test a URL
Replace the URL with an asset you serve. Run the first command, then copy the complete ETag value, including quotation marks, into the second request.
curl -sS -D - -o /dev/null 'https://example.com/manual.css'
curl -sS -D - -o /dev/null -H 'If-None-Match: "style-v7"' \
'https://example.com/manual.css'
With an unchanged asset and the same request headers, look for 304 in the second response. If it returns 200, compare the tags and check whether a CDN or reverse proxy changes the response to either request.
Choose a cache policy
no-cache- Allows storage, then requires successful validation before reuse. Use it for files that can change at the same URL.
no-store- Tells caches to skip storage. Use it for responses that should never be saved in an HTTP cache.
max-age- Sets the freshness lifetime in seconds. Give fingerprinted assets a long lifetime and change their URLs when the contents change.