Skip to content
tezvyn:

Cache API: Manual Control Over Network Responses

Source: developer.mozilla.orgHardHow cards are made

Cache API: Manual Control Over Network Responses

The Cache API is a key-value store for network requests you control directly, unlike the browser's automatic HTTP cache. Use it in service workers for offline support or to pre-cache app assets.

Why it exists

The browser's standard HTTP cache is automatic and opaque. Developers needed a low-level, scriptable API to explicitly control caching logic, especially for building reliable offline experiences and performance-critical applications. The Cache API provides this direct control over request/response pairs.

The mental model

The Cache API provides a persistent storage mechanism for Request and Response pairs. Think of it as a set of named, versionable hash maps that you manage yourself. Unlike the browser's HTTP cache, nothing happens automatically; you decide what to store, when to retrieve it, and when to delete it. It's available in both the main window and web workers, but its most common use is within a service worker.

How it works

You first open a named cache using CacheStorage.open('my-cache-v1'). This returns a promise that resolves with a Cache object. You can then use methods like add() or addAll() to fetch URLs and store their responses, or put() to store a Request/Response pair you already have. To retrieve an item, you use match(request), which returns the corresponding Response. Deletion is done explicitly with delete(request). All these operations are asynchronous and return Promises.

When to use it

The primary use case is inside a service worker to make a Progressive Web App (PWA) work offline. By intercepting fetch events, the service worker can check the cache first with cache.match(event.request) and return a stored response if available, falling back to the network otherwise. It's also used to pre-cache an application's "shell"—the minimal HTML, CSS, and JS—during the service worker's install event for instant subsequent loads.

When not to use it

Do not use it if you want the browser's standard, header-based caching behavior. The Cache API ignores Cache-Control, Expires, and ETag headers. If you just want to cache user-specific data that isn't a network response, IndexedDB is a more appropriate choice. The Cache API is specifically for request/response pairs.

One canonical example

A service worker caching its app shell on install. During the install event, it calls caches.open('app-shell-v1') and then uses the addAll() method with an array of URLs like ['/', '/styles.css', '/app.js']. On subsequent visits, the service worker's fetch event handler can intercept requests for these assets and serve them directly from the cache, making the app load instantly. A common footgun is forgetting to manage old caches; when a new service worker activates, it should delete caches from previous versions.

Interview question

Which statement accurately describes a key characteristic of the Cache API compared to the browser's standard HTTP cache?

  • a.It is primarily designed for persistent storage of arbitrary user-specific data, such as application settings.
  • b.It automatically manages cache invalidation based on HTTP headers like Cache-Control.
  • c.It provides developers with explicit, scriptable control over storing and retrieving network Request/Response pairs.Correct
  • d.Its cached content is only accessible within the main browser window, not in web workers or service workers.
Why?

The Cache API's core value is offering direct, programmatic control over network responses, allowing developers to decide what to store and when to retrieve it. Unlike the browser's HTTP cache, it explicitly ignores HTTP caching headers and requires manual management of cache entries.

Just read this? Test yourself on what you have been reading.

Read the original → developer.mozilla.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles