ratemysite.ai

How to set Cache-Control and ETag headers for HTML

Cache-Control and ETag headers let crawlers revalidate pages cheaply, so they can check for changes more often. Send a Cache-Control header with a sensible max-age for your HTML pages, add an ETag validator so unchanged pages return a tiny 304, and use s-maxage to control the CDN cache.

By . Updated .

How do you fix it?

  1. Run curl -I https://yourdomain/page and read the Cache-Control, ETag and Last-Modified headers.
  2. Set Cache-Control: public, max-age=300 (or longer for static pages) on HTML responses.
  3. Make sure the server sends an ETag, so repeat requests with If-None-Match get a fast 304 response.
  4. At the CDN, use s-maxage and stale-while-revalidate so visitors and bots hit the edge cache.
  5. Re-run the audit and confirm the Cache headers check passes.

What are cache headers?

Cache headers are HTTP response headers that tell browsers, CDNs and crawlers how long a response may be reused and how to check whether it changed. Cache-Control sets the lifetime. An ETag is a fingerprint of the content: the client sends it back, and the server answers with a tiny 304 Not Modified when nothing changed. This lets a crawler re-check thousands of pages cheaply and makes it more willing to return often.

What values should I choose?

For HTML that changes a few times a week, Cache-Control: public, max-age=300, s-maxage=3600 is a good start. For static assets with hashed file names, use max-age=31536000 and immutable, which is one year. Avoid no-store on public pages, because it forces a full download on every request. After changing the headers, test with curl twice and confirm that the second request returns a 304 or a cache hit. Also check that logged-in or personalized pages use private or no-store, so no visitor ever sees another person's content from a shared cache.

How much does this check matter?

What else do people ask?

Is a long cache time dangerous for HTML?
It can delay updates. Use a short max-age such as 5 minutes with revalidation, and long lifetimes only for hashed static assets.
What is a 304 response?
It means the page has not changed since the copy the client holds, so the server sends only headers and saves bandwidth.

Sources