Reference
Policy surface for controlled clients. Strategies catalog: Cache API · strategies · Offline Cookbook.
respondWith → browser continues normal
network path (HTTP cache, then network).
self.addEventListener("fetch", (event) => {
// event.request — Request (url, method, mode, headers, …)
event.respondWith(
// must return a Response or Promise<Response>
caches.match(event.request).then((cached) => {
return cached || fetch(event.request);
})
);
});
respondWith synchronously in the
event handler (don’t wait for an async gap first).
Response body can be read once — use
response.clone() if you both return it and
cache.put it.
new Response("…", { status, headers }).
| Signal | Often means |
|---|---|
request.mode === "navigate" |
Document navigation — shell / HTML policy |
| Same-origin static asset | Cache-first / precached shell (versioned) |
| API / frequently changing JSON | Network-first or network-only |
self.addEventListener("fetch", (event) => {
event.respondWith(
caches.match(event.request).then((hit) => {
if (hit) return hit;
return fetch(event.request).then((res) => {
const copy = res.clone();
caches.open("runtime-v1").then((c) => c.put(event.request, copy));
return res;
});
})
);
});