ServiceWorkerRegistration: thuộc tính updateViaCache

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since October 2018.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Note: This feature is available in Web Workers.

Thuộc tính chỉ đọc updateViaCache của giao diện ServiceWorkerRegistration trả về giá trị của cài đặt được sử dụng để xác định các trường hợp mà trình duyệt sẽ tham vấn bộ nhớ đệm HTTP khi cố gắng cập nhật service worker hoặc bất kỳ tập lệnh nào được nhập qua importScripts().

Giá trị

Trả về một trong các giá trị sau:

  • imports, có nghĩa là bộ nhớ đệm HTTP không được tham vấn cho các cập nhật đối với tập lệnh service worker, nhưng được tham vấn cho các tập lệnh được nhập bằng importScripts(). Đây là giá trị mặc định.
  • all, có nghĩa là bộ nhớ đệm HTTP được tham vấn cho các cập nhật đối với tập lệnh service worker và cho các tập lệnh được nhập bằng importScripts().
  • none, có nghĩa là bộ nhớ đệm HTTP không bao giờ được tham vấn.

Ví dụ

Ví dụ sau đây cho thấy cách sử dụng updateViaCache.

js
if ("serviceWorker" in navigator) {
  navigator.serviceWorker
    .register("/service-worker.js", {
      updateViaCache: "none",
    })
    .then((registration) => {
      registration.addEventListener("updatefound", () => {
        // If updatefound is fired, it means that there's
        // a new service worker being installed.
        console.log(`Value of updateViaCache: ${registration.updateViaCache}`);
      });
    })
    .catch((error) => {
      console.error(`Service worker registration failed: ${error}`);
    });
}

Đặc tả kỹ thuật

Specification
Service Workers Nightly
# service-worker-registration-updateviacache

Tương thích trình duyệt

Xem thêm