run*_*ace 5 javascript google-chrome google-chrome-devtools service-worker progressive-web-apps
我创建了一个服务工作人员,在本地使用 chrome 对其进行了调试,并且工作正常。我将其部署到网站上,并看到下图中的错误(仅在 Chrome 中,Firefox 工作正常):
\n/static/core.jslib/sw.min.js:1 Failed to load the script unexpectedly\n/static/core.jslib/sw.min.js:1 Failed to load the script unexpectedly\n
Run Code Online (Sandbox Code Playgroud)\n使用 Chrome,如果我单击启动服务工作线程,它会立即停止。对于 Firefox,它会运行一段时间然后停止(预期)。
\nNetwork
或Application
我已经将我的服务人员简化为这样,所以现在我很确定这不是我的代码,而是 chromium 正在做的事情:
\n/static/core.jslib/sw.min.js:1 Failed to load the script unexpectedly\n/static/core.jslib/sw.min.js:1 Failed to load the script unexpectedly\n
Run Code Online (Sandbox Code Playgroud)\n注意:工作时,sw.min.js 文件会出现在源下(安装时),关闭并再次打开浏览器,然后不会显示:\n
PS 在 Ubuntu 20.04 上运行。也许与 Linux 中 Service Worker 的存储位置有关。
\n我什至添加了一个 fetch 调用来获取文件并在注册服务工作之前将其打印出来,以验证它可以访问该文件,而且总是没问题:
\nFetched /static/core.jslib/sw.min.js \nResponsebody: (...)bodyUsed: falseheaders: Headers\xc2\xa0{}ok: trueredirected: falsestatus: 200statusText: ""type: "basic"url: "https://www.demo..../static/core.jslib/sw.min.js"[[Prototype]]: Response\nswregister.min.50eb376e2ddc.js:1 SW: Registered - At scope https://www.demo..../\n/static/core.jslib/sw.min.js:1 Failed to load the script unexpectedly\n/static/core.jslib/sw.min.js:1 Failed to load the script unexpectedly\n
Run Code Online (Sandbox Code Playgroud)\n奇怪的是,当点击导航到其他地方时,就会出现错误。下面的控制台日志生成如下:
\n所以我不认为这是一个获取问题,我认为它是服务工作文件的内部存储,chrome 无法访问。
\n\nchrome://serviceworker-internals/` shows the service worker:\nScope: https://www.demo..../\nRegistration ID: 287\nNavigation preload enabled: false\nNavigation preload header length: 4\nActive worker:\nInstallation Status: ACTIVATED\nRunning Status: STOPPED\nFetch handler existence: EXISTS\nScript: https://www.demo..../static/core.jslib/sw.min.js\nVersion ID: 649\nRenderer process ID: 0\nRenderer thread ID: -1\nDevTools agent route ID: -2\nLog:\n
Run Code Online (Sandbox Code Playgroud)\n如果我点击start
它会显示:
Console: {"lineNumber":0,"message":"Failed to load the script unexpectedly",\n"message_level":3,"sourceIdentifier":1,"sourceURL":"https://www.demo.../static/core.jslib/sw.min.js"}\n
Run Code Online (Sandbox Code Playgroud)\n而其他人的服务人员则启动并运行一段时间。
\n以下是响应标头sw.min.js
# Here are the response headers for:\ndate: Tue, 25 Jan 2022 18:15:44 GMT\netag: "61f03d46-350"\nlast-modified: Tue, 25 Jan 2022 18:11:18 GMT\nserver: notepad.exe\nservice-worker-allowed: /\n
Run Code Online (Sandbox Code Playgroud)\n{\n "name": "Cube",\n "short_name": "Cube",\n "icons": [\n {\n "src": "/static/core.pwa/img/android-chrome-192x192.png",\n "sizes": "192x192",\n "type": "image/png",\n "purpose": "any"\n },\n {\n "src": "/static/core.pwa/img/android-chrome-512x512.png",\n "sizes": "512x512",\n "type": "image/png",\n "purpose": "any"\n },\n {\n "src": "/static/core.pwa/img/maskable_icon.png",\n "sizes": "1024x1024",\n "type": "image/png",\n "purpose": "maskable"\n }\n ],\n "theme_color": "#212121",\n "background_color": "#ffffff",\n "display": "standalone",\n "start_url": "/"\n}\n
Run Code Online (Sandbox Code Playgroud)\n
Chromium 的人员发现这是 MIME 内容类型标头: https://bugs.chromium.org/p/chromium/issues/detail ?id=1292379#c17
在 nginx 中,我将标头更改为:
content-type: application/javascript; charset=utf-8
到
content-type: application/javascript
现在可以了!
注册服务工作者时,我将类型设置为模块。显然这在 Chrome 中意味着“重新启动时崩溃”。
const options = {
scope: "/",
// type: "module", <-- needed to comment this out
};
const result = await navigator.serviceWorker.register(swPath, options);
Run Code Online (Sandbox Code Playgroud)
显然Chrome似乎不支持模块类型的服务工作者,当我检查支持表时,似乎有很好的支持。
归档时间: |
|
查看次数: |
1607 次 |
最近记录: |