web api interface
BackgroundFetchRegistration
Limited availabilityAvailable in workers
The BackgroundFetchRegistration interface of the Background Fetch API represents an individual background fetch.
A BackgroundFetchRegistration instance is returned by the fetch() or get() methods, and therefore there has no constructor.
Instance properties
Also inherits properties from its parent, EventTarget.
idRead only Experimental- : A string containing the background fetch’s ID.
uploadTotalRead only Experimental- : A
Numbercontaining the total number of bytes to be uploaded.
- : A
uploadedRead only Experimental- : A
Numbercontaining the size in bytes successfully sent, initially0.
- : A
downloadTotalRead only Experimental- : A
Numbercontaining the total size in bytes of this download. This is the value set when the background fetch was registered, or0.
- : A
downloadedRead only Experimental- : A
Numbercontaining the size in bytes that has been downloaded, initially0.
- : A
resultRead only Experimental- : Returns an empty string initially, on completion either the string
"success"or"failure".
- : Returns an empty string initially, on completion either the string
failureReasonRead only Experimental- : A string with a value that indicates a reason for a background fetch failure. Can be one of the following values:
"","aborted","bad-status","fetch-error","quota-exceeded","download-total-exceeded".
- : A string with a value that indicates a reason for a background fetch failure. Can be one of the following values:
recordsAvailableRead only Experimental- : A
Booleanindicating whether therecordsAvailableflag is set.
- : A
Instance methods
Also inherits methods from its parent, EventTarget.
abort()Experimental- : Aborts the background fetch. Returns a
Promisethat resolves withtrueif the fetch was successfully aborted.
- : Aborts the background fetch. Returns a
match()Experimental- : Returns a single
BackgroundFetchRecordobject which is the first match for the arguments.
- : Returns a single
matchAll()Experimental- : Returns a
Promisethat resolves with an array ofBackgroundFetchRecordobjects containing requests and responses.
- : Returns a
Events
Also inherits events from its parent, EventTarget.
Listen to these events using addEventListener() or by assigning an event listener to the oneventname property of this interface.
progressExperimental- : Fired when there is a change to any of the following properties:
uploaded,downloaded,resultorfailureReason.
- : Fired when there is a change to any of the following properties:
Examples
The following code creates a BackGroundFetchRegistration as bgFetch, with an id of "my-fetch".
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",
},
],
downloadTotal: 60 * 1024 * 1024,
},
);
});
Logging the id to the console returns "my-fetch".
console.log(bgFetch.id); // "my-fetch"
The match() method can be used to find a particular BackgroundFetchRecord from those that are part of the registration.
bgFetch.match("/ep-5.mp3").then(async (record) => {
if (!record) {
console.log("No record found");
return;
}
console.log(`Here's the request`, record.request);
const response = await record.responseReady;
console.log(`And here's the response`, response);
});