Does it make sense to set Cache-Control max-age=0 and s-maxage= not zero?

1.4k views Asked by At

Somebody commented on this question about caching:

...using a Cache-Control value of: max-age=0, s-maxage=604800 seems to get my desired behavior of instant client updates on new page contents, but still caching at the CDN level

Will I really get caching at CDN level and instant updates for my users?

Does it make sense? How does that combination work?

1

There are 1 answers

1
cbdeveloper On BEST ANSWER

Yes, it makes sense.

With the configuration mentioned in that comment, your users will get instant stale responses, so they'll have to verify it the next time they make a resquest. And the CDN will cache a valid response for 604800 seconds. So repeated requests will be mostly served by CDN, instead of the Origin server.

But what if you update your app? What happens to the stale cache on the CDN?

After a new deployment, you need to make sure all of your stale cache from the CDN will be purged / cleared.

For example, see Purging cached resources from Cloudflare: it gives you numerous options on how to do that.

  • Purge by single-file (by URL)
  • Purging by single-file through your Cloudflare dashboard
  • Purge everything
  • Purge cached resources through the API
  • etc

Firebase Hosting, for example, will clear all CDN cache after a new deployment:

Any requested static content is automatically cached on the CDN. If you redeploy your site's content, Firebase Hosting automatically clears all your cached static content across the CDN until the next request.

As far as the setting suggested in the comment, I think Cache-Control: no-cache would do a better job.

From MDN - Cache Control:

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.