Intercepting http calls from an existing Service Worker and returning a canned response to the service worker #1869
Replies: 1 comment
-
Hey, @mickeypuri. Thanks for raising this. Let's start by addressing the confusion here.
I believe you think of the setup as (A) Your Service Worker, (B) MSW's Service Worker, and whether one can intercept requests from another. But that's not how service workers work. You can only have one service worker controlling the host. This automatically answers your question: there's only 1 service worker and it cannot intercept its own requests. To use MSW in an app that already has a service worker registered, you'd have to compose the workers using the We can technically patch the worker's scope |
Beta Was this translation helpful? Give feedback.
-
Our web application uses a Service Worker which makes certain data intensive calls, and then caches the data in this Service Worker. The idea being that the user can open a new tab which also will use the same common Service Worker.
I'm looking to use MSW to write tests, and to intercept calls to APIs and return canned responses from MSW.
What I'm unsure about is can the MSW Service Worker intercept API calls from our existing Service Worker, and how can I set that up.
The idea being that I set up the API in MSW, and when our current Service Worker makes this call, its intercepted and picked up in MSW and this canned response sent back.
Is this going to be possible, or is the MSW only able to intercept calls from the browser and not from an existing service worker.
Beta Was this translation helpful? Give feedback.
All reactions