You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Workbox构建的Service Worker默认缓存存储名称?

自定义Workbox预缓存的缓存存储名称

可以修改预缓存的缓存存储名称,只需在调用precacheAndRoute时传入包含cacheName的配置选项即可。

修改后的代码如下:

import { registerRoute, NavigationRoute } from "workbox-routing";
import { precacheAndRoute, createHandlerBoundToURL } from "workbox-precaching";

precacheAndRoute(
  [
    {
      url: "/index.html",
      revision: ...autogenerated...,
    },
    {
      url: "/proxy.html",
      revision: ...autogenerated...,
    },
  ],
  {
    cacheName: "html-assets" // 指定自定义缓存名称
  }
);

const handler = createHandlerBoundToURL("/index.html");
const navigationRoute = new NavigationRoute(handler, {
  denylist: [new RegExp("/proxy.html")]
});
registerRoute(navigationRoute);

这样修改后,index.html和proxy.html就会被预缓存到名为html-assets的缓存存储中,替代原来默认的workbox-precache-v2-<my domain>名称,同时不会影响原有离线功能和路由逻辑的正常运行。

内容的提问来源于stack exchange,提问作者Alexander Nestorov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 08:25:00