question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

My app hooks in beforeunload with a confirmation message and then MSW stops working

See original GitHub issue

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.0
  • nodejs: v17.4.0
  • npm: 8.3.1
  • nuxt 3
  • vite

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. image

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
Ryomasaocommented, Mar 23, 2022

I’m having the same problem.

The order beforeunloads are executed is the order in which they are registered, so msw’s beforeunload is executed first before e.stopImmediatePropagation or e.stopPropagation is executed in the application.

Is there any way to do this? Like restarting the worker when beforeunloading?

UPDATE

I tried to start the worker again beforeunload, but the following error occurred.

image

0reactions
florianmatzcommented, Dec 6, 2022

Same here 😛 - as described evt.preventDefault() is not working, because it is to late. Any updates?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Easy fix for: Blocked attempt to show a 'beforeunload ...
The fix is pretty straightforward: Make sure the user interacts with your page before showing the beforeunload confirmation box. If the user did ......
Read more >
reactjs event listener beforeunload added but not removed
I am removing the eventListener from the component on componentWillUnmount . Then why it is not being removed? How can I remove the...
Read more >
Sure you want to leave?—browser beforeunload event
In the video, I explain a bit about the beforeunload event—which lets you prompt or warn your user that they're about to leave...
Read more >
Untitled
#Kid Free giveaway of the day game, Koleksi foto hyohyuk, Csync2 ubuntu, ... orange line stops, Profesor bochenek opinie, Three core values of...
Read more >
React - CodeProject
A post method is working fine with Postman but not working in react native and getting error code 400. The api working from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found