Offline-First
WELD is built for the real world, where networks are unreliable. It handles offline scenarios differently depending on the type of request.
GET requests — Cache fallback
Section titled “GET requests — Cache fallback”When you make a GET request, WELD automatically caches the successful response in IndexedDB. If the network goes down and the same request is made again, WELD serves the cached data transparently.
const { promise } = api.get('v1/products', ProductArraySchema, { offlineFallback: true, // default for GET requests})
// Online: fetches from server, caches the response// Offline: returns the cached data from IndexedDBconst products = await promiseThe cache has a default TTL of 5 minutes. You can configure this globally or per-request in a future version.
Mutation queue — POST, PUT, PATCH, DELETE
Section titled “Mutation queue — POST, PUT, PATCH, DELETE”When a mutation fails because the device is offline, WELD doesn’t throw an error and give up. Instead, it enqueues the mutation in IndexedDB and replays it automatically when the network is restored.
// User is offline — this doesn't failconst { promise } = api.post('v1/orders', Schema, { body: { productId: '123', quantity: 2 },})
// The mutation is stored in the queue// When network comes back, it fires automaticallyHow queue sync works
Section titled “How queue sync works”WELD listens to the browser’s navigator.onLine event. When it fires, the queue processor:
- Reads all pending mutations from IndexedDB in FIFO order (first in, first out)
- Replays each mutation against the server
- On success — removes it from the queue
- On failure — increments the attempt counter
- After 5 failed attempts — marks it as a dead letter (kept for inspection, doesn’t block others)
Manual sync
Section titled “Manual sync”You can trigger a sync manually if needed:
import { syncQueue } from 'weld-http'
// Force replay all queued mutations nowawait syncQueue()Disabling offline support
Section titled “Disabling offline support”// Disable for a specific requestconst { promise } = api.get('v1/products', Schema, { offlineFallback: false,})
// Disable globallyconst api = new Weld<AppRouter>('https://api.example.com', { offline: false,})Browser support
Section titled “Browser support”Offline-First requires IndexedDB and navigator.onLine, which are available in all modern browsers. In Node.js, Bun, and Deno, offline mode is automatically disabled since these environments don’t have IndexedDB.
