S
S
SagePtr2021-08-13 21:34:03
caching
SagePtr, 2021-08-13 21:34:03

Am I using the Cache-Control header correctly?

The caching scheme is as follows: a certain resource should be cached at the request of the browser, but at the same time, with each new page opening, it should be revalidated on the server and receive a 200 or 304 header (depending on the presence in the cache). Also, every 10 minutes it twitches in the background via XMLHttpRequest and is updated on the page in order to avoid open god knows when tabs are open.
Is a simple header sufficient for this purpose:
Cache-Control: no-cache?
No need to additionally add directives like max-age or must-revalidate there, separated by commas? As the documentation
says :

no-cache
The response may be stored by any cache, even if the response is normally non-cacheable. However, the stored response MUST always go through validation with the origin server first before using it, therefore, you cannot use no-cache in-conjunction with immutable. If you mean to not store the response in any cache, use no-store instead. This directive is not effective in preventing caches from storing your response.

A cursory check in the main browsers (Chrome, Firefox and IE11) showed that only no-cache without must-revalidate is enough to specify (provided that before that the resource was not shown anywhere with other caching headers and was not shown in caches), but there is Are there any nuances or pitfalls in which this scheme can fail? (provided that the resource has Last-Modified and ETag, which change correctly every time the resource is changed on the server side). Support for browsers older than 5 years can be neglected.

UPD:
If the server is behind Cloudflare, then in this case the Cloudflare cache is triggered, which has its own caching policy (at least on free accounts). In such cases, to bypass it, you can use:
Cache-Control: private, no-cache

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question