如何让 Service Worker 在 Chrome Lighthouse 上工作?

Joe*_*oel 7 lighthouse

我正在尝试在 Chrome Lighthouse 上审核我的应用程序,但我无法让 Service Worker 工作。它已注册并运行,没有错误,但是当我尝试运行 Lighthouse 时,它​​被卡住并控制台记录以下错误:

Service worker not registered  DOMException: Failed to register a ServiceWorker for scope ('https://www.localhost.com/') with script ('https://www.localhost.com/sw.js'): Operation has been aborted
Run Code Online (Sandbox Code Playgroud)

SW 的调用方式为:

    if('serviceWorker' in navigator){
    let sw = 'sw.js';
    navigator.serviceWorker.register(sw, {scope: "/"})
        .then(function(){
            console.log('Service worker registered.');
        })
        .catch(function(e){
            console.log('Service worker not registered ', e);
        })
}
Run Code Online (Sandbox Code Playgroud)

在清单中:

start_url: "/",
    scope: "/",
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

“../”、“https://www.localhost.com/”、“./”、

我正在使用以下标志运行 chrome:

"C:\Program Files\Google\Chrome\Application\chrome.exe" --ignore-certificate-errors --unsafely-treat-insecure-origin-as-secure=https://www.localhost.com
Run Code Online (Sandbox Code Playgroud)

但是,我还尝试使用 https 在远程服务器中运行该应用程序,但出现了相同的错误。

你能帮忙的话,我会很高兴。

Joe*_*oel 8

如果我取消选中灯塔选项中的清除缓存选项,它就会开始工作。

编辑:正如 Sean McCarthy 在下面提到的,正确的名称是“清除存储”

  • 除了我之外,这里也一样,它被称为“清除存储”。[此 GitHub 问题评论](https://github.com/GoogleChrome/lighthouse/issues/13236#issuecomment-959888243) 向我展示了在哪里执行此操作。 (3认同)