Full Stack Engineer: Building Offline-First Progressive Web Apps (PWAs) with Next.js 15, Service Workers & IndexedDB Sync

Field engineering teams, hospital bedside tablets, and warehouse logistics scanners frequently operate in dead zones where cellular and Wi-Fi connections drop mid-form. A standard web application fails with a browser network error and loses unsaved user work. As a Full Stack Engineer, I architect Offline-First Progressive Web Apps (PWAs) in Next.js 15 and React 19 using Workbox/Serwist Service Workers, local IndexedDB mutation outboxes, and Background Sync API reconciliation.

1. Local-First IndexedDB Mutation Outbox & Honest Draft Persistence

Rather than displaying a fake "Draft saved" badge on component mount before any user edit occurs, our offline-first state manager only records a local timestamp after an explicit user mutation is durably committed to an IndexedDB object store (`pending_mutations`), queuing an idempotent sync envelope for background replay:

export interface QueuedMutation {
  mutationId: string;
  endpoint: string;
  payload: Record<string, unknown>;
  clientUpdatedAtIso: string;
}

export async function enqueueOfflineMutation(
  db: IDBDatabase,
  mutation: QueuedMutation
): Promise<void> {
  return new Promise((resolve, reject) => {
    const tx = db.transaction(["pending_mutations"], "readwrite");
    const store = tx.objectStore("pending_mutations");
    store.put(mutation);
    tx.oncomplete = () => resolve();
    tx.onerror = () => reject(tx.error);
  });
}

export async function flushOfflineMutations(db: IDBDatabase): Promise<number> {
  const tx = db.transaction(["pending_mutations"], "readonly");
  const store = tx.objectStore("pending_mutations");
  const all: QueuedMutation[] = await new Promise((res, rej) => {
    const req = store.getAll();
    req.onsuccess = () => res(req.result as QueuedMutation[]);
    req.onerror = () => rej(req.error);
  });

  let synced = 0;
  for (const item of all) {
    const resp = await fetch(item.endpoint, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        "Idempotency-Key": item.mutationId,
      },
      body: JSON.stringify(item.payload),
    });
    if (resp.ok || resp.status === 409) {
      const delTx = db.transaction(["pending_mutations"], "readwrite");
      delTx.objectStore("pending_mutations").delete(item.mutationId);
      synced += 1;
    }
  }
  return synced;
}

2. Service Worker Stale-While-Revalidate Caching & Conflict Resolution

Static Next.js `_next/static/` bundles are precached with immutable content hashes, while API reads use a `NetworkFirst` strategy falling back to IndexedDB snapshots when offline. On reconnect, the Service Worker `sync` event replays queued mutations against our idempotent PostgreSQL backend and Next.js 15 & Django Ninja API.

Need a Full Stack Engineer to build an offline-first PWA in Next.js and React? Book a 1-Day Engineering 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