Cypress 16.1: starting a dedicated Worker installs a new service worker version, so MSW stops mocking (works in 15.16)
Current behavior
In component testing on Chrome, a page that has a service worker registered (in our case MSW 2.12) gets a new service worker version installed each time it starts a dedicated Worker from a URL inside the service worker scope, for example new Worker('/src/layout.worker.ts', { type: 'module' }).
MSW's worker script calls skipWaiting() and clients.claim(), so the new version takes control of the page right away. The new version does not know that the page activated mocking, so every later request passes through to the network. Mocks stop working in the middle of a spec. In our suite this showed up as requests reaching the dev server (404) after the first new Worker(...) call.
It also makes worker.start() after worker.stop() hang forever: MSW sends its activation message to the service worker instance it kept, and that instance is now retired. When start() is awaited in a Cypress.on('test:before:run:async') handler, the run hangs with no output until the CI job times out.
Desired behavior
Same as Cypress 15.16.0: starting a dedicated Worker does not install a new service worker version. The registered service worker stays active and keeps controlling the page.
Test code to reproduce
- Component testing (Vite 8, React). Register MSW with
setupWorker(...).start()in the spec. - Mount a component that starts a module worker from a same-origin URL:
new Worker(new URL('./layout.worker.ts', import.meta.url), { type: 'module' }). - After the worker starts, make a request that an MSW handler mocks.
Observed on Cypress 16.1.0:
- A
controllerchangeevent fires onnavigator.serviceWorkerafter eachnew Worker(...). - A new service worker version appears (new target, state
activating→activated). - The mocked request reaches the network instead of MSW.
On Cypress 15.16.0 with the same Chrome version and the same spec, the service worker does not change and the mocks keep working. On 15.16.0, calling the CDP command ServiceWorker.setForceUpdateOnPageLoad(true) reproduces the 16.1.0 behavior exactly. The service worker's soft update check on the worker script fetch seems to install a new version every time.
Workaround: start the worker from a blob: URL that only imports the original module (new Worker(URL.createObjectURL(new Blob(['import "<worker url>";'], { type: 'text/javascript' })), { type: 'module' })). A blob: load is not a service worker fetch, so no update check runs and the service worker is not replaced.
This could be related to the service worker changes in 16.1.0 (#34789, #34721), but it happens for worker URLs both inside and outside /__cypress/.
Cypress Version
16.1.0 (works on 15.16.0)
Debug Logs
No response (DEBUG=cypress:* shows no explicit ServiceWorker.setForceUpdateOnPageLoad call from Cypress, so the exact code path was not identified.)
Other
- Browser: Chrome 153 (headless in CI on Linux, headed locally on macOS)
- Node: 24
- Testing type: component, Vite 8 dev server
- MSW: 2.12.14
Source: cypress-io/cypress