Explain Codes LogoExplain Codes Logo

How do I uninstall a Service Worker?

javascript
service-worker
cache
developer-tools
Nikita BarsukovbyNikita Barsukov·Aug 21, 2024
TLDR

Uninstall a service worker via a two-step process: use the navigator.serviceWorker.getRegistrations() method to retrieve all service worker registrations, and then apply the unregister() method on each.

// Time to clean house, folks! navigator.serviceWorker.getRegistrations().then(regs => regs.forEach(reg => reg.unregister()));

Running this code snippet detaches the service worker, ensuring it won't interfere with your web application. Do remember: you need to refresh the page to witness the changes.

Detailed walkthrough: Beyond Basics

The fundamental way to unregister a Service Worker may appear straightforward, but depending on situations, complexities can arise that require your attention. Let's deep-dive into additional scenarios.

Manual Cache Clearance

Once the Service Worker is unregistered, the cache may still cling on. To avoid outdated data, ensure to clear all caches.

// Out with the old, in with the new! caches.keys().then(names => names.forEach(name => caches.delete(name)));

Mind the lifecycle

Just because you've unregistered a Service Worker doesn't mean it's vanished entirely. All active tabs must be closed for a Service Worker to fully disappear. So, restarting the browser seals the deal.

Be aware of the scope

The scope of a Service Worker registration has an impact on unregistering a Service Worker. If you have multiple Service Workers registered at different paths, unregister() must be called individually for each.

navigator.serviceWorker.ready.then(reg => { // Who's ready for an eviction party? if (reg.scope === '/app') { reg.unregister(); } });

Working with browsers: Developer Tools and fixes

Every browser has its own quirks and developer tools that can help manage Service Workers.

Developer Tools

  • Browse Application in Chrome Devtools to unregister a service worker. Additionally, Clear Storage is useful when you need to refresh your local data storage.
  • For Firefox users, navigate to about:debugging#workers, choose your service worker, and hit unregister.
  • If you're using Safari, you can force unregister a service worker via Preferences > Privacy > Manage Website Data.

Login ERR_FAILED and Cache

Service Worker cache can sometimes cause ERR_FAILED during login. Clearing the cache can be a potential solution.

navigator.serviceWorker.getRegistration('/app').then(reg => { reg.unregister(); return caches.delete('user-cache'); }).then(() => { console.log('Cache cleared, Service Worker unregistered. Phew!'); });

"Update on reload" Setting

For those who live and breathe Service Workers, Chrome DevTools offers an Update on Reload feature. This gem ensures that the Service Worker is updated with every page reload, reducing the risk of old versions hanging around.

Service Worker Internals

The secret command chrome://serviceworker-internals/ in Chrome reveals the curtain, offering developers more control over their Service Workers.

Stubborn Service Workers

In rare cases, if issues persist even after uninstalling a Service Worker, it's time to consider factors like application architecture and network conditions.