The Service Worker does not shut down

22 Views Asked by At

I'm using msw to mock API request, and almost API develop is done, so I want to turn off serviceworker and I turn on / turn off bypass for network in developer mode's application tab

but still msw is running and intercepting requests...

I tried unregistering service worker but when I refresh, it is registered again...

I'm using mac.

1

There are 1 best solutions below

0
kettanaito On

MSW is running and intercepting requests after a page reload because your client-side JavaScript has a worker.start() call. If you remove it, or call it conditionally, MSW will not register its Service Worker.

In fact, MSW detects when you close your application's tab in the browser, and when the last one closes, it unregisters the worker automatically so it doesn't leave across different projects served on the same host. The only reason it registers back is when your code has worker.start().

This is a very long explanation of:

  1. Skip the worker.start() call if you don't want the worker to register;
  2. There's also worker.stop() at any point on runtime until the page refreshes.