MagentaManatee
MagentaManatee

Reputation: 11

How can I make the browser keep the cache for n days but revalidate every m hours via Etag?

I tried to understand this thoroughly, but so far, while it appears to be a very reasonable and common performance and efficiency use case, it seemingly cannot be achieved with HTTP headers at all.

I tried reading different answers here, the documentation or spec (which seems to be differently implemented by browsers), and doing my own tests.

But the different caching headers apparently cause different browsers to do different things, take different priorities, or don't even work at all, it appears.

Here is my setup: I am sending images with Cache-Control: max-age=864000, public header, and an Etag header.

It would seem from what I read, that this and the presence of the Etag should be enough for the browser to revalidate every time.

But it doesn't. It just loads from disk cache and does not even send a request to the server including the Etag.

In one of the answers it said must-revalidate is not necessary, the presence of the Etag causes the browser to revalidate already. I cannot confirm. I also tried Cache-Control: max-age=864000, must-revalidate, public but same unsuccessful result: it does not revalidate for 10 days.

What I want to achieve: store and use the image in the browser cache for 10 days, but revalidate say every 12 hours.

What would the correct combination of headers be?

Upvotes: 1

Views: 195

Answers (1)

Kevin Christopher Henry
Kevin Christopher Henry

Reputation: 48952

The headers you want are Cache-Control: max-age=43200 (12 hours) and an ETag.

Note that you don't have the ability to tell the browser how long to store the resource (other than no-store). What matters in caching is the freshness of the resource displayed to the user; whether it comes from a revalidated stored resource or a fresh request doesn't really matter since the ETag guarantees that the resource is the current one.

I am sending images with Cache-Control: max-age=864000, public header, and an ETag header. It would seem from what I read, that this and the presence of the ETag should be enough for the browser to revalidate every time.

No, the max-age is telling the browser it doesn't have to revalidate for 10 days.

Upvotes: 1

Related Questions