docs(service-worker): add section to explain unrecoverable state (#36847)

PR Close #36847
This commit is contained in:
Sonu Kapoor 2020-08-02 19:31:01 -04:00 committed by Joey Perrott
parent 036a2faf02
commit f5d1e9a2d1
2 changed files with 44 additions and 0 deletions

View File

@ -0,0 +1,17 @@
import { Injectable } from '@angular/core';
import { SwUpdate } from '@angular/service-worker';
function notifyUser(message: string): void { }
// #docregion sw-unrecoverable-state
@Injectable()
export class HandleUnrecoverableStateService {
constructor(updates: SwUpdate) {
updates.unrecoverable.subscribe(event => {
notifyUser(
`An error occurred that we cannot recover from:\n${event.reason}\n\n` +
'Please reload the page.');
});
}
}
// #enddocregion sw-unrecoverable-state

View File

@ -67,6 +67,33 @@ Therefore, it is recommended to reload the page once the promise returned by `ac
</div>
### Handling an unrecoverable state
In some cases, the version of the app used by the service worker to serve a client might be in a broken state that cannot be recovered from without a full page reload.
For example, imagine the following scenario:
- A user opens the app for the first time and the service worker caches the latest version of the app.
Let's assume the app's cached assets include `index.html`, `main.<main-hash-1>.js` and `lazy-chunk.<lazy-hash-1>.js`.
- The user closes the app and does not open it for a while.
- After some time, a new version of the app is deployed to the server.
This newer version includes the files `index.html`, `main.<main-hash-2>.js` and `lazy-chunk.<lazy-hash-2>.js` (note that the hashes are different now, because the content of the files has changed).
The old version is no longer available on the server.
- In the meantime, the user's browser decides to evict `lazy-chunk.<lazy-hash-1>.js` from its cache.
Browsers may decide to evict specific (or all) resources from a cache in order to reclaim disk space.
- The user opens the app again.
The service worker serves the latest version known to it at this point, namely the old version (`index.html` and `main.<main-hash-1>.js`).
- At some later point, the app requests the lazy bundle, `lazy-chunk.<lazy-hash-1>.js`.
- The service worker is unable to find the asset in the cache (remember that the browser evicted it).
Nor is it able to retrieve it from the server (since the server now only has `lazy-chunk.<lazy-hash-2>.js` from the newer version).
In the above scenario, the service worker is not able to serve an asset that would normally be cached.
That particular app version is broken and there is no way to fix the state of the client without reloading the page.
In such cases, the service worker notifies the client by sending an `UnrecoverableStateEvent` event.
You can subscribe to `SwUpdate#unrecoverable` to be notified and handle these errors.
<code-example path="service-worker-getting-started/src/app/handle-unrecoverable-state.service.ts" header="handle-unrecoverable-state.service.ts" region="sw-unrecoverable-state"></code-example>
## More on Angular service workers
You may also be interested in the following: