BackgroundFetchManager.fetch()
The fetch()
method of the BackgroundFetchManager
interface returns a Promise
that resolves with a BackgroundFetchRegistration
object for a supplied array of URLs and Request
objects.
Syntax
let backgroundFetchRegistration = BackgroundFetchManager.fetch(id, requests [,options]);
Parameters
id
-
A developer-defined identifier that can be passed to the other methods to retrieve a
backgroundFetchRegistration
. requests
-
A
RequestInfo
object or an array of such objects. -
options
Optional -
A
BackgroundFetchOptions
object.
Return value
A Promise
that resolves with a BackgroundFetchRegistration
object.
Exceptions
- TypeError
-
Raised if no request is provided, if the mode of a request is 'no-cors', if no service worker is present, a request already exists with the requested
id
, or the request fails. - DOMException
-
This method may raise a
DOMException
of the following types:Exception Description AbortError
Indicates the fetch was aborted. NotAllowedError
Indicates that user permission has not been granted to make background fetches.
Examples
The following examples shows how to use fetch()
to create a BackgroundFetchRegistration
. With an active service worker
, use the 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', }], downloadTotal: 60 * 1024 * 1024, }); });
Specifications
Specification |
---|
Background Fetch # background-fetch-manager-fetch |
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 | |
fetch |
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/BackgroundFetchManager/fetch