Browser cache

On this page 7

A browser cache is the local storage in which a web browser keeps copies of previously downloaded resources, such as HTML documents, stylesheets, scripts, images and fonts, so that it can reuse them instead of requesting them from the network again. Among browser technologies, it is the primary mechanism that makes repeat visits faster, cheaper and less dependent on the network.

What the browser stores and for how long is controlled mainly by the server through HTTP caching headers, defined in the HTTP caching specification. The browser cache is therefore a shared responsibility: the browser applies the rules, but the website decides them.

How the browser cache works

When the browser needs a resource, it first looks for a stored copy keyed by URL and, in modern browsers, also by the top-level site. This cache partitioning prevents one site from detecting what another site has loaded. If a copy exists, the browser checks whether it is still fresh according to its caching headers.

A fresh copy is used immediately without any network request. A stale copy can still be reused if the server confirms it has not changed, through a conditional request that sends a validator. The server then answers with a short 304 Not Modified response instead of resending the whole file.

OutcomeNetwork activityTypical cost
Fresh hitNoneNear-instant from memory or disk
Revalidated hitConditional request, 304 responseOne round trip, tiny payload
MissFull request and downloadFull latency and transfer size
BypassedFull request, nothing storedUsed for sensitive or no-store content
Possible outcomes of a cache lookup

Browsers keep both a memory cache for resources used in the current session and a disk cache that survives restarts. Separately, the back/forward cache can keep a whole page in memory so that the back button restores it instantly.

HTTP headers that control caching

The Cache-Control header is the central tool for controlling the browser cache. Its directives define how long a response stays fresh, who may store it and when it must be revalidated.

Header or directiveMeaningTypical use
max-ageSeconds the response stays freshStatic assets, often one year
no-cacheStore, but revalidate before every useHTML documents
no-storeDo not store at allBanking pages, personal data
privateOnly the browser may store itUser-specific responses
immutableContent never changes during freshnessFingerprinted file names
stale-while-revalidateServe stale copy while updating in backgroundContent that tolerates brief staleness
ETagVersion identifier for validationConditional requests
Last-ModifiedDate-based validatorOlder or simpler setups
Key caching headers and directives

A common misunderstanding concerns no-cache, which does not prevent storage. It only forces revalidation. To prevent storing a response, the correct directive is no-store.

Browser cache compared with other caches and storage

The browser cache is one of several layers of caching between a user and a server. Each layer has different control and visibility, so performance problems require knowing which layer served a response.

LayerLocationControlled by
Browser HTTP cacheUser’s deviceHTTP headers
Service worker Cache APIUser’s device, per originJavaScript in a service worker
Back/forward cacheBrowser memoryBrowser heuristics and page behavior
CDN or proxy cacheNetwork edge serversHTTP headers and CDN configuration
Local storage and IndexedDBUser’s deviceApplication JavaScript
Caching layers and browser storage

The service worker Cache API is fully programmable and enables offline use, while the HTTP cache follows header rules automatically. Both obey the same-origin policy, so one site cannot read another site’s cached data.

Designing an effective caching strategy

The most robust strategy separates resources that change unpredictably from resources that never change under a given URL. This approach is often called cache busting with fingerprinted file names.

ResourceRecommended policyReason
HTML documentsno-cache with ETagUsers must get the latest version quickly
Fingerprinted CSS and JavaScriptmax-age of one year, immutableA new version gets a new URL
Images and fonts with versioned URLsLong max-ageRarely change, large transfer savings
API responsesShort max-age or no-cacheData freshness matters
Personal or sensitive dataprivate or no-storePrevents exposure on shared devices
Recommended caching by resource type

With content hashes in file names, such as a stylesheet whose name changes when its content changes, long cache lifetimes become safe. The HTML document references the new name, so users receive updates immediately while unchanged files are reused.

Common caching problems

Caching errors are often invisible to developers because their own browsers behave differently from those of first-time visitors. The following problems appear frequently.

  • Stale assets after deployment: long max-age on files without fingerprints keeps old CSS or scripts in use.
  • No caching at all: missing headers force repeat downloads and slow the critical rendering path on every visit.
  • Caching personal pages: shared or public caching of user-specific responses can expose private data.
  • Relying on heuristics: without explicit headers, browsers estimate freshness from Last-Modified, which leads to unpredictable behavior.
  • Testing with a warm cache: developers miss slow first-visit performance unless they disable the cache in developer tools.

Clearing the browser cache can fix a problem for one user, but it is not a solution for a website. Correct headers and versioned URLs ensure that every visitor receives the right version automatically.

The idea in brief

The browser cache is the browser’s short-term memory for websites. When you visit a page, the browser keeps copies of things like images and styles. On your next visit, it can use those copies instead of downloading everything again.

This makes pages load faster and saves data. The website tells the browser how long each copy may be kept and when it must check for a newer version.

A practical scenario

A news website notices that returning readers on mobile networks wait several seconds for each article. An audit shows that its stylesheets, scripts and logo are sent without any caching headers, so they are downloaded again on every page view.

The development team adds content hashes to asset file names and serves them with max-age of one year and immutable. Article HTML is served with no-cache and an ETag, so readers always receive the latest version of a story after a quick revalidation.

After the change, repeat page views transfer only the HTML and new images, reducing loading time noticeably for returning readers, and deployments no longer cause users to see outdated layouts.