How to remove a service worker registered in previous deploy from users' browsers with the newly deployed version of site (Firebase)?

痴心易碎 提交于 2019-12-04 17:13:28

I finally found the solution!

So if you have the same problem, just do this:

1 Install this webpack plugin (if you're using webpack in your project)

npm install webpack-remove-serviceworker-plugin

2 Create the constant in webpack.prod.conf.js file (or whatever your production config is called)

const RemoveServiceWorkerPlugin = require('webpack-remove-serviceworker-plugin')

3 Add the plugin into plugins: [ ] in that file (don't put ; there if you don't use them in your project):

new RemoveServiceWorkerPlugin({ filename: 'service-worker.js' });

4 change that service-worker.js filename to whatever your service worker was called previously (the one you need to remove. You can find its name in the network tab of developer tools when page is loaded)

5 Build the project again and deploy


If you don't use webpack, you can try to do this - replace the old service worker with one that unregisters itself:

registration.unregister().then(async () => {
  // optionally, get controlled pages to refresh:
  for (const client of await clients.matchAll()) {
    client.navigate(client.url);
  }
});

I think it depends on what your service worker is doing and how it is doing it. This item from Google talks about the difficulties relating to service workers that cache code, and they recommend using this library.

What is a service worker?

A service worker sits between the client and the server and resides inside of the users browser. They have several roles and can perform many tasks. One of those tasks is to cache the script/code from a website (it's shell).

A historical comparison

In many ways caching code looks like how the internet worked back in the late 1990s. Back then you could go into the folder's browser history and open the folders and get a partially working website even when offline.

A risk

Service workers can do that same task as looking into a websites folders stored on a computer, by doing this before calling back to the server for files makes your site look and feel super fast, because the service worker delivered the locally stored version of your site.

However, there is a huge risk. If a service worker is not set up correctly when a user visits a url the service worker will always point to the cached version of a website first. That means no matter what code you change or deliver the client never actually asks for it.

This is the worst situation as the only way to fix this is for the user to go into the developers panel and delete the service worker, or clear their browsers data, but they may never know to do this, and just believe a website is broken.

Take a look at this merged Pull Request where it was implemented the flag --skip-plugins where you can specify the name of the plugins you want to ignore during npm run build or something like that. It seems a good idea the PWA plugin off.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!