My app hooks in beforeunload with a confirmation message and then MSW stops working
Author: jmvergesCreated Feb 16, 2022Updated May 7, 2026
Labelsbugpotentially solved
Describe the bug
I have some code in my page that hooks in beforeunload and then the user can cancel and stay in the page or leave the page. If the user decides to stay in the page msw gets broken.
Environment
msw: 0.35.0nodejs: v17.4.0npm: 8.3.1nuxt 3vite
Please also provide your browser version.
Firefox 98.0b
To Reproduce
Steps to reproduce the behavior:
Add some code like:
window.addEventListener("beforeunload", function(e) {
const confirmationMessage = "Msw will not work from this point";
e.returnValue = confirmationMessage; // Gecko, Trident, Chrome 34+
return confirmationMessage; // Gecko, WebKit, Chrome <34
});Cancel and remain in the page Trigger some mocked mutation
Expected behavior
Mutation should be triggered
Screenshots
I can go to .vite generated code and delete those lines and then the mutation is triggered.

Source: mswjs/msw