Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unregister only CRA service-worker #9566

Closed
marcosvega91 opened this issue Aug 29, 2020 · 4 comments
Closed

unregister only CRA service-worker #9566

marcosvega91 opened this issue Aug 29, 2020 · 4 comments

Comments

@marcosvega91
Copy link

marcosvega91 commented Aug 29, 2020

Describe the bug

CRA provides a script to use service worker. This script export a function to unregister it.
This function is called, by default, in the index file of the template. On MSW SW is used to intercept http calls and mock them, but because of the SW are unregistered on bootstrap it may cause some problem. Is this the correct behavior?
Maybe CRA should only unregister his SW.

The unregister function could be something like the following

export function unregister() {
  if ('serviceWorker' in navigator) {
    navigator.serviceWorker.getRegistrations().then((registrations) => {
      registrations.forEach((registration) => {
        if (registration?.active?.scriptURL.includes('service-worker.js')) {
          registration.unregister()
        }
      })
    })
  }
}

Expected behavior

unregister should remove only SW registered by CRA.
We have an issue here

Actual behavior

unregister is removing other SW.

@kettanaito
Copy link

I believe that the Service Worker script that came by default with Create React App has been removed and is no longer present in the newer installations. You can see the file structure included with the current template here (notice no Service Worker script).

Not sure to which extend the CRA team can support here, it may be worth just mentioning the removal of .unregister() in the MSW documentation as a good default.

@marcosvega91
Copy link
Author

There are new templates cra-template-pwa and cra-template-pwa-typescript with serviceWorker script

@stale
Copy link

stale bot commented Oct 4, 2020

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs.

@stale stale bot added the stale label Oct 4, 2020
@stale
Copy link

stale bot commented Oct 12, 2020

This issue has been automatically closed because it has not had any recent activity. If you have a question or comment, please open a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants