Track 02 · Lesson 14 of 17Inside the browser

Track 02 · Inside the browser

HTTP caching

Lesson 14 of 17· 8 min

The fastest request is the one never made. The Cache-Control header tells browsers and CDNs how long they may reuse a file, and an ETag lets them ask "has it changed?" cheaply. Load the page, let time pass, deploy a new version, and see what users get.

Deploy and see what users get

Try “The classic bug” first

Common setups

File name

Same name every release.

Cache-Control

Server has v1 as app.js. Browser cache: empty. Clock: 0 min.

The words that matter#

  • max-age=N: reuse this copy for N seconds without asking.
  • no-cache: keep a copy, but check with the server before every use. Despite the name, it caches.
  • no-store: never keep a copy at all.
  • immutable: this file will never change under this name, so don't even check.
  • ETag: a fingerprint of the file. The browser sends it back in If-None-Match, and if nothing changed the server answers 304 Not Modified with no body.

Why hashed file names win#

A file called app.js that's cached for a year will stay stuck on the old version after you deploy. Put a hash of the content in the name, app.3f9a2c.js, and every new version is a brand-new URL. The HTML points to the new name, so the old file can sit in caches forever without harming anyone. Next.js and Vite do this for you.

Check yourself

What's the best Cache-Control for app.3f9a2c.js, whose name changes with its content?