mswjs / msw

Industry standard API mocking for JavaScript.
https://mswjs.io
MIT License
15.97k stars 519 forks source link

Perform worker update in parallel #2310

Closed kettanaito closed 1 month ago

kettanaito commented 1 month ago

Prerequisites

Environment check

Browsers

No response

Reproduction repository

Any example

Reproduction steps

  1. Have a context that activates MSW.
  2. Have another MSW activation in the same context.

Current behavior

When MSW finds a matching registration and attempts to reuse it, it first updates the worker. That is done to make sure that the existing worker registration is up-to-date. For example, if you are registering a worker that doesn't match the existing worker (i.e. the script has changed due to the MSW version change), the existing worker will be marked as redundant.

https://github.com/mswjs/msw/blob/452686d27cc84c1a18262e9d0b18cf64b13aa71d/src/browser/setupWorker/start/utils/getWorkerInstance.ts#L39-L40

The problem is that .update() takes time. It needs to fetch the worker script, evaluate it byte-by-byte, comparing the old and the new scripts. That's a potential overhead that MSW doesn't have to await.

Expected behavior

Update is scheduled to run in the background. MSW resolves with the existing worker registration immediately.

kettanaito commented 1 month ago

Released: v2.4.10 🎉

This has been released in v2.4.10!

Make sure to always update to the latest version (npm i msw@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.