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).
| Event | Detail shape | When 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-changed | — | Queue 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:
| Property | Type | Purpose |
|---|---|---|
window.currentSWVersion | string | undefined | Currently active SW version |
window.latestSWVersion | string | undefined | Latest version available on server |
window.swMinSupportedVersion | string | undefined | Minimum version that can still serve |
window.swUpdateRequired | boolean | undefined | Whether 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:
| Adapter | Listens to | Exposes |
|---|---|---|
useSwoffNetwork | app-connectivity-change, navigator.connection change | online, wasOffline, lastChangedAt, effectiveType, downlink, isRetrying, retry |
useSwoffFetch | cache-invalidated | data, isLoading, error, fromCache, refetch |
useSwoffPrecache | sw-progress | progress |
useSwoffAuth | sw-auth-state-change | authenticated, auth, online, setAuth, clearAuth |
useSwoffQueue | mutation-queue-changed | pending, items, isProcessing, retryAll |
useSwoffPush | push-subscription-changed, push-permission-changed | subscribed, permission, subscribe, unsubscribe |
useSwoffSync | background-sync-complete | supported, registered, triggerSync |
useSwoffPwa | beforeinstallprompt | isInstallable, promptInstall |
useSwoffPrefetch | — | prefetch (calls prefetchCache) |
useSwoffMutation | — | mutate (calls fetchWithCache) |
useSwoffMutationState | mutation-queue-changed | states, track, getState |
useSwoffReset | — | reset (calls resetSwoff) |
useSwoffAnalytics | swoff:offline-fallback (or SW message OFFLINE_FALLBACK_ACTIVATED) | lastEvent, events, clear |
useSwoffStorage | — | usage, quota, percentUsed |
Prerequisites
- Basic knowledge of your framework's reactive system (hooks, refs, stores, signals)
- Familiarity with
window.addEventListenerandCustomEvent - The generated
swoff/directory in your project
How to add a new framework
- Create a file for your framework (e.g.,
frameworks/solid.mdx) - Document the
swoff.config.jsonvalues specific to that framework - Link to this Blueprint for the adapter implementation
- Update
frameworks/meta.jsonto include your file