如何在 Google Devtools 中强制更新 service worker 文件?

How to force update the service worker file in Google Devtools?

我试图强制重新加载新的 Service Worker 文件,但它没有自动加载。我已经选中了开发工具 "Application" 选项卡中的 "Update on reload" 复选框,并在 "Sources" 选项卡中禁用了缓存。

我正在使用 Chrome 版本“75.0.3770.100”。

我让它工作的唯一方法是再次选中和取消选中,另一种方法是单击 "skipWaiting"

是否有我遗漏的东西或我应该启用的标志?

感谢您的帮助!

如果你能在 Devtools 中看到 skipWaiting 选项是因为你没有在安装事件中调用 self.skipWaiting()。通常新更新的 service worker 会等到旧版本不再控制任何客户端。 self.skipWaiting() 阻止了这种等待。这是一个例子。

//This is the install event listener
self.addEventListener("install", (event) => {
    console.log(self);
    event.waitUntil(
        caches.open(CACHE_NAME).then(cache => {
            cache.addAll(STATIC_ASSETS).then(() => {
                    self.skipWaiting();
                }).catch(error => {
                    logForDevelopment("An error occured while adding static assets to the cache", true);
                    logForDevelopment(error, true);
                })
        })
    );
    logForDevelopment("Service Worker: Installed");
});