Skip to the page

Loading fast: critical path, hints, images, fonts, caching · mid

Caching and CDNs for speed

The fastest request is the one you do not make: give content-hashed static files a one-year immutable cache, make HTML revalidate, serve everything from a CDN near the user, and use a service worker only when you need offline or app-shell behaviour.

The notes

What you must know

  • Hashed assets (app.3f9a1c.js): Cache-Control: public, max-age=31536000, immutable. A change makes a new file name, so a long cache is safe.
  • HTML and other un-hashed URLs: Cache-Control: no-cache (store, but check with the server each time; a 304 Not Modified is cheap) plus an ETag.
  • Personal data: private (browser only) or no-store. Forgetting private on personalised responses can leak data through a shared cache.
  • A CDN reduces TTFB and download time by serving from an edge close to the user. s-maxage sets the CDN lifetime separately from the browser's max-age; stale-while-revalidate hides revalidation time.
  • Repeat-visit speed also needs good compression (Brotli/zstd) and HTTP/2 or HTTP/3 connections.
  • Service worker caching (Cache API) is separate from the HTTP cache and is controlled by your code. Use it for offline and app shells, not as a replacement for correct headers.

The red pen

Common traps: mistake → fix

  • Mistake: "no-cache means don't cache"

    Fix: no-cache means store but revalidate before reuse; no-store means don't store.

  • Mistake: "Give index.html a long max-age"

    Fix: users keep the old HTML pointing to old bundles after a deploy.

  • Mistake: "No Cache-Control header means no caching"

    Fix: browsers then cache heuristically; always set headers explicitly.

Easy to hard

Questions interviewers ask, easy to hard

  1. How would you cache an SPA's index.html vs its hashed JS bundles?
  2. What does immutable add on top of max-age?
  3. How does a CDN improve LCP? What should not be cached on a CDN?
  4. After a deploy, some users see a blank page. What caching mistake could cause it?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview