Offline Photo Evidence Sync in React Native

Resumen

Pixel shares a DEV Community article on building an offline outbox for photo evidence in a React Native insurance inspection app. It explains how roof photos are resized, hashed, and queued as separate media and evidence operations for later, repeat-safe syncing—crucial when storm damage knocks out cell service.

DEV Community

An offline outbox for photo evidence in React Native, and the bugs it taught me

An insurance claim for storm damage lives or dies on its photos. The adjuster wants the overview, every slope, the ridge and hips, the damage close up, the gutters and a measurement or two. If one required shot is missing, someone has to go back up the ladder, and after a storm the roof may already be tarped or repaired. The people taking those photos are on roofs in rural areas, often right after the weather that took out the cell towers. ClaimLayer's field app is built around one rule: the phone never needs signal to do its job. Below: how that works, what the server does with the queue, and the bugs that showed up once I got strict about it. Why "upload when you press the shutter" fails The obvious version uploads each photo as it is taken. With no signal the request fails, and depending on how the code is written the photo is lost or the user stares at a spinner. Every fix for that leads to the same place: write locally first, send later, and make sending safe to repeat. Capture first, sync later. Capture writes two operations When the inspector takes a photo, the app resizes it, hashes it and queues two operations: one for the file and one for the evidence record it belongs to. apps/field/src/InspectionScreen.tsx const photo = await camera.current.takePictureAsync({ quality: 0.85, exif: true })
const resize = photo.width >= photo.height ? { width: 1600 } : { height: 1600 }
const resized = await ImageManipulator.manipulateAsync(photo.uri, [{ resize }],
{ compress: 0.82, format: ImageManipulator.SaveFormat.JPEG })
const contents = await FileSystem.readAsStringAsync(resized.uri, { encoding: FileSystem.EncodingType.Base64 })
const digest = await Crypto.digestStringAsync(Crypto.CryptoDigestAlgorithm.SHA256, contents)

const base = ${Date.now()}-${digest.slice(0, 8)}
await enqueue(storage, { id: media-${base}, claimId, operation: 'media.upload',
idempotencyKey: media-${base}-offline, occurredAt: capturedAt, payload: metadata })
await enqueue(storage, { id: evidence-${base}, claimId, operation: 'evidence.save',
idempotencyKey: evidence-${base}-offline, occurredAt: capturedAt,
payload: { inspectionId, itemKey: ITEMS[activeItem], sha256: digest, caption: caption.trim() } })
One photo becomes two queued operations. A few details are worth copying: Resize on the device. 1600 px on the long edge at JPEG 0.82 is plenty for an adjuster and keeps a day of photos small. Store the file path, not the bytes. The queue holds the URI and metadata, so the queue itself stays small. Record where and when. Capture time, device time, EXIF, GPS and accuracy (when permitted) and a SHA-256 travel with every photo. Every operation gets its own idempotency key, built from time and content hash, minted on the device before any network call. One capture at a time. A busy flag stops two captures racing on the same queue key. The screen tells the inspector exactly that: "Capture locally first. The original remains on this device until the server confirms upload." Sync: the status code decides The outbox envelope is defined once in a shared contracts package, so the app and the server agree on its shape: packages/contracts/src/index.ts type OutboxOperation = {
id: string
claimId: string
operation: 'media.upload' | 'evidence.save'
idempotencyKey: string // 16 to 128 chars
occurredAt: string
payload: Record
state: 'pending' | 'in_flight' | 'synced' | 'refused' | 'abandoned'
}
Sync walks the queue one item at a time, in capture order, and the response decides what happens to each item. apps/field/src/api.ts try {
const response = await fetcher(${baseUrl}/api/trpc/inspections.saveEvidence, {
method: 'POST', headers: headers(token, deviceId),
body: JSON.stringify({ claimId: op.claimId, inspectionId: p.inspectionId, itemKey: p.itemKey,
photos: 1, note: p.caption || undefined, idempotencyKey: op.idempotencyKey }),
})
next.push({ ...op, state: response.ok ? 'synced'
: response.status...
Offline Photo Evidence Sync in React Native
Powered by Readybot.io

Lo último de Pixel

Incident Response: Look Beyond the Red Alert

Pixel’s DEV Community announcement explores how teams can fixate on one failing service while overlooking the real cause of an outage, such as a full …

Sach: AI Fact-Checks WhatsApp Forwards in India

Pixel’s announcement introduces Sach, a tool that checks forwarded messages in English, Hindi, Telugu, and 14 other Indian languages against official sources. It gives cited …

Java 25 Removes SecurityManager: What to Fix

Pixel’s announcement explains how Java 24’s JEP 486 permanently disabled the SecurityManager, breaking test workarounds that intercept System.exit. It recommends returning exit codes or throwing …