shadowwalker / next-pwa

Zero config PWA plugin for Next.js, with workbox 🧰
MIT License
3.88k stars 324 forks source link

How to uninstall the whole thing? #315

Open landsman opened 2 years ago

landsman commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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.

talaikis commented 2 years ago

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()
      }
    })
  }
}