docs(service-worker): improve docs related to SwUpdate APIs (#46960)

This commit improves documentation related to recently improved or
deprecated `SwUpdate` APIs in the following ways:

- Update [check-for-update.service.ts][1] to make use of the return
  value of [SwUpdate#checkForUpdate()][2].
- Update [prompt-update.service.ts][3] to not call
  [SwUpdate#activateUpdate()][4] and just reload the page.
- Update the [SwUpdate#activateUpdate()][4] API docs to explain that it
  is only useful if you want to update a client without reloading and
  that it can easily lead to version skew.
- Update [a code-snippet][5] to no longer be [hard-coded][6].

[1]: https://github.com/angular/angular/blob/9d9d05911dbd6e2f30e4c7bced0e41fd20ec4285/aio/content/examples/service-worker-getting-started/src/app/check-for-update.service.ts#L16
[2]: https://angular.io/api/service-worker/SwUpdate#checkForUpdate
[3]: https://github.com/angular/angular/blob/9d9d05911dbd6e2f30e4c7bced0e41fd20ec4285/aio/content/examples/service-worker-getting-started/src/app/prompt-update.service.ts#L15
[4]: https://angular.io/api/service-worker/SwUpdate#activateUpdate
[5]: https://github.com/angular/angular/blob/96c6139c9ab35aa6ab2330a5a79a5906d5c2e8be/packages/service-worker/src/update.ts#L44-L54
[6]: https://angular.io/guide/docs-style-guide#hard-coded-snippets

Fixes #43665

PR Close #46960
This commit is contained in:
George Kalpakas
2022-08-26 21:26:22 +03:00
committed by Andrew Kushnir
parent f8e78e3f9b
commit c905da12e2
4 changed files with 66 additions and 28 deletions
+19 -12
View File
@@ -40,18 +40,11 @@ export class SwUpdate {
*
* @deprecated Use {@link versionUpdates} instead.
*
* The of behavior `available` can be rebuild by filtering for the `VersionReadyEvent`:
* ```
* import {filter, map} from 'rxjs/operators';
* // ...
* const updatesAvailable = swUpdate.versionUpdates.pipe(
* filter((evt): evt is VersionReadyEvent => evt.type === 'VERSION_READY'),
* map(evt => ({
* type: 'UPDATE_AVAILABLE',
* current: evt.currentVersion,
* available: evt.latestVersion,
* })));
* ```
* The behavior of `available` can be replicated by using `versionUpdates` by filtering for the
* `VersionReadyEvent`:
*
* {@example service-worker-getting-started/src/app/prompt-update.service.ts
* region='sw-replicate-available'}
*/
readonly available: Observable<UpdateAvailableEvent>;
@@ -124,6 +117,20 @@ export class SwUpdate {
* Updates the current client (i.e. browser tab) to the latest version that is ready for
* activation.
*
* In most cases, you should not use this method and instead should update a client by reloading
* the page.
*
* <div class="alert is-important">
*
* Updating a client without reloading can easily result in a broken application due to a version
* mismatch between the [application shell](guide/glossary#app-shell) and other page resources,
* such as [lazy-loaded chunks](guide/glossary#lazy-loading), whose filenames may change between
* versions.
*
* Only use this method, if you are certain it is safe for your specific use case.
*
* </div>
*
* @returns a promise that
* - resolves to `true` if an update was activated successfully
* - resolves to `false` if no update was available (for example, the client was already on the