dist/plugins/web-pwa-offline-first/skills/web-pwa-offline-first/SKILL.md
Local-first architecture with sync queues
npx skillsauth add agents-inc/skills web-pwa-offline-firstInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Quick Guide: Build applications that work primarily with local data, treating network connectivity as an enhancement. Use IndexedDB (via Dexie.js 4.x or idb 8.x) as the single source of truth. Implement sync queues for reliable background synchronization. Use optimistic UI patterns for instant feedback. Note: Background Sync API is experimental with limited browser support (Chrome/Edge only).
<critical_requirements>
All code must follow project conventions in CLAUDE.md (kebab-case, named exports, import ordering,
import type, named constants)
(You MUST use IndexedDB (via wrapper library) as the single source of truth for all offline data)
(You MUST implement sync metadata (_syncStatus, _lastModified, _localVersion) on ALL entities that need synchronization)
(You MUST queue mutations during offline and process them when connectivity returns)
(You MUST use soft deletes (tombstones) for deletions to enable proper sync across devices)
(You MUST implement exponential backoff with jitter for ALL sync retry logic)
(You MUST NOT await non-IndexedDB operations mid-transaction - transactions auto-close when control returns to event loop)
</critical_requirements>
Auto-detection: offline-first, IndexedDB, Dexie, idb, sync queue, local-first, offline storage, background sync, optimistic UI offline, conflict resolution, CRDT, last-write-wins
When to use:
When NOT to use:
Storage Considerations:
Detailed Resources:
Offline-first is a design philosophy where applications are built to work primarily with local data, treating network connectivity as an enhancement rather than a requirement.
Core Principles:
Local is the Source of Truth: The local database is always authoritative. All reads and writes go through local storage first. Server sync happens in the background.
Immediate Responsiveness: Users never wait for network operations. Changes are applied locally instantly, synced later.
Graceful Degradation: Apps work fully offline, enhance when online, and handle transitions seamlessly.
Sync Transparency: Users understand their data's sync state through clear UI indicators without technical jargon.
The Offline-First Data Flow:
User Action
|
Local Database (IndexedDB) <-- Single Source of Truth
|
UI Updates Immediately (Optimistic)
|
Sync Queue (Background)
|
Server (When Online)
|
Conflict Resolution (If Needed)
|
Local Database Updated
</philosophy>
Every entity that needs synchronization must include metadata for tracking sync state. This is the foundational pattern - all other patterns depend on it.
interface SyncableEntity {
id: string;
_syncStatus: "synced" | "pending" | "conflicted";
_lastModified: number;
_serverTimestamp?: number;
_localVersion: string;
_serverVersion?: string;
_deletedAt?: number; // Soft delete tombstone
}
Why this matters: Without sync metadata, you cannot track what needs syncing, detect conflicts, or implement soft deletes. See examples/core.md Pattern 1 for full implementation with factory functions.
Use a repository as the single access point for all data operations, encapsulating local storage and sync queue logic. All reads come from local DB, all writes save locally first then queue for sync.
interface DataRepository<T extends SyncableEntity> {
get(id: string): Promise<T | null>;
getAll(): Promise<T[]>;
save(item: T): Promise<void>; // Local write + queue sync
delete(id: string): Promise<void>; // Soft delete + queue sync
getPendingCount(): Promise<number>;
}
Why this matters: Encapsulates the local-first write pattern (save locally, queue for sync) so consumers don't need to manage both operations. See examples/core.md Pattern 2 for full implementation.
Queue operations when offline, process reliably with exponential backoff when connectivity returns.
const MAX_RETRY_ATTEMPTS = 5;
const INITIAL_BACKOFF_MS = 1000;
const MAX_BACKOFF_MS = 30000;
function calculateBackoff(attempt: number): number {
const exponentialDelay = Math.min(
INITIAL_BACKOFF_MS * Math.pow(2, attempt),
MAX_BACKOFF_MS,
);
const jitter = exponentialDelay * 0.5 * (Math.random() * 2 - 1);
return Math.floor(exponentialDelay + jitter);
}
Why this matters: Without retry logic, transient network failures cause permanent data loss. Jitter prevents thundering herd when many clients reconnect simultaneously. See examples/core.md Pattern 3 for full queue implementation.
Don't rely solely on navigator.onLine (returns true behind captive portals, dead WiFi). Verify with actual health check requests.
async function checkConnectivity(): Promise<boolean> {
if (!navigator.onLine) return false;
try {
const response = await fetch("/api/health", {
method: "HEAD",
cache: "no-store",
});
return response.ok;
} catch {
return false;
}
}
Why this matters: navigator.onLine only checks for a network interface, not actual internet connectivity. See examples/core.md Pattern 4 for full status manager with slow connection detection.
Update UI immediately, store previous value for rollback if sync fails. Return a rollback function from each optimistic update.
See examples/core.md Pattern 5 for full implementation with rollback support.
Fetch from network when online, fall back to cache when offline. Return source metadata ("network" | "cache") so UI can indicate data freshness.
See examples/core.md Pattern 6 for full implementation with timeout and cache fallback.
Three strategies ordered by complexity:
Last-Write-Wins (LWW): Simplest. Most recent timestamp wins. Good for independent values. See examples/sync.md Pattern 18.
Field-Level Merge: Only conflicts where both sides changed the same field. Preserves non-conflicting changes from both sides. See examples/sync.md Pattern 19.
Version Vectors: Detect true concurrent modifications without clock synchronization. Use when timestamp-based approaches fail due to clock drift. See examples/sync.md Pattern 21.
For collaborative text editing, use a CRDT library (separate concern from this skill).
</patterns><red_flags>
High Priority:
navigator.onLine alone - verify with actual network requestfetch() or setTimeout() inside an IndexedDB transaction - transaction auto-closesMedium Priority:
Gotchas & Edge Cases:
navigator.storage.persist() and encourage home screen installnavigator.storage.estimate() requires HTTPS; returns { usage: 0, quota: 0 } in unsecured contextsonline event listener as fallback.where("[userId+completed]").equals([userId, 1]) (1 = true)useLiveQuery returns undefined while loading, not null - check with === undefinedBroadcastChannel for coordination (see examples/indexeddb.md Pattern 16)</red_flags>
<critical_reminders>
All code must follow project conventions in CLAUDE.md
(You MUST use IndexedDB (via wrapper library) as the single source of truth for all offline data)
(You MUST implement sync metadata (_syncStatus, _lastModified, _localVersion) on ALL entities that need synchronization)
(You MUST queue mutations during offline and process them when connectivity returns)
(You MUST use soft deletes (tombstones) for deletions to enable proper sync across devices)
(You MUST implement exponential backoff with jitter for ALL sync retry logic)
(You MUST NOT await non-IndexedDB operations mid-transaction - transactions auto-close when control returns to event loop)
Failure to follow these rules will result in data loss, sync conflicts, and poor offline user experience.
</critical_reminders>
development
Material Design component library for Vue 3
development
VitePress 1.x — Vue-powered static site generator for documentation sites, built on Vite
tools
Docusaurus 3.x documentation framework — site configuration, docs/blog plugins, sidebars, versioning, MDX, swizzling, and deployment
development
TanStack Form patterns - useForm, form.Field, validators, arrays, linked fields, createFormHook, type safety