Context
Angular PWA.
Before explaining the error, I have to specify :
- We read the Angular service worker documentation
- We read this post, specially the validated answer as it looks like our situation (but that didn't really answer our questions ).
- All's working well during local test (ng build + http-server to serve the app).
- We don't use Angular's "environment" files. Instead, we're using an "appsettings.json" which is overidding inside our CI to use the same artifact and prevents multiple build.
Globally, inside our CI : Build (ngsw.json is generated) -> overriding appsettings.json -> deploy. Yes, appsettings is overidded after building (so, after ngsw.json
generation). But, before or after such a generation : appsettings.json content is the same: the generated hash has the right/same value between each deployment. Also, such a story's working locally without any issues (build -> change appsettings.json content -> serve).
Our ngsw-config.json
looks like this :
{
"$schema": "./node_modules/@angular/service-worker/config/schema.json",
"index": "/index.html",
"assetGroups": [
{
"name": "app",
"installMode": "prefetch",
"resources": {
"files": [
"/favicon.ico",
"/index.html",
"/appsettings.json", // The added line
"/manifest.webmanifest",
"/*.css",
"/*.js"
]
}
},
{
"name": "assets",
"installMode": "lazy",
"updateMode": "prefetch",
"resources": {
"files": [
"/assets/**",
"/*.(svg|cur|jpg|jpeg|png|apng|webp|avif|gif|otf|ttf|woff|woff2)"
]
}
}
]
}
Problem
After each deployment (made by our CI), we faced with this error :
Failed to install app version '30ba317c7d27a544efb0c7eb47653aa0fa664fb9'
Error: Hash mismatch (cacheBustedFetchFromNetwork): https://....../appsettings.json: expected 92e837ce30053ba552fcf37c8b34b5d1b1f5f06e, got 9756c02e19847ca03c3cf2d677c65e0a4e9665ef (after cache busting)
at PrefetchAssetGroup.cacheBustedFetchFromNetwork (https://....../ngsw-worker.js:474:21)
at async PrefetchAssetGroup.fetchFromNetwork (https://....../ngsw-worker.js:449:19)
at async PrefetchAssetGroup.fetchAndCacheOnce (https://....../ngsw-worker.js:428:21)
It seems service worker found a wrong hash for appsettings.
However, when the app's checking an update, the resulting ngsw.json
file is correct (we compared with the latest deployed files in our server) : we didn't find the hash that the service worker got (9756c02e19847ca03c3cf2d677c65e0a4e9665ef
). Once again : no hash issue locally, even after editing appsettings.
Questions
Of course, there is something we don't understand. Inside the Angular documentation, the "hashed content" part's saying :
If a particular file fails validation, the Angular service worker attempts to re-fetch the content using a "cache-busting" URL parameter to prevent browser or intermediate caching.
What is behind this "validation" ? Is there a "rehash" operation somewhere (which would explain this unknown hash we got) ? We didn't find any explanation about this. If its the case, that doesn't explain why such a story's working locally. As we understood (if we're not goats...), the only "hashing" process is during "ng build"... but maybe we're wrong.