IndexedDB can store structured local records without blocking the main thread like a large synchronous localStorage write. A draft queue needs a stable local ID, case ID, body, creation instant, and status such as pending or conflict. Saving locally means only that the device holds the draft; it does not mean the server accepted it. The application must present that distinction clearly, protect sensitive data according to its product policy, and tolerate storage denial or eviction. A transaction groups local writes inside one store, but it cannot make an offline server write atomic.
IndexedDB for Local Drafts
Working case
An inspector types a note for case 47 while offline. The page saves it under local ID 'draft-47-83' and shows 'Saved on this device; not submitted'. When the network returns, a sync process reads pending drafts and submits them with idempotency keys. If the server reports that the case has changed, the draft moves to conflict instead of disappearing. If local storage is unavailable, the page warns before navigation. The snippet opens a versioned database and writes one draft, but the full product still needs retries, conflict display, and deletion after confirmation.
Implementation
const opening = indexedDB.open("inspection-drafts", 2);
opening.onupgradeneeded = () => {
const database = opening.result;
if (!database.objectStoreNames.contains("drafts")) {
database.createObjectStore("drafts", { keyPath: "draftId" });
}
};
opening.onsuccess = () => {
const database = opening.result;
const transaction = database.transaction("drafts", "readwrite");
transaction.objectStore("drafts").put({
draftId: "draft-47-83", caseId: 47, note: "seal checked", status: "pending"
});
};Cost and boundaries
IndexedDB writes are asynchronous, and indexed key lookup is usually much cheaper than scanning a large collection, but latency depends on device storage and transaction size. Keeping D drafts consumes O(D plus body bytes) space until cleanup. Large images should not be copied into every draft record; use a separate bounded storage policy. Schema upgrades can block when old tabs hold open connections, so handle versionchange and test upgrades. Device-local drafts require explicit retention and privacy decisions.
Common Mistakes
- Do not call a local draft a completed submission.
- Do not delete a draft before the server confirms its write.
- Do not assume device storage is permanent or always available.
Connected lessons
Offline and Device Capabilities; Service Worker Offline Fallback; Web App Manifest and Optional Installation; Offline Sync and Conflict Policy; Browser storage: save convenience data without storing credentials; Fetch requests: separate HTTP failure, transport failure, and cancellation; HTTP caching: validate a changed representation with an ETag.
Failure trace
A reviewer writes a long note offline, closes the tab, and reopens it. The UI said 'saved' when it only updated an in-memory array; the note is gone. Persist a draft record and its sync state in a completed IndexedDB transaction before claiming local save. A queue item should include a stable client ID and the base server version so reconnect logic can detect duplicates and conflicts.
Verification
- Write a draft, close the tab, reopen it offline, and confirm the exact text returns.
- Simulate a rejected storage transaction and ensure the interface does not announce a successful save.
- Replay the same queued item twice and verify the server does not create two notes.
Decision note
Local storage can be evicted or unavailable, so a local save is weaker than a server commit. Make that distinction visible and offer a way to export important unsynced work where the product requires it.
Apply and check
Build Project: offline inspection draft and photo intake; then check the boundary with Web Development: offline and delivery contracts quiz.
Further connections
Autosave, Draft Recovery, and Conflicts.
Further connections
IndexedDB Version Changes and Blocked Tabs; Browser Storage Quota, Eviction, and Recovery.
