Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cache a HTML page with must-revalidate?

When caching a HTML page with must-revalidate, this means that browser must check for any update defined by Last-Modified or Etag. However, the problem is that before max-age, browser will not make any connection with the website to read HTTP headers (to analyze Last-Modified and Etag)?

How to force the browser to make a brief connection to read (at least) HTTP readers before loading the page from cache?

I do not understand the usage of must-revalidate! Doesn't it its responsibility to check for updates before max-age? because after reaching max-age, browser will read from the website and never use local cache.

like image 944
Googlebot Avatar asked Apr 03 '12 11:04

Googlebot


People also ask

How do I revalidate cache?

The must-revalidate response directive indicates that the response can be stored in caches and can be reused while fresh. If the response becomes stale, it must be validated with the origin server before reuse. Typically, must-revalidate is used with max-age .

Should HTML be cached?

Do not cache HTML in the browser. Always set cache-control: no-store, no-cache before sending HTML response to the client-side. Embed fingerprints in the URL of static resources like image, JS, CSS, and font files. Safely cache static resources, i.e., images, JS, CSS, font files for a longer duration like six months.

How do you set Cache-Control max-age in HTML?

Cache-Control: max-age=<seconds> This directive tells the browser or intermediary cache how long the response can be used from the time it was requested. A max-age of 3600 means the response can be used for the next 60 minutes before it needs to fetch a new response from the origin server.

Where do I put Cache-Control in HTML?

To use cache-control in HTML, you use the meta tag, e.g. The value in the content field is defined as one of the four values below. HTTP 1.1. Allowed values = PUBLIC | PRIVATE | NO-CACHE | NO-STORE.


1 Answers

Yes, your understanding of must-revalidate is wrong: it says that the cache may not serve this content when it is stale (i.e. "expired"), but must revalidate before that. Yes, caches (and browsers) can in theory be set to serve pages even if they are stale, though the standard says they should warn the user if they do this.

To force the browser to recheck your page with the server, the simplest solution is to add max-age=0 to the Cache-Control header. This will let your browser keep a copy of the page in its cache, but compare it with the server's version by sending the contents of Last-Modified or ETag, as you wanted.

It used to be that you could add no-cache instead, but as users have been expecting this to behave as no-store, browsers are gradually treating them the same.

Check the HTTP/1.1 RFC, section 14.9 for more information on the headers.

like image 102
Ben Deutsch Avatar answered Oct 05 '22 06:10

Ben Deutsch