I need to completely remove the service worker script (sw.js) from my webpage; I can successfully unregister it via
// index.js
try {
navigator.serviceWorker.getRegistrations().then((registrations) => {
for (let registration of registrations) {
// console.log('Reg:')
// console.log(registration)
if (registration.scope.endsWith('/my_scope/')) {
registration.unregister().then((unregRes => {
if (unregRes) {
console.log('Unregistration success');
}
else console.log('Unregistration failed');
}));
}
}
}).catch(function(err) {
console.log('Service Worker unregistration failed: ', err);
});
} catch(e) {
swRes['error'] = e.message;
}
It says 'Unregistration success' without errors, but I still see sw.js in Chrome DevTools => Sources. In chrome://serviceworker-internals/, I see
Unregistered worker:
Installation Status: REDUNDANT
Running Status: RUNNING
Fetch handler existence: DOES_NOT_EXIST
Fetch handler type: NO_HANDLER
Script: https://my.site/my_scope/sw.js
Version ID: 577
Renderer process ID: 11084
Renderer thread ID: 4
DevTools agent route ID: 18
I can stop sw.js in chrome://serviceworker-internals/ by clicking the Stop button, and the file goes away from Sources, but I need automated file removal due to project requirements.
I feel like Running Status: RUNNING restricts me from removing my worker, but I don't know how to change this status. Also I tried self-destroying worker from https://github.com/NekR/self-destroying-sw , but the behavior stays the same.
Any explanations of how to automatically remove SW's file from the browser storage are welcome.
Upd: Running Status is RUNNING only if I access my page with opened Chrome DevTools; otherwise sw.js appears in chrome://serviceworker-internals/ with Running Status: STOPPED and is flushed from Source tab.
It appears that unregistered SW's RUNNING status is an issue of chrome://serviceworker-internals/ tab; if I check the Application DevTools tab after accessing my page with DevTools open, I see my service worker deleted:
Application tab with deleted SW
So, the service worker is actually deleted after unregistration, but Chrome keeps it in DevTools for debug purposes, I suppose.