HTTP Caching
Build an accurate mental model of what the browser does, from navigation and HTTP through parsing, rendering, storage, workers, security, and DevTools, so you can predict behavior and debug real pages.
What you will be able to do
#- Apply Cache-Control directives correctly.
- Explain revalidation with ETag and Last-Modified.
- Describe how CDN caches interact with browser caches.
Overview
#Caching avoids refetching resources that have not changed.
Freshness
Cache-Control: max-age=31536000, immutable tells the browser a resource is fresh for a year and will never change. Hashed asset filenames make this safe.
Revalidation
When a cached response is stale, the browser sends a conditional request with If-None-Match (from ETag) or If-Modified-Since (from Last-Modified). A 304 Not Modified response reuses the cached body.
Directives worth knowing
no-store: never cache.no-cache: cache but always revalidate.private: only the browser may cache, not shared caches.stale-while-revalidate: serve stale while refreshing in the background.Vary: cache variants keyed on a header such asAccept-Encoding.
CDN interaction
A CDN caches responses at the edge and respects Cache-Control and s-maxage. s-maxage overrides max-age for shared caches. A cache-busting query string or hashed filename forces a new fetch.
Questions to carry forward
- What is the difference between no-store and no-cache?
- How does s-maxage differ from max-age?
- Why use hashed filenames with immutable?
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
Which directive means the response may be cached but must always be revalidated before use?
Exercise 2Implement
Write the response header for a hashed, content-addressed asset that never changes.
starter.text// asset path: /assets/app.8f3a2c.jsReveal the reference answer
reference.textCache-Control: public, max-age=31536000, immutableAuthored expected output: A long-lived immutable cache header.
The hash in the filename changes when content changes, so the file can be cached for a year and marked immutable.
Common misconceptions
#- no-cache means do not cache.
- max-age applies to shared caches the same as private caches.
- ETag and Last-Modified are the same mechanism.
Follow-up practice
#- browser-internals-service-workers
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.