docs.rodeo

MDN Web Docs mirror

ServiceWorkerContainer: register() method

{{APIRef("Service Workers API")}} {{SecureContext_Header}} {{AvailableInWorkers}} 

The register() method of the {{domxref("ServiceWorkerContainer")}}  interface creates or updates a {{domxref("ServiceWorkerRegistration")}}  for the given scope. If successful, the registration associates the provided script URL to a scope, which is subsequently used for matching documents to a specific service worker.

A single registration is created for each unique scope. If register() is called for a scope that has an existing registration, the registration is updated with any changes to the scriptURL or options. If there are no changes, then the existing registration is returned. Note that calling register() with the same scope and scriptURL does not restart the installation process. You can therefore call this method unconditionally from a controlled page: you don’t need to first check whether there’s an active registration or service worker.

A document can potentially be within the scope of several registrations with different service workers and options. The browser will associate the document with the matching registration that has the most specific scope. This ensures that only one service worker runs for each document.

[!NOTE] It is generally safer not to define registrations that have overlapping scopes.

Syntax

register(scriptURL)
register(scriptURL, options)

Parameters

Return value

A {{jsxref("Promise")}}  that resolves with a {{domxref("ServiceWorkerRegistration")}}  object.

Exceptions

Examples

The examples below should be read together to understand how service worker scope applies to a page.

Register a service worker with default scope

The following example uses the default value of scope by omitting it, which sets it to be the same location as the script URL.

Suppose the service worker code is at example.com/sw.js, and the registration code at example.com/index.html. The service worker code will control example.com/index.html, as well as pages underneath it, like example.com/product/description.html.

if ("serviceWorker" in navigator) {
  // Register a service worker hosted at the root of the
  // site using the default scope.
  navigator.serviceWorker.register("/sw.js").then(
    (registration) => {
      console.log("Service worker registration succeeded:", registration);
    },
    (error) => {
      console.error(`Service worker registration failed: ${error}`);
    },
  );
} else {
  console.error("Service workers are not supported.");
}

Note that we have registered the scriptURL relative to the site root rather than the current page. This allows the same registration code to be used from any page.

Register a service worker with an explicit default scope

The code below is almost identical, except we have specified the scope explicitly using { scope: "/" }. We’ve specified the scope as site-relative so the same registration code can be used from anywhere in the site.

if ("serviceWorker" in navigator) {
  // declaring scope manually
  navigator.serviceWorker.register("./sw.js", { scope: "/" }).then(
    (registration) => {
      console.log("Service worker registration succeeded:", registration);
    },
    (error) => {
      console.error(`Service worker registration failed: ${error}`);
    },
  );
} else {
  console.error("Service workers are not supported.");
}

This scope is the same as the default scope, so the registration applies to exactly the same pages as the previous example. Note that if we were to run this code after the previous example, browsers should recognize that we’re updating an existing registration rather than a new one.

Register a service worker using page-relative URLs

There is nothing to stop you from using page-relative URLs except that this makes it harder to move your pages around, and it is easy to accidentally create unwanted registrations if you do so.

In this example the service worker code is at example.com/product/sw.js, and the registration code at example.com/product/description.html. We’re using URLs that are relative to the current directory for the scriptURL and the scope, where the current directory is the base URL of the page that is calling register() (example.com/product/). The service worker applies to resources under example.com/product/.

if ("serviceWorker" in navigator) {
  // declaring scope manually
  navigator.serviceWorker.register("./sw.js", { scope: "./" }).then(
    (registration) => {
      console.log("Service worker registration succeeded:", registration);
    },
    (error) => {
      console.error(`Service worker registration failed: ${error}`);
    },
  );
} else {
  console.error("Service workers are not supported.");
}

Using Service-Worker-Allowed to increase service worker scope

A service worker can’t have a scope broader than its own location, unless the server specifies a broader maximum scope in a {{HTTPHeader("Service-Worker-Allowed")}}  header on the service worker script. Use the scope option when you need a narrower scope than the default.

The following code, if included in example.com/index.html, at the root of a site, would only apply to resources under example.com/product.

if ("serviceWorker" in navigator) {
  // declaring scope manually
  navigator.serviceWorker.register("./sw.js", { scope: "/product/" }).then(
    (registration) => {
      console.log("Service worker registration succeeded:", registration);
    },
    (error) => {
      console.error(`Service worker registration failed: ${error}`);
    },
  );
} else {
  console.error("Service workers are not supported.");
}

As noted above, servers can change the default scope by setting the Service-Worker-Allowed header on the service worker script. This allows the scope option to be set outside the path defined by the service worker’s location.

The following code, if included in example.com/product/index.html, would apply to all resources under example.com if the server set the Service-Worker-Allowed header to / or https://example.com/ when serving sw.js. If the server doesn’t set the header, the service worker registration will fail, as the requested scope is too broad.

if ("serviceWorker" in navigator) {
  // Declaring a broadened scope
  navigator.serviceWorker.register("./sw.js", { scope: "/" }).then(
    (registration) => {
      // The registration succeeded because the Service-Worker-Allowed header
      // had set a broadened maximum scope for the service worker script
      console.log("Service worker registration succeeded:", registration);
    },
    (error) => {
      // This happens if the Service-Worker-Allowed header doesn't broaden the scope
      console.error(`Service worker registration failed: ${error}`);
    },
  );
} else {
  console.error("Service workers are not supported.");
}

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN