MDN Web Docs

Syntax

js

update()

Parameters

None.

Return value

A Promise that resolves with a ServiceWorkerRegistration object.

Examples

The following simple example registers a service worker example then adds an event handler to a button so you can explicitly update the service worker whenever desired:

js

if ("serviceWorker" in navigator) {
  navigator.serviceWorker
    .register("/sw.js", { scope: "/" })
    .then((registration) => {
      // registration worked
      console.log("Registration succeeded.");
      button.onclick = () => {
        registration.update();
      };
    })
    .catch((error) => {
      // registration failed
      console.error(`Registration failed with ${error}`);
    });
}

Specifications

Specification
Service Workers Nightly
# service-worker-registration-update

Browser compatibility

See also

Help improve MDN

Yes No

Learn how to contribute

This page was last modified on by MDN contributors.

Read the original on developer.mozilla.org ↗