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:
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.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();
}
}
});
}