乐闻世界logo
搜索文章和话题

如何卸载 service worker?

5 个月前提问
3 个月前修改
浏览次数133

6个答案

1
2
3
4
5
6

当您想要卸载 Service Worker 时,可以通过几个步骤来实现:

  1. 开发者工具中卸载:

    如果是在开发过程中,可以直接通过浏览器的开发者工具来卸载 Service Worker。以下以 Google Chrome 浏览器为例:

    • 打开需要卸载 Service Worker 的网站。
    • F12 或者右键选择“检查”打开开发者工具。
    • 切换到“Application”标签页。
    • 在左侧边栏的“Service Workers”选项下,可以看到当前激活的 Service Worker。
    • 有一个 "Unregister" 的按钮,点击它可以卸载当前的 Service Worker。
  2. 编程卸载:

    如果您希望在网站上编程实现卸载 Service Worker,可以通过以下代码实现:

    javascript
    if ('serviceWorker' in navigator) { navigator.serviceWorker.getRegistrations().then(function(registrations) { for (let registration of registrations) { registration.unregister(); } }).catch(function(error) { console.log('Service Worker 注销失败: ', error); }); }

    上面的代码段会找到所有注册的 Service Worker,并逐个注销它们。

  3. 清除浏览器数据:

    另一种卸载 Service Worker 的方法是清除网站的浏览器数据,包括缓存和本地存储。

    • 在 Chrome 浏览器中,可以按 Ctrl+Shift+Delete 快捷键打开“清除浏览数据”对话框。
    • 选择“高级”选项卡,勾选“Cookie 和其他站点数据”以及“缓存的图片和文件”。
    • 点击“清除数据”。

    这将删除所有的 Service Worker,以及相关的缓存和数据。

  4. 手动修改Service Worker代码:

    可以在 Service Worker 的代码中添加一个自毁逻辑,当满足特定条件时,自动卸载自身。例如:

    javascript
    self.addEventListener('install', (event) => { if (someCondition) { self.skipWaiting(); // 忽略等待,立即激活 } }); self.addEventListener('activate', (event) => { if (someCondition) { self.registration.unregister().then(() => { return self.clients.matchAll(); }).then((clients) => { clients.forEach(client => client.navigate(client.url)); // 刷新页面 }); } });

    在上述代码中,someCondition 是一个占位符,表示某个您决定卸载 Service Worker 的条件。当条件满足时,Service Worker 将自我注销,并刷新所有的客户端页面。

通过这些方法,您可以有效地卸载 Service Worker。不过需要注意的是,卸载后相关的缓存功能将不再可用,如果是用户的设备上执行卸载操作,可能会影响到网站的离线功能和加载性能。因此,在执行卸载操作之前,请确保这是您所期望的。

2024年6月29日 12:07 回复

Removing Service Workers Programmatically:

You can remove service workers programmatically like this:

shell
navigator.serviceWorker.getRegistrations().then(registrations => { for (const registration of registrations) { registration.unregister(); } });

Docs: getRegistrations, unregister

Removing Service Workers Through The User Interface

You can also remove service workers under the Application tab in Chrome Devtools.

2024年6月29日 12:07 回复

You can also go to the URL: chrome://serviceworker-internals/ and unregister a serviceworker from there.

2024年6月29日 12:07 回复

You can do this through Chrome Developer Tool as well as Programatically.

  1. Find all running instance or service worker by typing

    chrome://serviceworker-internals/

    in a new tab and then select the serviceworker you want to unregister.

  2. Open Developer Tools (F12) and Select Application. Then Either

    Select Clear Storage -> Unregister service worker

    or

    Select Service Workers -> Choose Update on Reload

  3. Programatically

    if(window.navigator && navigator.serviceWorker) { navigator.serviceWorker.getRegistrations() .then(function(registrations) { for(let registration of registrations) { registration.unregister(); } }); }

Run code snippetHide results

Expand snippet

2024年6月29日 12:07 回复

In Google Chrome, you can go to Developer tools (F12) -> Application -> Service worker and unregister the service workers from the list for that specific domain.

Screenshot

This method is effective in development mode of a site and mostly they run on localhost which is you may need for other project's development.

2024年6月29日 12:07 回复

FYI, in case you are using MacOS Safari browser, there is one way to forcibly unregister a service worker (steps and images for Safari 12.1):

  1. Safari > Preferences... > Privacy > Manage Website Data…

    Safari Preferences : Privacy

  2. Enter domain name (ex. 'localhost'), click "Remove"

    Safari Preferences : Privacy : manage website data

Note: In addition to service workers, this also will erase all caches, cookies, and databases for this domain.

2024年6月29日 12:07 回复

你的答案