SwoffSwoff

Adapter Blueprint

All Swoff events and properties — create adapters for any framework.

Swoff communicates with your framework through standard browser APIs: window events and CustomEvent details. If you know how to listen to a DOM event and expose reactive state in your framework, you can create an adapter.

No Swoff internals required.

The contract

Window events

These are the "sockets" Swoff provides. Listen with window.addEventListener(eventName, handler).

EventDetail shapeWhen dispatched
sw-progress{ percent: number }During background precaching
sw-auth-state-change{ type: "login" | "logout" | "refresh" | "clear" }Auth state changed
cache-invalidated{ tags: string[] }Cache entries invalidated (locally or from another tab)
mutation-sync-complete{ succeeded: number, failed: number }Offline mutation queue sync finished
mutation-queue-changedQueue contents changed (mutation added or processed)
background-sync-complete{ succeeded: number, failed: number }Background sync event finished
push-subscription-changed{ subscribed: boolean }Push subscription added or removed
push-permission-changed{ permission: NotificationPermission }Notification permission granted or denied
app-connectivity-change{ online: boolean }Verified connectivity changed (from connectivity-manager heartbeat)
swoff:offline-fallback{ route: string, fallbackLevel: string, timestamp: number }SW served an offline fallback page

Window properties

Swoff sets these properties on window for synchronous access:

PropertyTypePurpose
window.currentSWVersionstring | undefinedCurrently active SW version
window.latestSWVersionstring | undefinedLatest version available on server
window.swMinSupportedVersionstring | undefinedMinimum version that can still serve
window.swUpdateRequiredboolean | undefinedWhether the update is enforced

Adapter template

Every adapter follows the same pattern. Here's a minimal useSwoffFetch for any reactive framework:

// 1. Define the reactive state shape
interface CachedFetchState<T> {
  data: T | null;
  isLoading: boolean;
  error: Error | null;
  fromCache: boolean;
}

// 2. Create adapter using your framework's reactive primitives
//    React: useState + useEffect
//    Vue:   ref + onMounted
//    Svelte: writable + onMount

function useSwoffFetch(url: string) {
  // Initialize reactive state
  // Call swoff fetch on mount / url change
  // Listen to cache-invalidated to refetch
  // Return reactive state
}

The 16 hooks available in the React ecosystem show the complete pattern — inspect the generated files in swoff/adapters/ for reference.

Adapter list

These are the standard adapters. Each maps directly to the events above:

AdapterListens toExposes
useSwoffNetworkapp-connectivity-change, navigator.connection changeonline, wasOffline, lastChangedAt, effectiveType, downlink, isRetrying, retry
useSwoffFetchcache-invalidateddata, isLoading, error, fromCache, refetch
useSwoffPrecachesw-progressprogress
useSwoffAuthsw-auth-state-changeauthenticated, auth, online, setAuth, clearAuth
useSwoffQueuemutation-queue-changedpending, items, isProcessing, retryAll
useSwoffPushpush-subscription-changed, push-permission-changedsubscribed, permission, subscribe, unsubscribe
useSwoffSyncbackground-sync-completesupported, registered, triggerSync
useSwoffPwabeforeinstallpromptisInstallable, promptInstall
useSwoffPrefetchprefetch (calls prefetchCache)
useSwoffMutationmutate (calls fetchWithCache)
useSwoffMutationStatemutation-queue-changedstates, track, getState
useSwoffResetreset (calls resetSwoff)
useSwoffAnalyticsswoff:offline-fallback (or SW message OFFLINE_FALLBACK_ACTIVATED)lastEvent, events, clear
useSwoffStorageusage, quota, percentUsed

Prerequisites

  • Basic knowledge of your framework's reactive system (hooks, refs, stores, signals)
  • Familiarity with window.addEventListener and CustomEvent
  • The generated swoff/ directory in your project

How to add a new framework

  1. Create a file for your framework (e.g., frameworks/solid.mdx)
  2. Document the swoff.config.json values specific to that framework
  3. Link to this Blueprint for the adapter implementation
  4. Update frameworks/meta.json to include your file

On this page