Search code examples
angularservice-workerangular-service-worker

Mistakenly added service worker. Removed it but how to get it off existing computers?


When starting an Angular (6) project, i used a template that had a service worker. Several months later, i had a problem that the solution was to remove the service worker (via removing ServiceWorkerModule.register from my app.module imports). However, every computer that opened that app prior to that version still has the service worker installed and running (still causing the original issue). I know I can manually remove one on one computer via dev-tools, but how can I get a solution in code?

Some Things I've tried:

  • https://angular.io/guide/service-worker-devops#fail-safe This says you can remove your ngsw.json file and the service worker will "self destruct" but our running code's root folder doesn't have a ngsw.json, only a ngsw-config.json. Also says you can rename safety-worker.js to the name of your old service worker but my deployed code doesn't have safety-worker.js nor do i know the name of my service worker.

Solution

  • You can 'unregister' the service worker using javascript. Here is an example:

    if ('serviceWorker' in navigator) {
          navigator.serviceWorker.getRegistrations().then(function (registrations) {
            //returns installed service workers
            if (registrations.length) {
              for(let registration of registrations) {
                registration.unregister();
            }
         }
      });
    }