MSW fails to start Mock because worker update() fails.
Prerequisites
- I confirm my issue is not in the opened issues
- I confirm the Frequently Asked Questions didn't contain the answer to my issue
Environment check
- I'm using the latest
mswversion - I'm using Node.js version 18 or higher
Browsers
Chromium (Chrome, Brave, etc.), Firefox
Reproduction repository
https://codesandbox.io/p/devbox/msw-react-xx1c8
Reproduction steps
Can't make a reproduction repository, it depends if my backend is accessible. If backend is running, MSW mock starts and do his job. MSW fails to start mock with SetupWorker.start() when server is not reachable.
To reproduce, I have to stop my backend that serves assets and bundle.js resources.
Current behavior
The function that find existing registration of the service worker is trying to update it and returns fetch error below because service-worker.js resources is not accessible.
getWorkerInstance.ts :
if (existingRegistration) { // When the Service Worker is registered, update it and return the reference. return existingRegistration.update().then(() => { return [ getWorkerByRegistration( existingRegistration, absoluteWorkerUrl, findWorker, ), existingRegistration, ] }) }
I'm asking, why do we need to call update() on existingRegistration before returning worker instance if worker is active ?
Expected behavior
Suggestion:
- Add an option in StartOptions interface to bypass update if not needed.
- Try update() but catch error and return worker instance from registration if active.
Source: mswjs/msw