Offline shell & mutation outbox

An opt-in service worker caches the shell and immutable assets. Explicit mutations can also use a persistent outbox. Disabled by default — no worker or registration is emitted.

Enable with swelte.serviceWorker.enabled: true in svelte.config.js or SWELTE_SW=1 in the env (env wins). See docs/config.md §Service worker + §Offline fallback page.

What the build emits

  • Additive dist/sw.js only — the single-artifact default never changes.
  • Hand-rolled precache (no workbox dep): / + hashed assets/*.js + assets/*.wasm; cache version = version stamp, only this registration’s old caches are purged on activate.
  • offline.fallback page precached only when configured and the prerender pass wrote it.

Registration

  • register: 'auto' (default) injects a same-origin registration snippet.
  • register: 'manual' emits dist/sw.js but skips the snippet — the app registers itself.

Runtime scope

  • GET navigations + immutable assets are cache-first with SPA-shell offline fallback.
  • Accept: application/json / text/event-stream / POST / /api/* are network-only — offline the caller sees the failure, never stale JSON.
  • #lib/ WASM rides the same immutable-asset cache.
  • Hand serves exact dist/ files (incl. sw.js) regardless of Accept — real files win over the SPA-first proxy rule.

Persistent mutation outbox

svelte.config.js
export default {
  swelte: {
    serviceWorker: {
      enabled: true,
      outbox: { paths: ['/api/save'], maxAttempts: 5, maxAgeMs: 86400000 }
    }
  }
}

The allowlist matches whole path prefixes relative to swelte.base, on the worker's origin. POST, PUT, PATCH and DELETE are supported. Ordinary failed requests are not automatically enqueued; call enqueueOffline from a browser handler after the worker takes control.

browser handler
import { enqueueOffline, flushOutbox, listOutbox, retryOutbox, discardOutbox } from '@swelte/core'

// Call from a browser handler, after the service worker takes control.
const id = await enqueueOffline('/api/save', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ title: 'Draft' })
})
await flushOutbox()
const pending = await listOutbox()
// retryOutbox(id) resets the budget, keeping the Idempotency-Key.
// discardOutbox(id) explicitly removes the entry.

Enqueue resolves after an IndexedDB transaction commits and returns a stable UUID. Every attempt, including a manual retry, sends that UUID as Idempotency-Key. The endpoint must deduplicate it: a response can be lost after a mutation commits. Delivery is at least once.

Retries and retained failures

  • Requests preserve URL, method, headers and body: up to 1 MiB per request and 1000 records.
  • Replay is sequential. IndexedDB leases prevent overlapping workers from claiming the same request; leases expire after 60 seconds and requests time out after 30 seconds.
  • 2xx acknowledgements delete records. Network errors, 408, 425, 429 and 5xx retry with exponential delays from 1 second to 1 hour. Retry-After can extend the delay up to 24 hours.
  • Other statuses, redirects, expiry and exhausted attempts retain a failed record. Pending retries hold later work; failed records do not block it.
  • Defaults are five attempts and 24 hours. Bounds are 1–20 attempts and 1 second–30 days.

Background Sync schedules retries where supported. After its first use, the SDK also flushes on online. Call flushOutbox() on app startup or provide a retry control in browsers without Background Sync. It returns true while retryable work remains.

listOutbox() returns metadata without bodies or headers. retryOutbox(id) resets the attempt and age budget while preserving the key; discardOutbox(id) explicitly removes an entry. Worker messages of type swelte:outbox:changed can trigger a UI refresh. The database survives worker upgrades and restarts, subject to browser storage eviction or removal.

Credentials and account changes

Credentials default to omit. Explicit credentials: 'same-origin' replays with current cookies; caller-supplied Authorization headers are persisted. Authenticated apps must discard queued entries on sign-out or account switches. Swelte does not infer app identity.