v159 · service worker · basic usage

Fallback-timer race

The exact scenario from the ChromeStatus entry. A static route sends the request down "race-network-and-fetch-handler"; the fetch handler calls fetch(event.request) and races it against an offline-fallback timer inside event.respondWith(). When the timer wins before the server has sent headers, Chrome cancels the raced request — and the worker’s fetch() rejects. Which TypeError message it rejects with is what Chrome 159 changed.

Registering this feature’s service worker…

Run one raced request

800 ms The real route waits this long before sending any response headers.
150 ms Timer < header delay → the fallback answers first and the raced request is cancelled.
Press “Run the race” to make a real request.

what the worker runs

// ../sw.js, install: route the slow URL down the race source
await event.addRoutes({
  condition: { urlPattern: { pathname: "/v151/cpu-performance-api/capability-echo" } },
  source: "race-network-and-fetch-handler",
});

// fetch: race the deduplicated network fetch against a fallback timer
const network = fetch(event.request);
event.respondWith(Promise.race([network, timer(fb).then(offlineFallback)]));
network.catch((error) => report(error.name, error.message));   // verbatim, to this page

The page also reads Resource Timing for the same URL: workerMatchedRouterSource proves the request really matched the race rule, and workerFinalRouterSource says whether the network ("network") or your handler ("fetch-event") supplied the response.

see also