docs.rodeo

MDN Web Docs mirror

BackgroundFetchManager: fetch() method

{{APIRef("Background Fetch API")}} {{SeeCompatTable}} {{AvailableInWorkers}} 

The fetch() method of the {{domxref("BackgroundFetchManager")}}  interface initiates a background fetch operation, given one or more URLs or {{domxref("Request")}}  objects.

Syntax

fetch(id, requests)
fetch(id, requests, options)

Parameters

Return value

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

Exceptions

Examples

The following example shows how to use fetch() to initiate a background fetch operation. With an active {{domxref('ServiceWorker', 'service worker', "", "nocode")}} , use the {{domxref('ServiceWorkerRegistration.backgroundFetch')}}  property to access the BackgroundFetchManager object and call its fetch() method.

navigator.serviceWorker.ready.then(async (swReg) => {
  const bgFetch = await swReg.backgroundFetch.fetch(
    "my-fetch",
    ["/ep-5.mp3", "ep-5-artwork.jpg"],
    {
      title: "Episode 5: Interesting things.",
      icons: [
        {
          sizes: "300x300",
          src: "/ep-5-icon.png",
          type: "image/png",
          label: "Downloading a show",
        },
      ],
      downloadTotal: 60 * 1024 * 1024,
    },
  );
});

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

In this article

View on MDN