大网数据广告活动
Web 标准与前端开发

BackgroundFetchRegistration

· 52CE 我爱测

The **`BackgroundFetchRegistration`** interface of the Background Fetch API represents an individual background fetch.

Background Fetch API

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.

Instance properties

Also inherits properties from its parent, EventTarget.

  • BackgroundFetchRegistration.id
    • : A string containing the background fetch's ID.
  • BackgroundFetchRegistration.uploadTotal
    • : A Number containing the total number of bytes to be uploaded.
  • BackgroundFetchRegistration.uploaded
    • : A Number containing the size in bytes successfully sent, initially 0.
  • BackgroundFetchRegistration.downloadTotal
    • : A Number containing the total size in bytes of this download. This is the value set when the background fetch was registered, or 0.
  • BackgroundFetchRegistration.downloaded
    • : A Number containing the size in bytes that has been downloaded, initially 0.
  • BackgroundFetchRegistration.result
    • : Returns an empty string initially, on completion either the string "success" or "failure".
  • BackgroundFetchRegistration.failureReason
    • : 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".
  • BackgroundFetchRegistration.recordsAvailable
    • : A Boolean indicating whether the recordsAvailable flag is set.

Instance methods

Also inherits methods from its parent, EventTarget.

  • BackgroundFetchRegistration.abort()
    • : Aborts the background fetch. Returns a Promise that resolves with true if the fetch was successfully aborted.
  • BackgroundFetchRegistration.match()
    • : Returns a single BackgroundFetchRecord object which is the first match for the arguments.
  • BackgroundFetchRegistration.matchAll()
    • : Returns a Promise that resolves with an array of BackgroundFetchRecord objects containing requests and responses.

Events

Also inherits events from its parent, EventTarget.

Listen to these events using EventTarget.addEventListener or by assigning an event listener to the oneventname property of this interface.

  • BackgroundFetchRegistration/progress_event
    • : Fired when there is a change to any of the following properties: BackgroundFetchRegistration.uploaded, BackgroundFetchRegistration.downloaded, BackgroundFetchRegistration.result or BackgroundFetchRegistration.failureReason.

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 BackgroundFetchRegistration.id to the console returns "my-fetch".

console.log(bgFetch.id); // "my-fetch"

The BackgroundFetchRegistration.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

Browser compatibility

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。