SwoffSwoff

Service Worker

Full schema for swoff.config.json features.serviceWorker — every field, its type, default, and description.

features.serviceWorker

FieldTypeDefaultDescription
autoActivatebooleanfalseAutomatically activate newly registered service workers (skipWaiting()). When false, the SW activates on next navigation or browser reload — no user prompt.
precacheobjectControls how assets are precached (see below).

features.serviceWorker.precache

Controls the background precache behavior.

FieldTypeDefaultDescription
concurrencynumber1Number of assets to download simultaneously during background precaching. Higher values (e.g. 5) speed up large precaches but may increase bandwidth contention. All assets (build output, fallback routes, per-route fallbacks, manifest) are cached in the background after activation — nothing is cached during the install event.
delayMsnumber0Delay in milliseconds between starting individual asset requests within a batch. Spaces out requests to avoid network saturation.

features.serviceWorker.strategy

FieldTypeDefaultDescription
defaultstring"cache-first"Default caching strategy (lowest priority in 3-tier resolution). One of: cache-first, network-first, stale-while-revalidate, cache-only, network-only, reactive
patternsobject{}Per-route strategy overrides. Keys are URL patterns (e.g. /api/*). Values can be a strategy name string or a StrategyEntry object. Set to "network-only" for routes you want to pass through to the network entirely — useful when using in-memory caching libraries for data routes while keeping offline capability everywhere else.
reactive.staleTimenumber0Global stale time in seconds for reactive strategy. Data is considered fresh for this long; after expiry, SW serves cached but triggers a background refresh. 0 = always stale.
reactive.refetchIntervalnumber0Global auto-refetch interval in seconds for reactive strategy. SW periodically re-fetches matching routes in the background. 0 disables.
reactive.refetchOnReconnectbooleanfalseGlobal default — refetch reactive entries when the browser comes back online
reactive.refetchOnFocusbooleanfalseGlobal default — refetch reactive entries when the tab gains focus
maxRuntimeCacheAgenumber2592000Maximum age in seconds for runtime cache entries. 0 disables eviction. Default 2592000 (30 days).
normalizeKeybooleanfalseWhen true, query params are sorted alphabetically in cache keys so ?b=1&a=2 and ?a=2&b=1 resolve to the same entry.
ignoreQueryParamsstring[][]Query params to strip from cache keys (e.g. ["_t", "utm_source"]). Prevents cache-busting params from creating duplicate cache entries.
timeoutnumber10Network fetch timeout in seconds. The SW's _fetch wraps fetch() with an AbortController that aborts after this duration. On timeout or network error, a SW_NOTIFICATION is broadcast (level: "error", code: "FETCH_FAILED"), and the strategy falls through to its cache fallback.
storageThresholdnumber80Storage quota warning threshold (percent). When navigator.storage.estimate().percentUsed exceeds this value after SW init, a STORAGE_QUOTA_HIGH notification is dispatched. Range: 0–100.

features.serviceWorker.navigation

FieldTypeDefaultDescription
mode"spa" | "default" | "ssr""spa"Navigation mode. "spa": serves global fallback directly from serveFromCache() (no runtime HTML caching). "default": no special navigation handling — strategies handle all requests equally. "ssr": checks HTML cache → per-route fallback → global fallback via navFallback(); adds auto-prefetch that intercepts history.pushState/replaceState to warm the SW cache on client-side navigation. Non-navigation subresources (JS, CSS, images, API) return 502 Bad Gateway instead of entering the HTML fallback chain.
preloadbooleantrueEnable Navigation Preload API — reduces SW startup latency
fallbackstring""Global fallback HTML path for offline navigation. For SPA mode, set to "/index.html" to serve the SPA shell from precache when offline. For SSR mode, checked by navFallback() after per-route rules if the runtime HTML cache misses.
precacheRoutesstring[][]Additional routes to precache in the background after activation (e.g. ["/", "/about"]). Useful for SSG or critical pages — cached alongside scanned build assets.
rulesNavigationRule[][]Per-route offline fallback pages (see below). Rules provide the fallback path used by navFallback() when strategy dispatch fails; they do not override the caching strategy. Navigation-only — non-subresource requests skip the rule check and return 502.
FieldTypeDefaultDescription
matchstring(required)Glob pattern matching request paths (supports *, **, ?, {a,b}).
fallbackstringPer-route offline fallback HTML path. Used in the ultimate fallback chain, checked before the global fallback.

Example — per-route fallback rules

"navigation": {
  "mode": "ssr",
  "fallback": "/offline.html",
  "rules": [
    { "match": "/blog/*", "fallback": "/blog-offline.html" },
    { "match": "/dashboard/**", "fallback": "/dashboard-offline.html" }
  ]
}

StrategyEntry object

When a strategy value is an object instead of a string:

"/api/*": {
  "strategy": "reactive",
  "staleTime": 30,
  "refetchOnFocus": true
}
FieldTypeDefaultDescription
strategystring(required)Caching strategy name. One of: cache-first, network-first, stale-while-revalidate, cache-only, network-only, reactive
staleTimenumberStale time in seconds. Reactive-only — only valid when strategy is "reactive". Data is considered fresh for this long; after expiry, SW serves cached but triggers a background refresh. 0 = always stale.
refetchIntervalnumberAuto-refetch interval in seconds. Reactive-only. The SW periodically re-fetches this route in the background. 0 disables.
refetchOnReconnectbooleanfalseRefetch when the browser comes back online. Reactive-only.
refetchOnFocusbooleanfalseRefetch when the browser tab gains focus. Reactive-only.
timeoutnumberOverride the global fetch timeout for this specific pattern (in seconds).

Resolution tiers

strategy and staleTime resolve in 3 tiers (highest to lowest priority):

  1. Per-request — passed as options to fetchWithCache({ strategy }) or useSwoffFetch(), or via X-SW-Strategy / X-SW-Stale-Time headers
  2. Route pattern — configured in features.serviceWorker.strategy.patterns
  3. Global default — configured at features.serviceWorker.strategy.reactive.* (for staleTime) or features.serviceWorker.strategy.default (for strategy)

refetchInterval, refetchOnReconnect, and refetchOnFocus are SW-initiated (interval timers, online/focus event handlers) and resolve in 2 tiers only:

  1. Route pattern — configured in the pattern entry
  2. Global default — configured at features.serviceWorker.strategy.reactive.*

On this page