Fetch events and response interception
Quick answer
A controlling service worker can respond to eligible requests through its fetch event. It can produce a Response or pass the request to another source.
How it works
In the fetch event listener, inspect event.request, including method and URL. Call event.respondWith(responseOrPromise) during dispatch, not after awaiting unrelated work. If the handler does not respond, normal fetch handling continues. Match only the intended route and method; avoid applying offline policies to every request. These worker-script examples are alternatives; install one in the supplied lab, reload until controlled, then request the indicated same-origin path.
Local examples
Use this as a replacement sw.js in a fresh isolated offline-lab origin; install/activate or request ./v14-message as directed. Observe the worker console. NOT RUN locally.
(async () => {self.addEventListener("fetch",event=>{if(event.request.method==="GET"&&new URL(event.request.url).pathname===new URL("v14-message",self.registration.scope).pathname){console.log(event.request.method);event.respondWith(new Response("demo"));}});})()Expected console output (not executed locally):
GETA controlled-page GET to ./v14-message produces this worker-console line.
Use this as a replacement sw.js in a fresh isolated offline-lab origin; install/activate or request ./v14-message as directed. Observe the worker console. NOT RUN locally.
(async () => {self.addEventListener("fetch",event=>{if(event.request.method==="GET"&&new URL(event.request.url).pathname===new URL("v14-message",self.registration.scope).pathname){event.respondWith(new Response("offline-demo",{headers:{"Content-Type":"text/plain"}}));console.log("responded");}});})()Expected console output (not executed locally):
respondedA controlled GET triggers the handler and receives offline-demo as its body.
Common mistakes
Do not intercept all POST requests or cache personal responses by default. A Response with an error status is still a Response; decide separately how to handle it.
Practice
In the lab, fetch ./v14-message from a controlled page and compare its response with a normal network request.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Fetch events and response interception — Knowledge Check
Apply the concept and reason about local example output.
When must respondWith be called?
Which property exposes the HTTP method of the intercepted request?
Why restrict interception to specific methods and paths?
What is the expected console output of Fetch events and response interception example 1?
(async () => {self.addEventListener("fetch",event=>{if(event.request.method==="GET"&&new URL(event.request.url).pathname===new URL("v14-message",self.registration.scope).pathname){console.log(event.request.method);event.respondWith(new Response("demo"));}});})()What is the expected console output of Fetch events and response interception example 2?
(async () => {self.addEventListener("fetch",event=>{if(event.request.method==="GET"&&new URL(event.request.url).pathname===new URL("v14-message",self.registration.scope).pathname){event.respondWith(new Response("offline-demo",{headers:{"Content-Type":"text/plain"}}));console.log("responded");}});})()