The best `Cache-Control` strategy

1116 days ago, 0 views.

A wild tweet from appeared on my timeline:

What's faster than a static HTML file?

An HTML page with `Cache-Control: public, max-age=60`. Browsers can use a local cache instead of requesting the page again.

But "instant cache invalidation" (jamstack "best practice") makes it impossible to cache HTML. So it's slower.

— MICHAEL JACKSON (@mjackson) March 29, 2021

I can see the @mjackson’s point there: A local cache copy is better than nothing, and caching it for a while (in this case, one minute) is okay for most static web assets scenarios.

However, what happens when your content tends to change, even if it is done in an infrequent way?

In that case, the strategy could be complemented very well must-revalidate:

Cache-Control: max-age=60, stale-while-revalidate=0

In that way, the cached copy will always be used while the data is being validated in the background, saving the user to be faced with a cache MISS and refreshing the cache copy as soon as possible.

Bibliography

Kiko Beats

Kiko Beats