这个问题通常发生在Angular应用程序中启用了Service Worker,并且在更新应用程序时遇到哈希值不匹配的错误。为了解决这个问题,可以使用以下步骤:
ng build --prod --output-hashing=all
注意,在构建时添加 --output-hashing=all
参数,这样会为每个文件生成哈希值。
ng g service-worker
{
"index": "/index.html",
"assetGroups": [
{
"name": "app",
"installMode": "prefetch",
"resources": {
"files": [
"/favicon.ico",
"/index.html",
"/*.(css|js)"
],
"urls": [
"https://fonts.googleapis.com/**"
]
}
},
{
"name": "assets",
"installMode": "lazy",
"updateMode": "prefetch",
"resources": {
"files": [
"/assets/**",
"/*.(svg|png|jpg)"
]
}
}
]
}
ngsw-config.json
文件中设置 hashTable
选项"hashTable": {
"/favicon.ico": "hash",
"/index.html": "hash",
"/main.js": "hash",
"/polyfills.js": "hash",
"/runtime.js": "hash",
"/styles.js": "hash",
"/vendor.js": "hash"
}
http-server -p 8080 -c-1 dist/my-project
完成上述步骤后,现在就应该可以在更新应用时避免哈希值不匹配的问题。