freecoding.school100% FREE · NO SIGNUP
API ArcadeISSUE #12 of 23

cache api · putting responses in cache

ApiaVSLegacy Larry
Apia saysThe Cache API stores request/response pairs for offline and fast repeat loads.

Used mostly inside service workers, caches.open(name) gives a cache where you put, add(fetch+store), and match requests. Combined with the fetch event, it implements strategies: cache-first (offline-friendly), network-first (fresh), or stale-while-revalidate. It's the storage layer behind offline PWAs.

Power-ups you unlock

Legacy Larry attacks — common mistakes

Boss battleSketch a stale-while-revalidate handler using the Cache API.

Example code

<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px"><pre>const cache = await caches.open('v1');
await cache.addAll(['/','/style.css']);   // precache
const hit = await cache.match('/style.css');
// SWR: respond from cache, then update it from network</pre></body></html>
▶ Open the interactive comic issue
‹ Service Workers · Install · Activate · FetchWeb Workers · PostMessage · Transferable ›