Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 37 ยท Network and Caching

HTTP Caching Headers

Learn how browser and HTTP caching works, with Cache-Control, max-age, ETag, Last-Modified and immutable, plus a caching strategy for HTML, assets and APIs.

The fastest request is the one never made. HTTP caching lets the browser reuse a response it already has, so repeat visits and navigations skip the network entirely. Done well, it is the single largest speed-up for returning visitors, and it also lowers your server load.

You control caching with response headers. This lesson explains the important ones and gives a strategy you can apply to almost any site.

Two ideas: freshness and validation

  1. Freshness. While a cached copy is fresh, the browser uses it without asking the server.
  2. Validation. When the copy is stale, the browser can ask the server "has this changed?" If not, the server replies 304 Not Modified with no body, which is tiny.
flowchart TD
  A["Request"] --> B{"In cache?"}
  B -->|No| N["Fetch from network"]
  B -->|Yes| C{"Fresh?"}
  C -->|Yes| U["Use cache: no network"]
  C -->|No| V["Conditional request"]
  V --> W{"Changed?"}
  W -->|No| X["304: reuse cache"]
  W -->|Yes| Y["200 with new body"]

A cached copy is either used straight away or revalidated.

Cache-Control

Cache-Control is the main header. It takes comma-separated directives.

Directive Meaning
max-age=N Fresh for N seconds from the response time
s-maxage=N Like max-age but only for shared caches (CDNs)
no-cache May store it, but must revalidate before every use
no-store Do not keep it at all (sensitive data)
private Only the user's browser may cache it, not shared caches
public Any cache may store it
immutable Will never change during its freshness lifetime; skip revalidation on reload
stale-while-revalidate=N Serve a stale copy for N seconds while refreshing in the background

A common mistake is thinking no-cache means "do not cache". It means "cache but always check first". no-store is the one that prevents storage.

HTTP
Cache-Control: public, max-age=31536000, immutable

That header says: any cache may keep this for a year, and it will never change.

Validators: ETag and Last-Modified

For revalidation the server sends a validator with the first response:

  • ETag: "abc123": an identifier of the content version.
  • Last-Modified: Tue, 03 Jun 2025 10:00:00 GMT: when it last changed.

On the next request the browser sends If-None-Match: "abc123" or If-Modified-Since: .... If the content is unchanged, the server answers 304 with no body.

Simulating the decision

This is a stand-in for what the browser does, so you can try different headers.

JS Is this response fresh?

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Strategy: fingerprint assets

You cannot reliably change a file at a given URL if browsers have cached it for a year. The solution is cache busting by name: put a hash of the content in the file name (app.3f9a1c.js). When the file changes, the name changes, so it is a new URL and caches cannot serve the old version.

Resource Example header Why
Fingerprinted CSS/JS/images/fonts public, max-age=31536000, immutable Never changes at that URL
HTML pages no-cache (or max-age=0, must-revalidate) Always get the latest references to the hashed files
Public API data that can be a bit stale max-age=60, stale-while-revalidate=300 Fast and fresh enough
Personalised or sensitive pages private, no-store Never share or keep

Build tools such as Vite, webpack and others create hashed file names for you. The HTML (not hashed) tells the browser which hashed files to use, so it must not be cached for long.

Do not give HTML a long max-age. Visitors would keep an old page pointing at old asset names, and you could not fix a bug until their cache expired.

Checking headers in the browser

In DevTools, open the Network panel, click a request and look at Response Headers. A request served from cache shows (disk cache) or (memory cache) in the Size column, and a validated one shows status 304.

Read the caching info the browser exposes

For cross-origin files, transferSize is hidden unless the server sends a Timing-Allow-Origin header.

Other levels of cache

A response may be cached in several places: the browser's memory cache, its disk cache, a service worker's Cache Storage, a CDN edge and your own server cache (see CDN and edge caching). Headers apply to the HTTP caches; a service worker follows whatever code you wrote.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does Cache-Control: no-cache do?

    no-cache means store it but check with the server each time. no-store prevents storage.
  2. 2. Which header is right for a fingerprinted app.3f9a1c.js?

    A hashed name never changes content, so it can be cached for a year.
  3. 3. What status code means the cached copy is still valid?

    304 Not Modified tells the browser to reuse its stored body.
  4. 4. Why should HTML get a short or no-cache policy?

    Stale HTML would keep referring to old assets and delay your fixes.

Summary

  • Fresh cached responses need no network; stale ones can be validated with a tiny 304.
  • no-cache revalidates, no-store forbids storing, max-age sets freshness.
  • Fingerprint static assets and cache them for a year with immutable; keep HTML revalidating.
  • Use stale-while-revalidate for data that can be briefly stale.

Next: CDN and Edge Caching