Rural India has over 350 million internet users, but connectivity is intermittent at best. 2G and 3G networks dominate, signal drops are frequent, and data is expensive relative to income. For applications serving agriculture, healthcare, education, and government services in these areas, the traditional online-first architecture simply does not work. A form that requires a stable connection to submit means lost data when the connection drops mid-entry. We have developed an offline-first architecture that treats the network as an enhancement, not a requirement.
The foundation is a service worker that caches the entire application shell — HTML, CSS, JavaScript, and critical assets — on first visit. Subsequent visits load entirely from the cache, with the service worker checking for updates in the background. For data, we use IndexedDB as a local database, structured with Dexie.js for a friendlier API. Every user action — filling a form, updating a record, uploading a photo — writes to IndexedDB first and queues a sync operation. The UI responds instantly because it reads from the local database, not the server.
The sync engine is the heart of the system. When connectivity is detected (we use the navigator.onLine API plus periodic fetch pings to our health endpoint for accuracy), the engine processes the outbound queue in order, sending each pending operation to the server. If the server accepts the operation, it is removed from the queue. If there is a conflict — another user modified the same record while this user was offline — the engine applies a last-write-wins strategy for simple fields and presents a conflict resolution UI for complex cases like form submissions that cannot be automatically merged.
The most challenging aspect is handling file uploads offline. Photos captured from a phone camera can be 3 to 5 MB each, and a field worker might capture 20 photos before reaching a connectivity zone. We compress images client-side using the Canvas API, targeting 200KB per image, store them in IndexedDB as blobs, and upload them via a resumable upload protocol when connectivity returns. If the connection drops mid-upload, the protocol resumes from where it left off rather than restarting. For one agricultural extension project, this system allowed field workers to survey 50 farms per day with zero data loss despite operating in areas with less than 30% network availability.