How to bypass requests on the worker level? #1424
-
ScopeAdds a new behavior Compatibility
Feature descriptionWe are running our tests currently on AMD which means loading lots of javascript files. When the service worker is active, each single request requires a round trip to the main thread and back just to say that certain urls should I did a quick proof of concept of having an exclude list of urls directly in the service worker so it can opt out of handling those urls entirely. This resolves the latency issue we're seeing. It would be nice if there was functionality where we could configure the worker with such a list of url prefixes. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
Hey, @jkieboom. Thanks for raising this. I understand the extra journey to the worker and back may have implications like latency but it's rather challenging to circumvent it entirely. When using MSW, the request flow is:
We cannot skip points 2 and 3 because the client won't be able to know anything about the request then. The whitelist of endpoints is data. The only way to pass the data to a worker is by posting a message. Posting a message is an asynchronous action, which means we introduce a window of uncertainty while it's being processed. In this light, even if we added something like worker.start({
whitelistHosts: ['api.github.com']
}) we would have to send that whitelist data to the worker upon its start. To prevent intermittent behaviors, we'd have to await the worker to receive that data, which means a window during which no requests can be handled. Now, we have the same latency applied to every request anyway. The most sensible way here is to compose workers, I think. You can have your custom worker that imports the MSW's worker via // public/whitelistWorker.js
const whitelistHosts = ['api.github.com']
self.addEventListener('fetch', (event) => {
const url = new URL(event.request.url)
if (whitelistHosts.includes(url.host)) {
// Do not propagate this event to other listeners (from MSW).
event.stopImmediatePropagation()
// I'd expect this early return to take precedence
// over the MSW logic below, allowing you to
// bypass it entirely, releasing the request.
return
}
})
importScripts('./mockServiceWorker.js') |
Beta Was this translation helpful? Give feedback.
-
I looked a bit into this, but I'm not sure how this could work. From what I can tell there isn't a way to say the worker should not handle the fetch event but at the same time no other event listeners should handle it either. |
Beta Was this translation helpful? Give feedback.
Hey, @jkieboom. Thanks for raising this.
I understand the extra journey to the worker and back may have implications like latency but it's rather challenging to circumvent it entirely. When using MSW, the request flow is:
We cannot skip points 2 and 3 because the client won't be able to know anything about the request then.
The whitelist of endpoints is data. The only way to pass the data to…