Skip to content
tezvyn:

Intercept Network Requests with the `fetch` Event

Source: developer.mozilla.orgMediumHow cards are made

Intercept Network Requests with the `fetch` Event

The fetch event turns your Service Worker into a programmable network proxy. Use it to intercept outgoing requests to serve assets from a cache for offline support or to synthesize custom responses.

Why it exists

To give developers fine-grained control over network requests originating from their web app. This enables powerful features like offline functionality and custom caching strategies that go beyond the browser's default behavior, making Progressive Web Apps (PWAs) more resilient and performant.

The mental model

Think of a Service Worker's fetch listener as a customs agent for your web app's network traffic. Every outgoing request—whether an API call, an image load, or a CSS file—must pass through this agent. The agent can then decide to let the request proceed to the network, redirect it to a local warehouse (the cache), or deny entry by creating a custom or error response on the spot.

How it works

When your app makes a network request, the browser fires a fetch event in the active Service Worker. Your event listener receives a FetchEvent object, which contains the original Request. To intercept it, you must synchronously call event.respondWith(). This method takes a Response object or, more commonly, a Promise that resolves to a Response. If you don't call respondWith(), the browser simply handles the request as if the Service Worker wasn't there, sending it to the network.

When to use it

This is the core mechanism for building offline-first applications. Use it to implement caching strategies like cache-first (serve from cache, fall back to network), network-first (try network, fall back to cache), or stale-while-revalidate. It's also useful for creating API mocks during development by intercepting fetch calls and returning synthetic JSON responses without a backend.

When not to use it

Avoid adding a fetch event listener if you don't intend to handle requests. An empty or slow listener adds overhead to every single network request your application makes, which can degrade performance. If you only need to cache assets when the Service Worker is installed, use the install event and the Cache API directly without intercepting fetch calls.

One canonical example

A cache-first strategy is a common use case. Inside the fetch event handler, you first check if a matching response exists in the Cache storage using caches.match(event.request). If a cached response is found, you return it immediately via event.respondWith(cachedResponse). If not, you proceed to make the actual network request with fetch(event.request), put the new response into the cache for future use, and then return that new response. This entire logic is wrapped in a promise chain passed to respondWith().

Interview question

What is the outcome if a Service Worker's `fetch` event listener receives a request but does not invoke `event.respondWith()`?

  • a.The request is automatically cached by the Service Worker for future offline use.
  • b.The Service Worker terminates the request, resulting in a network error for the client.
  • c.The browser proceeds to handle the request directly, bypassing the Service Worker's control.Correct
  • d.The request is redirected to the `install` event handler for initial setup and caching.
Why?

The card explicitly states that if `event.respondWith()` is not called, "the browser simply handles the request as if the Service Worker wasn't there, sending it to the network." Option A is incorrect because caching requires explicit logic and `respondWith()` to return a cached response, not its omission.

Just read this? Test yourself on what you have been reading.

Read the original → developer.mozilla.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles