Skip to content

Caching Strategies: Overview

A network request can fail for many reasons — the user is offline, on a slow connection, or the server is temporarily unavailable. Without a cache, any of these conditions means a broken experience. With a well-chosen caching strategy, the browser can serve assets instantly from local storage and sync with the server in the background.

Caching also eliminates unnecessary round trips. A CSS file that has not changed does not need to travel across the network every time someone loads your page. Serving it from a local cache brings load times down from hundreds of milliseconds to near zero.

Precaching happens at service worker install time. You explicitly list the URLs you want cached, and the browser fetches and stores them before the SW activates. This is ideal for your app shell — the HTML, CSS, and JS files that form the structural skeleton of your UI.

Runtime caching happens on demand. The first time a user requests a resource (an image, an API response, a font), the service worker intercepts the fetch, stores the response, and serves subsequent requests from cache. This approach is suitable for dynamic or user-specific content that you cannot enumerate at build time.

StrategyBest forTrade-off
Cache-firstStatic assets (CSS, JS, images)Can serve stale assets
Network-firstAPI data, HTMLSlower; needs cache fallback
Stale-while-revalidateAvatars, feeds, configEventually consistent
Cache-onlyPre-cached app shellNo network fallback
Network-onlyPayments, analyticsNo offline support
flowchart TD
  A[Start: incoming request] --> B{Is the asset static?}
  B -->|Yes| C[Cache-first]
  B -->|No| D{Does freshness matter?}
  D -->|Yes, always fresh| E[Network-first]
  D -->|Mostly fresh is fine| F[Stale-while-revalidate]
  D -->|No network needed| G[Cache-only]
  E --> H{Network failed?}
  H -->|Yes| I[Serve from cache fallback]
  H -->|No| J[Return fresh response and update cache]
Choosing a caching strategy
What is the difference between precaching and runtime caching?
Which strategy is best for a frequently-changing news feed API?
Stale-while-revalidate is ideal for: