Skip to content

unregister only CRA service-worker  #9566

@marcosvega91

Description

@marcosvega91

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions