当前行为
我做到了ng add @angular/pwa
,我在生产构建中遇到了这个错误
Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
at Driver.<anonymous> (ngsw-worker.js:2297)
at Generator.next (<anonymous>)
at fulfilled (ngsw-worker.js:1752)
我做了一些研究,发现
/ngsw.json?ngsw-cache-bust=0.08769372894975769
GET 请求存在问题。虽然 fetchLatestManifest 返回 200 状态码,但响应实际上不是 JSON ,而是它的无脚本版本index.html
您可以看到响应状态为 200/ngsw.json?ngsw-cache-bust=0.08769372894975769
但实际响应不是所需的 JSON
我尝试在 chrome Actual ``` 中复制作为 fetch 选项
fetch("https://officehawk.com/ngsw.json?ngsw-cache-bust=0.7927584506157848", {"credentials":"omit","headers":{},"referrer":"https://officehawk.com/ngsw-worker.js","referrerPolicy":"no-referrer-when-downgrade","body":null,"method":"GET","mode":"cors"});
``` 我?ngsw-cache-bust=0.7927584506157848
从 fetch 的 URL 部分中删除了,我可以看到响应是 JSON。
编辑了```
fetch("https://officehawk.com/ngsw.json", {"credentials":"omit","headers":{},"referrer":"https://officehawk.com/ngsw-worker.js","referrerPolicy":"no-referrer-when-downgrade","body":null,"method":"GET","mode":"cors"});
``` 这样你现在就可以看到 ngsw.json 的内容了
我怀疑它必须在这个函数中处理? https://github.com/angular/angular/blob/0b4d85e9f19246af68a75140afc4db3d97f9ddfd/packages/service-worker/worker/src/driver.ts#L645
环境
角度版本:6.0.0
浏览器:- [x] Chrome(桌面)版本 67