ServiceWorkerRegistration:updateViaCache 屬性

Baseline 已廣泛支援

此特性已成熟穩定,適用於多種裝置和瀏覽器版本。自 2018 年 10 月起,它已在各瀏覽器中可用。

安全上下文: 此功能僅在安全上下文(HTTPS)中可用,且支援此功能的瀏覽器數量有限。

注意:此功能在 Web Workers 中可用。

ServiceWorkerRegistration 介面的只讀屬性 updateViaCache 返回一個設定值,該設定用於確定在瀏覽器嘗試更新 service worker 或透過 importScripts() 匯入的任何指令碼時,將在何種情況下查閱 HTTP 快取。

返回以下值之一

  • imports,表示在更新 service worker 指令碼時不會查閱 HTTP 快取,但在使用 importScripts() 匯入指令碼時會查閱 HTTP 快取。這是預設值。
  • all,表示在更新 service worker 指令碼以及在使用 importScripts() 匯入指令碼時都會查閱 HTTP 快取。
  • none,表示從不查閱 HTTP 快取。

示例

以下示例展示了 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}`);
    });
}

規範

規範
Service Workers
# service-worker-registration-updateviacache

瀏覽器相容性

另見