angular-cn/packages/service-worker/worker/src
Alex Rickabaugh 395109817b fix(service-worker): properly handle invalid hashes in all scenarios (#21288)
When the SW fetches URLs listed in a manifest with hashes, it checks
the content hash against the manifest to make sure it has the correct
version of the URL. In the event of a mismatch, the SW is supposed to
consider the manifest invalid, and avoid using it. There are 3 cases
to consider by which this can happen.

Case 1: during the initial SW installation, a manifest is activated
without waiting for every URL to be fully loaded. In the background,
every prefetch URL listed by the manifest is requested and cached.
One such prefetch request could fail the hash test, and cause the
manifest to be treated as invalid. In such a case, the SW should
enter a state of EXISTING_CLIENTS_ONLY, as the latest manifest is
invalid.

This case works today.

Case 2: during the initial SW installation, as in Case 1, a manifest
is activated without waiting for each URL to fully load. However,
it's possible that the application could request a URL with a bad
hash before background initialization tries to load that URL. This
happens if, for example, the application has a broken index.html.

In this case, the SW should enter a state of EXISTING_CLIENTS_ONLY,
and serve the request from the network instead.

What happens today is that the internal error escapes the SW and
is returned as a rejected Promise to respondWith(), causing a
browser-level error that the site cannot be loaded, breaking the
site.

This change allows the SW to detect the error and enter the correct
state, falling back on the network if needed.

Case 3: during checkForUpdate(), the SW will try to fully cache the
new update before making it the latest version. Failure here is
complicated - if the page fails to load due to transient network
conditions (timeouts, 500s, etc), then it makes sense to continue
serving the existing cached version, and attempt to activate the
update on the next cycle.

If the page fails due to non-transient conditions though (400 error,
hash mismatch, etc), then the SW should consider the updated
manifest invalid, and enter a state of EXISTING_CLIENTS_ONLY.

Currently, all errors are treated as transient.

This change causes the SW to treat all errors during updates as
non-transient, which can cause the SW to unnecessarily enter a
safe mode. A future change can allow the SW to remain in normal mode
if the error is provably transient.

PR Close #21288
2018-01-10 12:18:24 -08:00
..
adapter.ts fix(service-worker): several misc fixes for corner cases 2017-10-05 13:27:31 -07:00
api.ts fix(service-worker): several misc fixes for corner cases 2017-10-05 13:27:31 -07:00
app-version.ts build: upgrade to TypeScript 2.6 (#21144) 2017-12-22 20:15:47 -08:00
assets.ts fix(service-worker): properly handle invalid hashes in all scenarios (#21288) 2018-01-10 12:18:24 -08:00
data.ts fix(service-worker): freshness strategy should clone response for cache (#19764) 2017-10-18 11:18:34 -07:00
database.ts feat(service-worker): introduce the @angular/service-worker package (#19274) 2017-09-28 16:18:12 -07:00
db-cache.ts build: upgrade to TypeScript 2.6 (#21144) 2017-12-22 20:15:47 -08:00
debug.ts fix(service-worker): several misc fixes for corner cases 2017-10-05 13:27:31 -07:00
driver.ts fix(service-worker): properly handle invalid hashes in all scenarios (#21288) 2018-01-10 12:18:24 -08:00
error.ts fix(service-worker): properly handle invalid hashes in all scenarios (#21288) 2018-01-10 12:18:24 -08:00
idle.ts fix(service-worker): several misc fixes for corner cases 2017-10-05 13:27:31 -07:00
manifest.ts feat(service-worker): introduce the @angular/service-worker package (#19274) 2017-09-28 16:18:12 -07:00
msg.ts feat(service-worker): introduce the @angular/service-worker package (#19274) 2017-09-28 16:18:12 -07:00
service-worker.d.ts build: upgrade to TypeScript 2.6 (#21144) 2017-12-22 20:15:47 -08:00
sha1.ts fix(service-worker): several misc fixes for corner cases 2017-10-05 13:27:31 -07:00
util.ts fix(service-worker): several misc fixes for corner cases 2017-10-05 13:27:31 -07:00