首页 > 解决方案 > Angular 9 PWA 哈希不匹配(cacheBustedFetchFromNetwork)

问题描述

我有一个角度 PWA,它不会离线启动(http 错误 504)。当我检查 /ngsw/state 我得到这个错误:

Error: Hash mismatch (cacheBustedFetchFromNetwork): https://example.com/favicon.ico: expected 566d8535a3adc2f7210a8a890bdc50ec4f91f0e3, got 8e076950c4c615772d8d5d753e07377bab9f2f27 (after cache busting)
    at PrefetchAssetGroup.<anonymous> (https://example.com/ngsw-worker.js:734:35)
    at Generator.next (<anonymous>)
    at fulfilled (https://example.com/ngsw-worker.js:174:62))
Latest manifest hash: 5952e5e67f8b4ee7115ef9d519a346712927b608

使用 Angular CLI:9.1.9 节点:12.16.1 Angular:9.1.11

PWA 上的灯塔检查没有给我任何错误,并且在我使用 .htaccess 的 apache 服务器上重定向角度路由

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

RewriteRule ^(.*) /index.html [NC,L]

任何人的想法?谢谢!

标签: angularprogressive-web-appsangular-service-worker

解决方案


就我而言,问题与我使用 git 部署应用程序(使用 post-receive git hook)这一事实有关。默认情况下,git 会替换行尾。这导致服务器上的文件与原始文件不同,它们的哈希值也不同。

我比较了服务器上文件 index.html 的哈希值,它与“ngsw.json”文件中的值不同。

    sha1sum index.html
    04e691dbfae931bfd24513ac63d833bdfee3a1f6  index.html

在我禁用行尾转换后:

    git config --local core.autocrlf false

并再次推送项目问题消失了,sha1sum index.html 向我显示了“ngsw.json”中的保存哈希。


推荐阅读