Offline-First in the CHT
Overview of Offline-First in the context of the CHT
CHT applications are designed to be used equally well in areas with no internet connectivity, slow or unreliable internet connectivity, and good internet connectivity. Achieving reliable performance and powerful features requires diligence and strict adherence to the principles of Offline-First development.
In this page we’ll cover why and how we achieve this in the CHT.
Why this is important
The CHT is designed to improve healthcare in the hardest to reach communities. While some users have a strong internet connection, to be as inclusive as possible we optimise for Care Teams with connections that are intermittent, unreliable, expensive, and low bandwidth. To achieve this the CHT is designed to be offline first, which, as the name suggests, means the application never relies on an internet connection for day to day tasks.
Caveat: a small set of use cases require a decent internet connection, but these are limited to:
- Logging in
- Initial download of the application and data
- Changing your own password
- Most administrative tasks such as creating new users
- Data analytics over large data sets
Requirements
Code
The CHT uses a Service Worker to store the code needed to start up and run which is best practice for caching web applications. The CHT Core Framework code is downloaded when the user visits the login page for the first time and completes silently in the background while the user logs in.
The code includes JavaScript, CSS, HTML, fonts, and images. It doesn’t contain any private information so it’s safe to download and cache it without authentication. It also doesn’t contain deployment specific configuration (covered in detail below).
The CHT Core Framework checks for updates to the code by attempting to connect to the server in the background. The user isn’t notified if this check fails or times out, so they can continue to complete workflows without interruption. When an update is found the new code is downloaded and the service worker cache is updated. The user is prompted to reload to start using the new version of the application, but this can be dismissed if they are busy with a patient or don’t want to lose their progress if they are midway through an action like creating a contact or completing a form. Either way, the update will be automatically applied the next time the application starts up.
Data
Once the application has started up it needs data to be useful. This data falls into two categories:
- Configuration specific to a single deployment such as forms, task definitions, places, and hierarchies.
- Patient data such as personal details, information about visits, reports, tasks, and messages.
Both of these types of data are cached on the device because they are required for the user to do their job. The CHT Core Framework uses PouchDB to store the data on the device’s disk. Once cached, when the application starts up the code is executed, which reads the data, allowing the user to do their job regardless of the quality of their internet connection.
If the user creates new patient data by registering a family or completing a task the application stores this in the phone’s cache and attempts to submit this to the server. Periodically the application checks to see if there is new data on the server that is relevant to the user and if so, it updates the cache. This process of sending and receiving data updates is called replication, and is performed without interrupting the user from their work.
FAQs from CHT contributors
Q: Should I rely on request failure handling?
Q: Should I rely on a request timeout?
Q: Should I rely on a spinner or loading bar?
Q: How can I check if the user is authenticated?
Q: Should I rely on APIs which report device connection status?
Q: Should I add a feature that requires a connection?
Further reading
- Designing Offline-First Web Apps by A List Apart
- A Design Guide for Building Offline First Apps by Hasura
- CouchDB takes CHT to the front lines of healthcare work by CouchDB