BackgroundFetchRegistration

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

Note: This feature is available in Web Workers.

Giao diện BackgroundFetchRegistration của Background Fetch API biểu diễn một tác vụ tìm nạp nền riêng lẻ.

Một thực thể BackgroundFetchRegistration được trả về bởi các phương thức BackgroundFetchManager.fetch() hoặc BackgroundFetchManager.get(), vì vậy giao diện này không có hàm khởi tạo.

EventTarget BackgroundFetchRegistration

Thuộc tính thực thể

Cũng kế thừa các thuộc tính từ phần tử cha của nó, EventTarget.

BackgroundFetchRegistration.id Read only Experimental

Một chuỗi chứa ID của tác vụ tìm nạp nền.

BackgroundFetchRegistration.uploadTotal Read only Experimental

Một number chứa tổng số byte sẽ được tải lên.

BackgroundFetchRegistration.uploaded Read only Experimental

Một number chứa kích thước theo byte đã được gửi thành công, ban đầu là 0.

BackgroundFetchRegistration.downloadTotal Read only Experimental

Một number chứa tổng kích thước theo byte của lượt tải xuống này. Đây là giá trị được đặt khi tác vụ tìm nạp nền được đăng ký, hoặc 0.

BackgroundFetchRegistration.downloaded Read only Experimental

Một number chứa kích thước theo byte đã được tải xuống, ban đầu là 0.

BackgroundFetchRegistration.result Read only Experimental

Ban đầu trả về một chuỗi rỗng; khi hoàn tất sẽ là chuỗi "success" hoặc "failure".

BackgroundFetchRegistration.failureReason Read only Experimental

Một chuỗi có giá trị cho biết nguyên nhân khiến tác vụ tìm nạp nền thất bại. Có thể là một trong các giá trị sau: "", "aborted", "bad-status", "fetch-error", "quota-exceeded", "download-total-exceeded".

BackgroundFetchRegistration.recordsAvailable Read only Experimental

Một boolean cho biết cờ recordsAvailable có được đặt hay không.

Phương thức thực thể

Cũng kế thừa các phương thức từ phần tử cha của nó, EventTarget.

BackgroundFetchRegistration.abort() Experimental

Hủy tác vụ tìm nạp nền. Trả về một Promise phân giải thành true nếu việc tìm nạp được hủy thành công.

BackgroundFetchRegistration.match() Experimental

Trả về một đối tượng BackgroundFetchRecord duy nhất, là kết quả khớp đầu tiên cho các đối số.

BackgroundFetchRegistration.matchAll() Experimental

Trả về một Promise phân giải thành một mảng các đối tượng BackgroundFetchRecord chứa các yêu cầu và phản hồi.

Sự kiện

Cũng kế thừa các sự kiện từ phần tử cha của nó, EventTarget.

Lắng nghe các sự kiện này bằng addEventListener() hoặc bằng cách gán một trình xử lý sự kiện vào thuộc tính oneventname của giao diện này.

progress Experimental

Được kích hoạt khi có thay đổi ở bất kỳ thuộc tính nào sau đây: uploaded, downloaded, result hoặc failureReason.

Ví dụ

Đoạn mã sau tạo một BackgroundFetchRegistration có tên bgFetch, với id"my-fetch".

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

Ghi id ra console sẽ trả về "my-fetch".

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

Phương thức match() có thể được dùng để tìm một BackgroundFetchRecord cụ thể trong số các bản ghi thuộc đăng ký này.

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

Thông số kỹ thuật

Specification
Background Fetch
# background-fetch-registration

Khả năng tương thích với trình duyệt