BackgroundFetchRegistration
The BackgroundFetchRegistration interface of the Background Fetch API represents an individual background fetch.
A BackgroundFetchRegistration instance is returned by the BackgroundFetchManager.fetch() or BackgroundFetchManager.get() methods, and therefore there has no constructor.
Properties
The following properties are available synchronously, as convenience properties copied from those in the BackgroundFetchRegistration instance.
-
BackgroundFetchRegistration.idRead only -
A
stringcontaining the background fetch's ID. -
BackgroundFetchRegistration.uploadTotalRead only -
A
numbercontaining the total number of bytes to be uploaded. -
BackgroundFetchRegistration.uploadedRead only -
A
numbercontaining the size in bytes successfully sent, initially0. -
BackgroundFetchRegistration.downloadTotalRead only -
A
numbercontaining the total size in bytes of this download. This is the value set when the background fetch was registered, or0. -
BackgroundFetchRegistration.downloadedRead only -
A
numbercontaining the size in bytes that has been downloaded, initially0. -
BackgroundFetchRegistration.resultRead only -
Returns an empty string initially, on completion either the string
"success"or"failure". -
BackgroundFetchRegistration.failureReasonRead only -
One of the following strings:
""-
The background fetch has not completed, or was successful.
"aborted"-
The operation was cancelled by the user, or
abort()was called. "bad-status"-
A response had a not-ok status (a status outside the range 200-299).
"fetch-error"-
A fetch failed for other reasons, for example CORS, or a network failure.
"quota-exceeded"-
Storage quota was reached during the operation.
"download-total-exceeded"-
The provided
downloadTotalwas exceeded. This value was set when the background fetch was registered.
-
BackgroundFetchRegistration.recordsAvailableRead only -
A
booleanindicating whether therecordsAvailableflag is set.
Event handlers
BackgroundFetchRegistration.onprogress-
Fired when there is a change to any of the following properties:
uploadeddownloadedresultfailureReason
Methods
BackgroundFetchRegistration.abort()-
Aborts the background fetch. Returns a
Promisethat resolves withtrueif the fetch was successfully aborted. BackgroundFetchRegistration.match()-
Returns a single
BackgroundFetchRecordobject which is the first match for the arguments. BackgroundFetchRegistration.matchAll()-
Returns a
Promisethat resolves with an array ofBackgroundFetchRecordobjects containing requests and responses.
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); });
Specifications
| Specification |
|---|
| Background Fetch # background-fetch-registration |
Browser compatibility
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
BackgroundFetchRegistration |
74 |
79 |
No |
No |
62 |
No |
No |
74 |
No |
53 |
No |
11.0 |
abort |
74 |
79 |
No |
No |
62 |
No |
No |
74 |
No |
53 |
No |
11.0 |
downloaded |
74 |
79 |
No |
No |
62 |
No |
No |
74 |
No |
53 |
No |
11.0 |
downloadTotal |
74 |
79 |
No |
No |
62 |
No |
No |
74 |
No |
53 |
No |
11.0 |
failureReason |
74 |
79 |
No |
No |
62 |
No |
No |
74 |
No |
53 |
No |
11.0 |
id |
74 |
79 |
No |
No |
62 |
No |
No |
74 |
No |
53 |
No |
11.0 |
match |
74 |
79 |
No |
No |
62 |
No |
No |
74 |
No |
53 |
No |
11.0 |
matchAll |
74 |
79 |
No |
No |
62 |
No |
No |
74 |
No |
53 |
No |
11.0 |
onprogress |
74 |
79 |
No |
No |
62 |
No |
No |
74 |
No |
53 |
No |
11.0 |
recordsAvailable |
74 |
79 |
No |
No |
62 |
No |
No |
74 |
No |
53 |
No |
11.0 |
result |
74 |
79 |
No |
No |
62 |
No |
No |
74 |
No |
53 |
No |
11.0 |
uploaded |
74 |
79 |
No |
No |
62 |
No |
No |
74 |
No |
53 |
No |
11.0 |
uploadTotal |
74 |
79 |
No |
No |
62 |
No |
No |
74 |
No |
53 |
No |
11.0 |
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/BackgroundFetchRegistration