When setting up caching for HTML responses, first consider whether the content is personalized and how long visitors can reasonably continue seeing old content after an update. According to web.dev, static HTML that does not vary by user or context can be cached briefly, with five minutes as a starting point—not a fixed standard for every page. Personalized HTML shown after login is usually best left uncached. To keep a cached copy while checking for updates, you can revalidate it using ETag or Last-Modified, though this still involves network latency.
Static HTML can be cached briefly, but may still reference old resources after deployment
HTML contains more than text: it also references resources such as CSS, JavaScript, and images. If those filenames include fingerprints that change with their content, old cached HTML may still point to outdated resources after a new version is deployed. For example, if stylesheet filenames change during deployment, old HTML may still reference the old files. A statically generated page is therefore not automatically suitable for long-term caching.
For static HTML that does not vary by context, web.dev suggests a five-minute cache lifetime as a safe starting point; the actual duration should still be adjusted to the content’s requirements. This advice assumes that the content is stable, not just that it has an HTML file extension. Caching and revalidation serve different purposes:
- Short-term caching can reduce the number of requests a CDN sends to the origin server.
- When a browser already has a cached copy, it can revalidate it to check for updates. If the content is unchanged, it does not need to download the entire page again.
For personalized pages, first consider leaving them uncached
If HTML varies by user identity, as with pages shown to authenticated users, the approach for static pages above should not be applied directly. For security and to keep content up to date, web.dev generally recommends avoiding caching this type of HTML entirely.
Another limitation to keep in mind is that once HTML has been cached in a user’s browser, the website cannot directly invalidate that existing cached copy. An update on the server therefore does not guarantee that the browser will stop using old content. Determine whether the content is personalized before deciding how long to cache it.
Revalidation saves bandwidth, but still requires requests
If you decide to keep a cached copy but want to check carefully for updates, you can revalidate it using ETag or Last-Modified. An ETag identifies a resource version and must be updated when the content changes. The browser subsequently sends that value back through If-None-Match. If it matches the version on the server, the server can return 304 Not Modified, allowing the browser to use its existing cached copy. Last-Modified instead uses the date and time of the resource’s last update for validation.
A 304 response is much smaller than the full HTML, but checking the version still requires a network round trip. When choosing revalidation, weigh the bandwidth savings against the work of maintaining validation information and the time spent waiting for a response. If the benefits do not justify these extra costs, leaving HTML uncached is also an option.
Caching HTML responses: perspectives from across the web
How should CDN caching be adjusted when content changes frequently?
The CDN cache time to live (TTL) can be shortened based on how frequently content changes, reducing the chance that visitors receive outdated copies. The duration should reflect the acceptable delay before updated content becomes visible. After major updates, old content can also be manually purged from the CDN as needed. However, purging the CDN cache does not clear copies already cached in users' browsers; the two must be considered separately.
Can public and private pages on the same website share CDN caching rules?
A single rule should not be applied indiscriminately. Rules can distinguish public from private content: public pages may be cached according to their update requirements, while restricted user data may need to remain uncached. If HTML varies by authenticated user, web.dev generally recommends avoiding caching entirely for security and freshness. File type alone should not determine whether content is cached.
How can you check what caching information a response actually contains?
Inspect and save the complete HTTP response headers, checking for information related to caching, cookies, or language rather than relying only on the few fields a tool captures by default. If the website uses additional caching features, test each one against the site's actual setup. Experiences shared in forums indicate that differences in website implementation can lead to different results, so those findings should not be treated as universal rules.
References
- General HTML performance considerationsweb.dev — Discusses: How should CDN caching be adjusted when content changes frequently?; Can public and private pages on the same website share CDN caching rules?
- Screaming Frog SEO Spider Update – Version 14.0www.screamingfrog.co.uk · 2020-11-23T09:33:00+00:00 — Discusses: How can you check what caching information a response actually contains?
- 小網站到底需不需要cloudflarewww.mobile01.com — Discusses: How can you check what caching information a response actually contains?
- Content Delivery Networks (CDNs): A Beginner’s Guidewww.semrush.com · 2021-09-14T19:46:00Z — Discusses: How should CDN caching be adjusted when content changes frequently?; Can public and private pages on the same website share CDN caching rules?
