v159 · service worker · static routing

ServiceWorker Static Routing: Distinct TypeError message for cancelled race-network-and-fetch-handler fetch() requests

With a static route whose source is "race-network-and-fetch-handler", Chrome starts the network request itself and fires your fetch handler in parallel; a fetch(event.request) inside the handler is deduplicated onto that same network request. If the browser cancels it before response headers arrive — because your offline-fallback timer answered respondWith() first, say — the promise used to reject with the generic TypeError("Failed to fetch"), indistinguishable from a real outage. From Chrome 159 it rejects with TypeError("The service worker race-network-and-fetch-handler request was cancelled before 'fetch()' settled."), mirroring what Navigation Preload already does for event.preloadResponse.

How to see the new message. Chrome 160+ and Canary: on by default. Chrome 159: the change is a Chromium feature, ServiceWorkerRaceNetworkRequestAbortedErrorMessage, that was still default-off in Chrome for Testing 159.0.8154.13 (so it reaches 159 by server-side field trial). To force it, relaunch Chrome with --enable-features=ServiceWorkerRaceNetworkRequestAbortedErrorMessage. There is no chrome://flags entry, and it is not a Blink runtime feature, so --enable-experimental-web-platform-features does not switch it on. Every demo still runs elsewhere and reports exactly which message your browser produced.

concepts

  1. Fallback-timer race

    The canonical scenario. Dial how long the server holds back its headers and how soon your offline-fallback timer fires, run a real raced request, and read the exact TypeError the worker’s fetch(event.request) rejected with — or watch it fulfil when the network wins.

  2. Fetch-shape inspector

    An inspection table: fetch(event.request), fetch(new Request(event.request)), fetch(event.request.url), calling fetch(event.request) twice, and an un-cancelled control — which shapes are deduplicated onto the raced request, and which of them get the distinct message.

  3. Telemetry noise filter

    Why it shipped: an app migrating from Navigation Preload filtered expected cancellations out of its error reports by message. Replay a session of real cancelled, successful and genuinely failing requests through a legacy and a message-aware classifier and count the false alarms each one would page you with.

  4. App-shell revalidation

    Cache-first app shell plus a background refresh, under a race route. Refreshing with fetch(event.request) is silently cancelled the moment the shell answers — now with a message that says so — and the cache never updates; refreshing with fetch(event.request.url) is an independent request that completes. Cache contents shown live.

  5. Error taxonomy

    The edge cases: race cancellation, a genuine network error on the same raced route, the page aborting its own request, going offline, and a clean success — side by side with the message each produces, plus a version-tolerant classifier for codebases that must run on Chrome before and after 159.

the API surface

// sw.js — nothing new to call: the change is the message on an existing rejection.
self.addEventListener("install", (event) => {
  event.addRoutes({
    condition: { urlPattern: { pathname: "/api/feed" } },
    source: "race-network-and-fetch-handler",
  });
});

self.addEventListener("fetch", (event) => {
  const network = fetch(event.request);            // deduplicated onto the raced request
  const fallback = new Promise((r) => setTimeout(() => r(offlinePage()), 3000));
  event.respondWith(Promise.race([network, fallback]));

  network.catch((error) => {
    if (error.message ===
        "The service worker race-network-and-fetch-handler request was cancelled before 'fetch()' settled.") {
      return;                                     // expected: we answered first (Chrome 159+)
    }
    reportNetworkError(error);                    // a real failure — or a cancellation on Chrome < 159
  });
});

Only the message changed; the rejection is still a TypeError, so catch blocks that test instanceof TypeError behave exactly as before. The message appears only for net::ERR_ABORTED on the raced, deduplicated request — genuine failures (DNS, offline, blocked redirects, CORS) keep "Failed to fetch".

why it shipped

Navigation Preload already rejects event.preloadResponse with The service worker navigation preload request was cancelled before 'preloadResponse' settled. when the preload is cancelled early, and offline-capable apps key their error telemetry on that string. Moving to static routing’s race source collapsed the same situation into Failed to fetch, so migrating produced false-positive network-error spikes and could even trip offline fallbacks. Chrome 159 gives the race path the matching treatment, with no IDL or specification change — and because web apps can observe the string, it went straight to an Intent to Ship for compatibility review.

what this demo depends on

All requests are real and same-origin. The slow side of each race is the published /v151/cpu-performance-api/capability-echo?delay=N route, which waits up to 1.2 s before sending headers; the genuine-failure case requests the published 301 alias /v150/disable-svg-filters-on-plugins-and-cross-origin-or-restricted-iframes/ with redirect: "error". The worker (sw.js, scoped to this folder) and the page harness (harness.js) are plain files you can read.

Not covered yet (needs a server route): cancelling a raced navigation before headers. A worker only intercepts navigations inside its own scope, and every file in this folder answers in about a millisecond, so there is no pre-header window to cancel. A header-delaying route inside this folder (for example v159/<slug>/slow-nav, wired in server.ts) would make that concept possible.

references