Full Stack Engineer Guide: Offline-First PWAs with IndexedDB Outbox & Service Worker Sync

Field-service technicians, warehouse scanners, clinical home-visit nurses, and commuters on spotty cellular networks cannot afford to lose 20 minutes of form input when an HTTP `POST` fails inside an elevator or rural dead zone. Building a true Offline-First Progressive Web App (PWA) requires more than caching static CSS/JS files in a Service Worker: it requires a transactional IndexedDB mutation outbox, optimistic UI state updates, and idempotent background synchronization when connectivity returns. As a Senior Full Stack Engineer, here is how I architect resilient offline-first web applications.

1. Persistent IndexedDB Mutation Outbox with Idempotency Keys

Instead of firing raw `fetch()` calls that reject when `navigator.onLine === false`, mutations write an immutable command envelope (complete with a client-generated UUIDv4 idempotency key and vector timestamp) to an IndexedDB `outbox` object store and flush in FIFO order upon network recovery:

export interface OutboxMutation {
  idempotencyKey: string;
  endpoint: string;
  method: "POST" | "PUT" | "PATCH";
  payload: Record;
  createdAtMs: number;
}

export async function flushOfflineOutbox(
  getPendingMutations: () => Promise,
  markCompleted: (key: string) => Promise
): Promise {
  if (typeof navigator !== "undefined" && !navigator.onLine) {
    return 0;
  }
  const pending = await getPendingMutations();
  let syncedCount = 0;

  for (const item of pending) {
    const response = await fetch(item.endpoint, {
      method: item.method,
      headers: {
        "Content-Type": "application/json",
        "Idempotency-Key": item.idempotencyKey,
      },
      body: JSON.stringify(item.payload),
    });

    if (response.ok || response.status === 409) {
      await markCompleted(item.idempotencyKey);
      syncedCount += 1;
    } else if (response.status >= 500) {
      // Stop FIFO flush on transient server error to preserve ordering
      break;
    }
  }
  return syncedCount;
}

2. Accurate Draft Status & Conflict Resolution

To maintain user trust, "Draft saved locally" badges only appear after the IndexedDB transaction commits in the active session—never on initial hydration—and transition to "Synced to cloud" once the backend acknowledges the `Idempotency-Key`.

3. Hire a Senior Full Stack Engineer for Offline-First Web Apps

Building a field-operations PWA, logistics scanner, or local-first web application? Explore my Full Stack Engineer Pillar Guide, review my CRDT & Local-First Sync Guide, or book a 1-Day Offline-First PWA Sprint.


About the Author — Hire Aldian Fazrihady (Cloud Engineer, GCP Professional Cloud Architect, GCP Professional Data Engineer, Backend, AI & Full Stack Engineer)

Aldian Fazrihady is a Top-Rated Senior Cloud Engineer, GCP Professional Cloud Architect (Google Cloud Certified), GCP Professional Data Engineer (Google Cloud Certified), Backend Engineer (Python, Django, FastAPI, Go, PostgreSQL), AI Application Engineer (LLMs, RAG, MCP, Google ADK, LangGraph), and Full Stack Engineer (React, Next.js, TypeScript) with 15+ years of production software engineering experience.

Hire Aldian Fazrihady on Upwork | Book 1-Day Programming Service ($100 USD / Rp 1.000.000) | View Live AI Portfolio | Send a Direct Message

Related Engineering Articles

Engineering Role Pillar Guides & Category Hubs