Module 4 · Caching and performanceLesson 40 of 886 minBeginner

Browser caching

The fastest request is the one never sent. HTTP headers tell browsers what they can reuse and for how long.

Try it

Step through the four visits. Watch the time and the amount downloaded fall to zero, then see what changes when the file is updated.

Browsers keep a cache of files they've downloaded. Servers control it with HTTP headers.

Cache-Control

The main header:

  • Cache-Control: max-age=3600: reuse for an hour without asking.
  • Cache-Control: no-cache: you may store it, but check with the server before each use.
  • Cache-Control: no-store: don't store it at all (for sensitive data).
  • private vs public: whether shared caches like CDNs may store it, or only the user's browser.
  • immutable: this file will never change, don't even revalidate.

Revalidation

When a cached file expires, the browser doesn't need to download it again blindly. It asks:

  • ETag: a fingerprint of the content. The browser sends If-None-Match: "v7".
  • Last-Modified: a date. The browser sends If-Modified-Since.

If nothing changed, the server replies 304 Not Modified with no body: one quick round trip, almost no data.

The recipe most sites use

  1. Static assets (JS, CSS, images) get a content hash in their file name, like app.3f9a1c.js, and Cache-Control: public, max-age=31536000, immutable. A new version gets a new name, so you never need to invalidate.
  2. HTML pages get a short max-age or no-cache, so users always pick up new asset names quickly.
  3. API responses with personal data use private or no-store.

Why it matters for design

Browser caching removes requests entirely: less bandwidth, less server load, instant repeat visits. It's free performance, and it's also a source of "why do users still see the old version?" bugs when headers are wrong.

Quick check

What does a 304 Not Modified response mean?