files/en-us/web/api/backgroundfetchmanager/fetch/index.md
{{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.
fetch(id, requests)
fetch(id, requests, options)
id
requests
: A RequestInfo object or an array of RequestInfo objects.
Each RequestInfo object is a {{domxref("Request")}} object or a string that will be given as the input argument to the {{domxref("Request.Request()", "Request()")}} constructor.
options {{optional_inline}}
title {{optional_inline}}
icons {{optional_inline}}
src
sizes {{optional_inline}}
sizes attribute of the {{HTMLElement("link")}} element.type {{optional_inline}}
label {{optional_inline}}
downloadTotal {{optional_inline}}
: A number representing the estimated total download size, in bytes, for the fetch operation. This is used to show the user how big the download is and to show the user download progress.
As soon as the total download size exceeds downloadTotal, then the fetch is aborted.
A {{jsxref("Promise")}} that resolves with a {{domxref("BackgroundFetchRegistration")}} object.
no-cors, if no service worker is present, a request already exists with the requested id, or the request fails.AbortError {{domxref("DOMException")}}
NotAllowedError {{domxref("DOMException")}}
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}}
{{Compat}}