next-pwa icon indicating copy to clipboard operation
next-pwa copied to clipboard

How to uninstall the whole thing?

Open landsman opened this issue 2 years ago • 3 comments

Basically, I am missing a tutorial how to uninstall next-pwa from the project.

I removed all related things with this and my customers now facing issues with the white page, so they have to do a manual reload, many times.

So I figured out that's a problem with the service worker. Can you please add an official way how to remove this implementation from the project? 🙏

landsman avatar Jan 20 '22 16:01 landsman

Now I am trying to do this:

/**
 * Get rid of relict from using next-pwa
 */
function ServiceWorkerUnregister() {
    if('serviceWorker' in navigator) {
        navigator.serviceWorker.getRegistrations().then((registrations) => {
            if (!registrations.length) {
                console.info('✅ No serviceWorker registrations found.')
                return;
            }
            for (let registration of registrations) {
                registration.unregister().then((boolean) => {

                    // result of the unregister
                    if (boolean) {
                        console.info('✅ Successfully unregistered');
                    } else {
                        console.error('🔥 Failed to unregister');
                    }

                    // additional info
                    if (registration.installing) {
                        console.log('SW_Registration.installing.scriptURL', registration.installing.scriptURL);
                    }
                    if (registration.waiting) {
                        console.log('SW_Registration.installing.scriptURL', registration.installing.scriptURL);
                    }
                    if (registration.active) {
                        console.log('SW_Registration.active.scriptURL', registration.active.scriptURL);
                    }
                    if (registration.scope) {
                        console.log('SW_registration.scope', registration.scope);
                    }
                })
            }

            // reload the whole page
            if (undefined !== window) {
                window.location.reload();
            }
        })
    } else {
        console.info('✅ Service worker is not supported in this browser');
    }
}

export default ServiceWorkerUnregister;

and link this function to _app.tsx:

    /** relict from using next-pwa */
    useEffect(() => {
        ServiceWorkerUnregister();
    }, [])

and link this function to /public/sw.js and /public/service-worker.js:

import ServiceWorkerUnregister from "../src/utils/browsers/service-worker-unregister";

 window.addEventListener('load', function() {
     ServiceWorkerUnregister();
 });

Did I miss something?

landsman avatar Jan 20 '22 16:01 landsman

So from first testing on my devices, it seems to be fine.

The service worker was successfully unregistered. You have to close the browser tab to fully remove it chrome://serviceworker-internals/ so bigger troubles was in our native app wrapper, where was PWA cached after many restarts.

Let's see what our users will report.

landsman avatar Jan 20 '22 22:01 landsman

I had similar problem, next-pwa created many versions of itself due to frequewnt pushes and messed up the whole site.

Used the following and it worked fine:

// _app.js

useEffect(() => {
  unregister()
}, [])

function unregister () {
  if ('serviceWorker' in navigator) {
    navigator.serviceWorker.getRegistrations().then(function (registrations) {
      for (const registration of registrations) {
        registration.unregister()
      }
    })
  }
}

talaikis avatar Jan 26 '22 20:01 talaikis