Does Angular's service worker verify cached API data it serves offline?
0 reputation · 12 Apr 2020, 09:43 UTC
0 reputation · 12 Apr 2020, 09:43 UTC
In an Angular PWA, the service worker generated from ngsw-config.json hash-verifies static assets against the generated manifest, so a corrupted asset served while offline is detected and treated as missing. A dataGroups entry can similarly cache API GET responses, letting the app keep rendering data after the network is gone — effectively the recovery source for that data.
The open point: the documented hash check covers assets, not dataGroup responses. Recovered API entries appear to be served as-is until maxAge or maxSize expires, and how a 304 Not Modified interacts with an existing cached entry seems to depend on the configured strategy (performance vs freshness). The documentation does not appear to settle this, so treat the detail as needing verification against the current version.
Assuming Angular 17–20 with @angular/service-worker:
strategy: "freshness", is a 304 treated as a valid revalidation that keeps the cached entry, or can stale data persist?33275 reputation · 12 Apr 2020, 14:41 UTC
Angular’s ngsw does not perform an integrity check on responses stored in dataGroups when it serves them offline. The hash-based validation mechanism is applied exclusively to the assetGroups section, which covers static files listed in ngsw-config.json. For API responses, the service worker returns the cached body as-is until the entry becomes stale according to maxAge or maxSize.
When the strategy is set to "freshness", the service worker (SW) attempts a network revalidation before serving cached data:
GET request (utilizing If-None-Match or If-Modified-Since headers).304 Not Modified, the SW treats this as a successful revalidation and retains the existing cached entry.Because the Angular service worker does not verify the body or custom headers of a cached API response, the application must implement its own validation if data integrity is critical. Accepted patterns include:
ETag or Last-Modified headers to detect staleness, or storing a checksum in a custom header (e.g., X-Checksum) that the Angular application compares against a locally computed hash of the response body.To verify this behavior in your environment, you can use the following scoped steps:
# 1. Build the PWA with a dataGroup configured in ngsw-config.json
ng build --configuration production
# 2. Use Chrome DevTools → Application → Service Workers
# Confirm the dataGroup entry exists in the Cache Storage.
# 3. Simulate offline mode via the Network tab.
# Observe that the request is fulfilled by the service worker without hash validation.
# 4. Configure backend to send ETag/Last-Modified headers.
# Verify that a 304 response results in the SW continuing to use the cached entry.
This behavior is consistent with Angular 16 and 17. While generally stable, specific behavior may vary if you have implemented a custom service worker script that overrides the default @angular/service-worker logic. If you are using a custom sw.js instead of the generated ngsw-worker.js, the caching and validation logic will depend entirely on your manual implementation.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.