Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. Background Fetch API

Background Fetch API

Experimental:This is anexperimental technology
Check theBrowser compatibility table carefully before using this in production.

Note: This feature is available inWeb Workers.

TheBackground Fetch API provides a method for managing downloads that may take a significant amount of time such as movies, audio files, and software.

Concepts and Usage

When a web application requires the user to download large files, this often presents a problem in that the user needs to stay connected to the page for the download to complete. If they lose connectivity, close the tab or navigate away from the page the download stops.

TheBackground Synchronization API provides a way for service workers to defer processing until a user is connected; however it can't be used for long running tasks such as downloading a large file. Background Sync requires that the service worker stays alive until the fetch is completed, and to conserve battery life and to prevent unwanted tasks happening in the background the browser will at some point terminate the task.

The Background Fetch API solves this problem. It creates a way for a web developer to tell the browser to perform some fetches in the background, for example when the user clicks a button to download a video file. The browser then performs the fetches in a user-visible way, displaying progress to the user and giving them a method to cancel the download. Once the download is complete the browser then opens the service worker, at which point your application can do something with the response if required.

The Background Fetch API will enable the fetch to happen if the user starts the process while offline. Once they are connected it will begin. If the user goes off line, the process pauses until the user is on again.

Interfaces

BackgroundFetchManagerExperimental

A map where the keys are background fetch IDs and the values areBackgroundFetchRegistration objects.

BackgroundFetchRegistrationExperimental

Represents a Background Fetch.

BackgroundFetchRecordExperimental

Represents an individual fetch request and response.

BackgroundFetchEventExperimental

The event type for thebackgroundfetchabort andbackgroundfetchclick event

BackgroundFetchUpdateUIEventExperimental

The event type for thebackgroundfetchsuccess andbackgroundfetchfail event

Extensions to other interfaces

ServiceWorkerRegistration.backgroundFetchRead onlyExperimental

Returns a reference to aBackgroundFetchManager object, which manages background fetch operations.

backgroundfetchabort eventExperimental

Fired when a background fetch operation has been canceled by the user or the app.

backgroundfetchclick eventExperimental

Fired when the user has clicked on the UI for a background fetch operation.

backgroundfetchfail eventExperimental

Fired when at least one of the requests in a background fetch operation has failed.

backgroundfetchsuccess eventExperimental

Fired when all of the requests in a background fetch operation have succeeded.

Examples

Before using Background Fetch, check for browser support.

js
if (!("BackgroundFetchManager" in self)) {  // Provide fallback downloading.}

Using Background Fetch requires a registered service worker. Then callbackgroundFetch.fetch() to perform a fetch. Thisreturns a promise that resolves with aBackgroundFetchRegistration.

A background fetch may fetch a number of files. In our example the fetch requests an MP3 and a JPEG. This enables a package of files that the user sees as one item (for example a podcast and artwork) to be downloaded at once.

js
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,    },  );});

You can find further code examples and a demo inIntroducing Background Fetch.

Specifications

Specification
Background Fetch

Browser compatibility

api.BackgroundFetchManager

api.BackgroundFetchRegistration

api.BackgroundFetchRecord

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp