Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MSW fails to start Mock because worker update() fails. #2006

Open
4 tasks done
lazpit opened this issue Jan 30, 2024 · 2 comments
Open
4 tasks done

MSW fails to start Mock because worker update() fails. #2006

lazpit opened this issue Jan 30, 2024 · 2 comments
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. scope:browser Related to MSW running in a browser

Comments

@lazpit
Copy link

lazpit commented Jan 30, 2024

Prerequisites

Environment check

  • I'm using the latest msw version
  • 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.

image
image

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.
@lazpit lazpit added bug Something isn't working needs:triage Issues that have not been investigated yet. scope:browser Related to MSW running in a browser labels Jan 30, 2024
@lazpit
Copy link
Author

lazpit commented Feb 12, 2024

This error seems to occur when there is a "no-cache" header when fetching service-worker.js

@kettanaito
Copy link
Member

Hi, @lazpit. Thanks for reporting this.

I'm asking, why do we need to call update() on existingRegistration before returning worker instance if worker is active ?

Because the existing worker may be out-of-date, and the one you are registering now can be newer. Calling .update() will let the browser compare the current and the latest worker at the same worker URL, and if they are not the same, the latest worker will be registered and activated.

This error seems to occur when there is a "no-cache" header when fetching service-worker.js

How can you reproduce this? Afaik, you cannot control how the fetch request during .update() is performed. The browser forces that request to be non-cacheable. Can you show me an example of setting no-cache on that fetch request?


I've looked into a related issue in #2310. Since #2311, MSW will now perform the update in the background, returning the existing worker instance immediately. I like the use case you are showing this with issue, if you can please elaborate on how to reproduce it, I will make sure MSW handles update failures gracefully as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. scope:browser Related to MSW running in a browser
Projects
None yet
Development

No branches or pull requests

2 participants