W3cubDocs

/Angular

SwUpdate

class final

Subscribe to update notifications from the Service Worker, trigger update checks, and forcibly activate updates.

class SwUpdate {
  versionUpdates: Observable<VersionEvent>
  available: Observable<UpdateAvailableEvent>
  activated: Observable<UpdateActivatedEvent>
  unrecoverable: Observable<UnrecoverableStateEvent>
  isEnabled: boolean
  checkForUpdate(): Promise<boolean>
  activateUpdate(): Promise<boolean>
}

See also

Provided in

Properties

Property Description
versionUpdates: Observable<VersionEvent> Read-Only

Emits a VersionDetectedEvent event whenever a new version is detected on the server.

Emits a VersionInstallationFailedEvent event whenever checking for or downloading a new version fails.

Emits a VersionReadyEvent event whenever a new version has been downloaded and is ready for activation.

available: Observable<UpdateAvailableEvent> Deprecated Read-Only

Emits an UpdateAvailableEvent event whenever a new app version is available.

Deprecated Use versionUpdates instead.

The behavior of available can be replicated by using versionUpdates 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,
    })));
activated: Observable<UpdateActivatedEvent> Deprecated Read-Only

Emits an UpdateActivatedEvent event whenever the app has been updated to a new version.

Deprecated Use the return value of SwUpdate#activateUpdate instead.

unrecoverable: Observable<UnrecoverableStateEvent> Read-Only

Emits an UnrecoverableStateEvent event whenever the version of the app used by the service worker to serve this client is in a broken state that cannot be recovered from without a full page reload.

isEnabled: boolean Read-Only

True if the Service Worker is enabled (supported by the browser and enabled via ServiceWorkerModule).

Methods

Checks for an update and waits until the new version is downloaded from the server and ready for activation.

checkForUpdate(): Promise<boolean>

Parameters

There are no parameters.

Returns

Promise<boolean>: a promise that

  • resolves to true if a new version was found and is ready to be activated.
  • resolves to false if no new version was found
  • rejects if any error occurs

Updates the current client (i.e. browser tab) to the latest version that is ready for activation.

activateUpdate(): Promise<boolean>

Parameters

There are no parameters.

Returns

Promise<boolean>: 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 latest version).
  • rejects if any error occurs

In most cases, you should not use this method and instead should update a client by reloading the page.

Updating a client without reloading can easily result in a broken application due to a version mismatch between the application shell and other page resources, such as lazy-loaded chunks, whose filenames may change between versions.

Only use this method, if you are certain it is safe for your specific use case.

© 2010–2023 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://angular.io/api/service-worker/SwUpdate