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

浏览器兼容性

另见