Course contentsShow
← Web Development
Lesson 2337 of 2,98242. Progressive Web AppsFree lesson

Storing Failed Requests with IndexedDB

Persisting failed network requests to IndexedDB so they can be retried during background sync.

Storing Failed Requests with IndexedDB

What you'll learn: How to persist network requests that fail when offline so they can be automatically retried through background sync.

When a user tries to submit a form, post a comment, or send data while offline, you don't want that action lost forever. This is where IndexedDB comes in—a powerful client-side database that lets you store structured data, including details about failed network requests.

The Core Pattern

Think of IndexedDB as a "holding area" for failed requests. When your app detects a network failure (caught in a catch block or through the offline state), you store the request details—URL, method, headers, body data—into IndexedDB. Later, when the sync event fires in your service worker, you retrieve these stored requests, attempt to send them again, and remove them from storage on success.

Why IndexedDB?

Unlike localStorage (limited to strings and ~5MB), IndexedDB can handle complex objects and larger datasets. You can store the entire request payload, timestamps, retry counts, and custom metadata—all critical for robust offline handling.

Typical Flow

  1. Network request fails → catch the error
  2. Store request details → write to IndexedDB (e.g., a "pending-requests" object store)
  3. Register background sync → trigger sync event
  4. Service worker wakes up → reads from IndexedDB, retries each request
  5. On success → delete the stored request from IndexedDB

This ensures user actions survive temporary connectivity loss and process automatically when the connection returns.

Key Takeaway: IndexedDB acts as a persistent queue for failed requests, enabling your PWA to reliably retry user actions through background sync without data loss.