Advertisement
Advanced JavaScript, Browser Storage & Web APIs

What is the Cache API and how does it relate to Service Workers?

Short answer

The Cache API lets you store Request/Response pairs programmatically, most commonly from within a Service Worker, to serve assets offline or speed up repeat loads. Unlike browser HTTP caching, you fully control what's cached and for how long.

Save JavaScript mock interview
Advertisement
AModel answer
The Cache API lets you store Request/Response pairs programmatically, most commonly from within a Service Worker, to serve assets offline or speed up repeat loads. Unlike browser HTTP caching, you fully control what's cached and for how long.
caches.open('v1').then(cache => {
  cache.addAll(['/', '/styles.css', '/app.js']);
});
Real-world example
Precaching an app's shell so it still loads (with a fallback UI) when the user is offline.
Likely follow-ups
  • How do you handle cache versioning when you deploy a new build?
Related topics