angular-cli: [app-shell] Doesn't work together with @angular/service-worker: "Error: Hash mismatch (cacheBustedFetchFromNetwork)" (+ explanation, + solution)
cc: @alxhub @StephenFluin
Versions
Angular CLI: 1.6.0
Node: 8.2.1
OS: darwin x64
Angular: 5.1.0
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, platform-server, router
@angular/cli: 1.6.0
@angular-devkit/build-optimizer: 0.0.35
@angular-devkit/core: 0.0.22
@angular-devkit/schematics: 0.0.41
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.9.0
@schematics/angular: 0.1.10
@schematics/schematics: 0.0.10
typescript: 2.4.2
webpack: 3.10.0
Repro steps
Generate a new app with --service-worker, then generate Universal app, then generate app-shell (if it fails, use workaround from https://github.com/angular/angular-cli/issues/8793) and run in the browser
Observed behavior
At /ngsw/state:
NGSW Debug Info:
Driver state: EXISTING_CLIENTS_ONLY (Degraded due to failed initialization: Hash mismatch (cacheBustedFetchFromNetwork): http://localhost:8080/index.html: expected e809040220500c7210a197856aa3ecdf8ddd3618, got 100d6731232af53d7a592198666378360e223ba9 (after cache busting)
Error: Hash mismatch (cacheBustedFetchFromNetwork): http://localhost:8080/index.html: expected e809040220500c7210a197856aa3ecdf8ddd3618, got 100d6731232af53d7a592198666378360e223ba9 (after cache busting)
at PrefetchAssetGroup.cacheBustedFetchFromNetwork (http://localhost:8080/ngsw-worker.js:657:27)
at <anonymous>)
Latest manifest hash: 0f59e15ff1beb35d4ff048bb6cc056e34917081f
Last update check: 6m4s739u
Desired behavior
Same run/update SW behavior as with non-app-shell node
Mention any other details that might be useful (optional)
It happens because app-shell version of index.html
generated after the generation of ngsw.json
, where the hash of index.html
located. So ngsw.json
contains an incorrect hash, this is why @angular/service-worker throws this error after checking the integrity. Solution: make ngsw.json
generation a last step in the flow.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 6
- Comments: 18 (1 by maintainers)
Commits related to this issue
- fix: remove Service Worker temporarily, due to the existing cli bug (angular/angular-cli#8794) — committed to PoiScript/solomon by PoiScript 7 years ago
- fix(@angular-devkit/build-angular): re-run ngsw after modifications in app-shell Fix https://github.com/angular/angular-cli/issues/8794 — committed to filipesilva/angular-cli by filipesilva 6 years ago
- fix(@angular-devkit/build-angular): re-run ngsw after modifications in app-shell Fix https://github.com/angular/angular-cli/issues/8794 — committed to filipesilva/angular-cli by filipesilva 6 years ago
- fix(@angular-devkit/build-angular): re-run ngsw after modifications in app-shell Fix https://github.com/angular/angular-cli/issues/8794 — committed to filipesilva/angular-cli by filipesilva 6 years ago
- fix(@angular-devkit/build-angular): re-run ngsw after modifications in app-shell Fix https://github.com/angular/angular-cli/issues/8794 — committed to filipesilva/angular-cli by filipesilva 6 years ago
- Update angular dependencies to fix issue where ngsw was not run for app-shell (https://github.com/angular/angular-cli/issues/8794) — committed to Voya100/VoyaCode by Voya100 6 years ago
I realized that in my case, I used angular universal to pre-rending my app, something like this. It will modify the
index.html
and then change the file hash.For example, before pre-rending, the sha1 hash for
index.html
wasa85416190bba14b95f37685b1b0d7ccc8251f982
which matched thengsw.json
and it changed to92cb3d0c8dd68b84e1747bdaa73982ecb8315748
when the pre-rending was done.Just like @webmaxru said, you have to manually generate the
ngsw.json
again to make sure the hash matches:So I suggest that the CLI should have some options like
--skip-service-worker
and--service-worker-only
to let the user decide when to use the NGSW.Priority: 1 (urgent)… meanwhile 26 days later. 😄 I just disabled SW until this is fixed. Great that there is a workaround out there though.
I have a similar problem where ngsw.json contains a number of wrong hashes (for .svg fonts and .css). Running ngsw-config again does not fix it. Could this be related?
When I fix these hashes manually and also apply the fix as detailed here: https://stackoverflow.com/a/48732922/4167438 then my app works offline, otherwise I get 504’s on the main page and hash mismatches on the style and fonts.
currently using cli 1.6.8 and @angular/* 5.2.5
Yes, unfortunately, this affects not only the SW, but the whole app. But there is a kill-switch for NGSW: just remove
ngsw.json
from your deployment, and NGSW will clean-up all the caches and de-register itself.