Skip to content

配置参考

完整 TypeScript 类型定义见 packages/core/src/types.ts

Vite 与 Webpack 插件的配置类型 ViteResourceFallbackOptions / WebpackPluginOptions 均等同于 PluginOptions

PluginOptions

字段类型默认值说明
rulesFallbackRule[]必填回退规则数组,按顺序匹配,重复 match 以最后一条为准
defaults{ retry?, circuit? }所有规则的默认重试/熔断配置
debugboolean | 'auto''auto'true 始终打印日志;'auto' 通过 localStorage.__RF_DEBUG__ 控制
sri'strip' | 'keep' | 'strict''strip'fallback 时对 integrity 属性的处理策略
enableDevbooleanfalse开发模式下是否启用
noncestring附加到注入的 <script> 标签的 CSP nonce
externalRuntimebooleanfalse将运行时作为外链引入而非内联
externalRuntimePathstring'/__rf/runtime.js'外链运行时的路径
injectPreconnectbooleantrue为每个 fallback 域名注入 <link rel="preconnect">
htmlInject'head-prepend' | 'head-append''head-prepend'注入到 <head> 的位置
serviceWorkerboolean | ServiceWorkerOptionsfalse启用 Hybrid SW,接管非脚本子资源和受控 CSS @import
hooksRuntimeHooksJS 函数钩子(仅 externalRuntime 模式可用)
disableGlobalsstring[]['__RF_DISABLE__']额外的 kill-switch 全局变量名
disableQueryParamstring'__rf'值为 off 时禁用运行时的查询参数名
disableCookiestring'__rf_disable'值为 1 时禁用运行时的 cookie 名

FallbackRule

字段类型默认值说明
matchstring | RegExp | (url) => boolean必填URL 匹配模式。string 为前缀匹配
urlsstring[]必填有序候选 URL 前缀列表。最后一个通常为回源地址
retryRetryOptions见下表覆盖该规则的重试配置
circuitCircuitOptions见下表覆盖该规则的熔断配置

match 模式说明

  • string — 前缀匹配(区分大小写)
  • RegExp — 对 URL 做正则测试
  • 函数 — 针对每个 URL 自行决定(构建期序列化时仅支持 string 或 RegExp)

RetryOptions

字段类型默认值说明
maxnumber2同一 URL 的最大重试次数
baseDelaynumber300首次重试延迟(ms)
maxDelaynumber3000指数退避的延迟上限(ms)
jitterbooleantrue为延迟添加 ±25% 随机抖动

CircuitOptions

字段类型默认值说明
thresholdnumber5同一 host 连续失败多少次后触发熔断
cooldownnumber30000熔断后冷却时长(ms),到期后重新尝试
shareAcrossTabsbooleantrue通过 localStorage 跨标签页共享熔断状态
storageTtlnumber120000localStorage 中熔断条目的存活时长(ms)

ServiceWorkerOptions

Hybrid SW 默认关闭。启用后,Vite/Webpack 插件会生成资源 manifest,并输出 SW asset;SW bundle 会预置 manifest/config(保留 RegExp 规则语义),页面 runtime 负责注册 SW、补发配置,并把 SW postMessage 事件桥接为现有 rf:* 事件。

ts
resourceFallback({
  rules: [...],
  serviceWorker: {
    scope: '/',
    includeStyleImports: true,
    fallbackOnOpaque: false,
    cache: { enabled: true, cacheOpaque: false },
  },
});
字段类型默认值说明
enabledbooleantrue(对象配置时)设为 false 可在对象配置中关闭
pathstring跟随 scope,如 //rf-sw.js/app//app/rf-sw.jsSW 文件路径。默认与 scope 同层,避免依赖 Service-Worker-Allowed 响应头
scopestring'/'SW 控制范围
includeStyleImportsbooleantrue允许 SW 在 request.destination === 'style' 且 referrer 命中 CSS manifest 时接管 CSS @import
fallbackOnOpaquebooleanfalse将跨源 opaque response 视为失败继续 fallback。适合 CDN 错误被浏览器隐藏成 opaque 的图片/CSS 子资源场景;开启后可能跳过本来可用的 opaque CDN 响应
cache.enabledbooleantruefallback 网络链路成功后写入 Cache API
cache.cacheOpaquebooleanfalse是否缓存 opaque response。默认不缓存

缓存策略

缓存策略固定为保守模式:只缓存 fallback 成功后的可读 2xx 响应;网络 retry/fallback 全部失败后,才读取当前 manifest version 对应的 cache 兜底;新 manifest version 激活后会清理旧的 resource-fallback-* cache。manifest version 会纳入资源、fallback rules 和关键 SW cache 策略,避免 rules 或 cache 配置变化后继续命中旧 cache。

SW 熔断器独立性

SW 内部 resolver 的熔断器始终使用独立内存状态,即使页面侧 defaults.circuit.shareAcrossTabstrue,SW 也不会读写 localStorage。若 SW fetch 链路最终 reject,会发出 rf:error 并返回 Response.error(),保持浏览器侧资源表现接近真实 network error。

配置示例

ts
resourceFallback({
  rules: [
    {
      match: 'https://cdn.example.com/',
      urls: ['https://cdn-backup.example.com/', 'https://static.mysite.com/', '/'],
      retry: { max: 2, baseDelay: 300, maxDelay: 3000, jitter: true },
      circuit: { threshold: 3, cooldown: 30000 },
    },
  ],
  defaults: {
    retry: { max: 2 },
    circuit: { threshold: 5, cooldown: 30000 },
  },
  debug: 'auto',
  sri: 'strip',
  nonce: 'my-csp-nonce',
  injectPreconnect: true,
  htmlInject: 'head-prepend',
});

相关文档