Skip to content

Configuration Reference

Full TypeScript types: packages/core/src/types.ts.

Both Vite (ViteResourceFallbackOptions) and Webpack (WebpackPluginOptions) plugins use PluginOptions.

PluginOptions

FieldTypeDefaultDescription
rulesFallbackRule[]RequiredFallback rule array, matched in order; last match wins for duplicates
defaults{ retry?, circuit? }Default retry/circuit config for all rules
debugboolean | 'auto''auto'true always logs; 'auto' controlled via localStorage.__RF_DEBUG__
sri'strip' | 'keep' | 'strict''strip'Strategy for handling integrity during fallback
enableDevbooleanfalseWhether to activate in dev mode
noncestringCSP nonce appended to the injected <script> tag
externalRuntimebooleanfalseLoad runtime as external script instead of inline
externalRuntimePathstring'/__rf/runtime.js'Path for the external runtime script
injectPreconnectbooleantrueInject <link rel="preconnect"> for each fallback domain
htmlInject'head-prepend' | 'head-append''head-prepend'Position in <head> for injection
serviceWorkerboolean | ServiceWorkerOptionsfalseEnable Hybrid SW for non-script subresources and controlled CSS @import
hooksRuntimeHooksJS function hooks (only available in externalRuntime mode)
disableGlobalsstring[]['__RF_DISABLE__']Additional kill-switch global variable names
disableQueryParamstring'__rf'Query param name that disables runtime when set to off
disableCookiestring'__rf_disable'Cookie name that disables runtime when set to 1

FallbackRule

FieldTypeDefaultDescription
matchstring | RegExp | (url) => booleanRequiredURL matching pattern. String uses prefix matching
urlsstring[]RequiredOrdered candidate URL prefix list. Last one is typically the origin
retryRetryOptionsSee belowOverride retry config for this rule
circuitCircuitOptionsSee belowOverride circuit breaker config for this rule

match pattern

  • string — prefix match (case-sensitive)
  • RegExp — regex test against URL
  • function — per-URL decision (build-time serialization supports string or RegExp only)

RetryOptions

FieldTypeDefaultDescription
maxnumber2Max retries per URL
baseDelaynumber300Initial retry delay (ms)
maxDelaynumber3000Exponential backoff delay cap (ms)
jitterbooleantrueAdd ±25% random jitter to delay

CircuitOptions

FieldTypeDefaultDescription
thresholdnumber5Consecutive failures on the same host before tripping the circuit
cooldownnumber30000Cooldown duration after circuit trip (ms), then retry
shareAcrossTabsbooleantrueShare circuit state across tabs via localStorage
storageTtlnumber120000TTL for circuit entries in localStorage (ms)

ServiceWorkerOptions

Hybrid SW is disabled by default. When enabled, Vite/Webpack plugins generate a resource manifest and emit a SW asset. The SW bundle preloads manifest/config (preserving RegExp rule semantics), while the page runtime registers the SW, sends follow-up config updates, and bridges SW postMessage events into existing rf:* events.

ts
resourceFallback({
  rules: [...],
  serviceWorker: {
    scope: '/',
    includeStyleImports: true,
    fallbackOnOpaque: false,
    cache: { enabled: true, cacheOpaque: false },
  },
});
FieldTypeDefaultDescription
enabledbooleantrue for object configSet to false to disable from an object config
pathstringDerived from scope, e.g. //rf-sw.js, /app//app/rf-sw.jsSW file path. Default stays inside scope to avoid requiring Service-Worker-Allowed
scopestring'/'SW control scope
includeStyleImportsbooleantrueLet SW handle CSS @import when request.destination === 'style' and referrer matches a CSS manifest asset
fallbackOnOpaquebooleanfalseTreat cross-origin opaque responses as failures and continue fallback. Useful when CDN errors are hidden as opaque responses; may skip otherwise usable opaque CDN responses
cache.enabledbooleantrueWrite to Cache API after a fallback network response succeeds
cache.cacheOpaquebooleanfalseWhether to cache opaque responses. Disabled by default

Cache policy

Conservative by design: only readable 2xx responses from successful fallback are cached; manifest-version cache is read only after network retry/fallback is exhausted; old resource-fallback-* caches are cleaned when a new manifest version activates. Manifest version includes resources, fallback rules, and key SW cache policy.

SW circuit breaker isolation

The SW resolver always uses an isolated in-memory circuit breaker. Even if page-side defaults.circuit.shareAcrossTabs is true, the SW does not read or write localStorage. If the SW fetch chain ultimately rejects, it emits rf:error and returns Response.error().

Example configuration

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',
});