This section covers how to design a web application that keeps working when the network doesn't. The architecture decision is how much the application depends on the connection:
- What the application caches
- Where the application keeps its data on the device
- How the application sends offline changes back to the server
These choices shape the whole application and are expensive to change later. The section then covers how to ship that architecture, including installable web applications (PWAs) and when they're a better fit than native applications.
Online-first vs. offline-first
Most web applications are online-first: they assume a connection and typically break without one. An offline-first application treats the device as the first place to read and write data, and syncs with the server in the background.
Starbucks' ordering application is a great example of an offline-first application: customers can browse the menu and build an order without a connection, and it's sent when the connection returns. An online-first version would keep what they see current, with live stock, prices and promotions, but block purchases while the network is down. Because the application is offline-first, orders are local requests until the device reconnects, and the customer doesn't know whether the server will accept them, or at what price, until it checks stock, price and payment.
The business must decide which failure costs more: a lost sale, or an order that can't be fulfilled or is priced differently than the customer expected.
Caching strategies
The application decides, resource by resource, whether to go to the cache or the network first. Because a cache is a retained copy of a resource, this can lead to stale data that may no longer match the server. An offline visit can only show resources saved earlier, or an available fallback.
- Cache-first: serve from the cache and only go to the network if something is missing or expired. This suits files that rarely change, like logos, fonts and application code, when they have a safe update strategy. A build tool can put a hash of each file's contents in its name, so a changed file gets a new URL that the browser fetches.
Cache-first: a cached copy ends the request at the cache. The server is contacted only when nothing is cached or the copy has expired. This suits files with a safe update strategy. - Network-first: try the network and fall back to the cache if it fails. This is best for data that should be fresh but still needs to show something offline, like a news feed.
Network-first: the server is asked first. The cache answers only when the network request fails. - Stale-while-revalidate: show the cached version instantly, fetch a fresh copy in the background, and update the screen when it arrives, which may be the same visit or only the next load. This is best for content where speed matters more than being up to the second, like a product catalog. It costs a background request on each visit.
Stale-while-revalidate: the cached copy is shown immediately, while a background request fetches a fresh copy and stores it in the cache.
Data on the device
Offline-first applications keep a local copy of the data the user needs, stored in the browser, such as in IndexedDB. The key decision is scope: which data to keep locally, how much, and for how long. Too little and the application is useless offline. Too much and it's slow, heavy and a privacy risk on shared devices. Browser storage can be cleared or evicted, so a local save is not a permanent backup.
Browsers have several kinds of storage. Some are limited to strings, but stringified data can be used:
- Cookies: small values shared across tabs and sent to the server with every request, mostly used for sessions.
- localStorage: text kept across visits and shared across tabs. It holds only a small amount.
- sessionStorage: like localStorage, but each tab has its own copy, cleared when the tab closes.
- Cache storage: saved copies of network responses, the store the caching strategies above read and write.
- IndexedDB: a browser-provided database for structured data on the device. It supports offline work, but browser storage is not a permanent backup.
Syncing and conflicts
Supported offline changes go into a queue and are sent when the connection returns. The UI must distinguish saved locally, pending sync, confirmed by the server, and rejected or conflicting changes. A queued order is only an intent: price, stock and payment must be checked before the server confirms acceptance.
The hard part is conflicts. Conflicts occur when two people edit the same record while offline, or the server data changes in the meantime. Version numbers or timestamps on records let the server detect them before it overwrites a newer change. Common strategies:
- Last write wins: the most recent change overwrites the others. It's simple, but data can silently disappear.
- Merge: combine changes when they touch different fields. It keeps both edits, but even separate fields can have related business constraints that the combined result breaks.
- Ask the user: show both versions and let them choose. Nothing is lost, but the change waits on a person to resolve it.
- Server decides: business rules on the server pick the winner. The result follows the rules, but those rules can be complex and their results hard to predict.
Keeping data consistent
Eventual consistency means copies may disagree temporarily, then converge once updates reach the server and conflicts are resolved. Retrying an arbitrary payment request is not automatically safe. Situations like this resolve smoothly when operations are idempotent , so repeating one has no additional effect, or when the server clearly fails an operation whose outcome already exists.
For example, the client can retain one submission ID for a queued order and reuse it on retries. The server must recognize that ID and return the existing outcome instead of creating another order, or clearly fail when an outcome already exists. Generating a new ID on every retry defeats that protection; a client-side ID alone does not make the operation safe.
Trade-offs
| Approach | What you gain | What it costs | Best fit |
|---|---|---|---|
| Online-only | Less local-sync complexity; requests can check current server state | Unusable without a connection; slow on poor networks; network failures still need handling; responses can age | Operations that require a live authoritative decision |
| Cached for speed (offline read) | Fast loads; content still viewable offline | Users can't make changes offline; cached content can be stale | News, catalogs, documentation, menus |
| Full offline-first (read and write, with sync) | Works anywhere; local feedback is immediate; no lost work | Most complex to build and test; sync and conflict risks; more data stored on devices; server acceptance remains separate | Ordering, field forms, travel notes and other work that can remain pending |
Shipping the application
Installable web applications (PWAs)
A progressive web application (PWA) is a website that can be installed from the browser, without an app store. Installation is what a PWA adds to an ordinary website. A web app manifest supplies installation information. Offline behavior, push notifications and synchronization are separate capabilities. Each depends on browser support, and some also depend on the PWA being installed, so none is a package of guaranteed features.
A service worker is an event-driven script the browser runs outside the page. It intercepts requests for caching and supported background events, but it doesn't render the UI or run continuously. Neither installation nor a service worker alone guarantees offline editing or delivery of queued changes.
Well-known examples include Starbucks, Pinterest, Uber's lightweight m.uber, and installable desktop web applications like Spotify and Outlook. Some of these case studies are a few years old, so check each is still current before using it.
Platform reality
Support varies by browser and operating system. Chrome on Android can show an install prompt. iOS has no equivalent, so users install through the share sheet. iOS supports web push for installed Home Screen web applications, but Safari doesn't support background sync, so queued changes wait until the application is open again.
Verify the capabilities needed on every target device. The least capable device your audience uses sets the limit of what the application can rely on. Check the latest status right before the session, since it changes often.
PWA vs. native: the product decision
Once the offline architecture is clear, the remaining question is how to ship it:
- A PWA can share web code across compatible browsers, and direct web distribution avoids app-store review delays, but device access is limited, especially on iOS.
- A native application offers broader device integration and can be distributed through app stores, but supporting multiple platforms adds development, testing, release and cost; separate codebases are possible, not mandatory.
- A hybrid wrapper like Capacitor packages a web codebase in a native shell. It can be an alternative to a PWA when you need app-store distribution or native device access, but it adds platform integration and store-release work that a PWA avoids.
This is mostly a budget, skills and time-to-market decision, which is worth settling early with the client.
What this means for delivery
How offline an application needs to be is a scoping question to settle early. Full offline-first can significantly increase build and testing effort, while caching selected reads is cheap and benefits almost every application. Both still need freshness, privacy and failure handling. The PWA vs. native choice then drives budget, team skills and time to market, without removing synchronization obligations.
Check your understanding
Quiz
What should an offline order flow do? Select two.
A commuter builds an order without a connection. The application must preserve the draft, but stock, payment and order acceptance require a server response. Which two behaviors respect both requirements?
Review questions and explanations (text alternative)
-
What should an offline order flow do? Select two.
A commuter builds an order without a connection. The application must preserve the draft, but stock, payment and order acceptance require a server response. Which two behaviors respect both requirements?
- Save the draft locally and show it as pending until the server confirms the submission.
- Show payment and order confirmation as soon as the draft has been written to device storage.
- Retry the same logical submission with an identifier the server uses to avoid duplicate orders or charges.
- Create a new independent order on each retry if the previous response did not arrive.
Suggested answer: Save the draft locally and show it as pending until the server confirms the submission.; Retry the same logical submission with an identifier the server uses to avoid duplicate orders or charges.
Saving locally protects the user's work but does not confirm stock or payment acceptance. Show the order as pending and retry the same identified submission with server-side duplicate protection. A new order on every retry risks duplicate purchases.
-
How should the UI handle competing offline edits?
Two reviewers edit the same contract paragraph offline. Neither version may be silently discarded, and the product has no rule for choosing a winner. What should happen when synchronization detects the conflict?
- Keep the edit with the latest timestamp and remove the other version.
- Concatenate both paragraphs and mark the contract as resolved.
- Accept whichever edit reaches the server first and discard the later upload.
- Keep both versions and have a reviewer resolve the conflict first.
Suggested answer: Keep both versions and have a reviewer resolve the conflict first.
Human resolution preserves both contributions when overlapping contract edits have no safe automatic winner. Timestamp or arrival-order rules discard work, while concatenation can change the meaning without resolving the conflict.
-
How should the application present a cached travel schedule offline?
A traveler opens yesterday's saved schedule without a connection. Departures can change, and the application cannot contact the provider to check for updates. What should the screen do?
- Hide all departures until a live request can succeed, even though a saved copy is available.
- Present the departures as confirmed because no update has contradicted the saved copy.
- Show the saved copy with its retrieval time and a warning that the departures may have changed.
- Reset the last-updated time whenever the device opens its cache, so the copy appears recently checked.
Suggested answer: Show the saved copy with its retrieval time and a warning that the departures may have changed.
Hiding the saved schedule loses the offline benefit, while assuming it is current promises unverified departures. Opening local storage does not check the provider, so it must not reset the last-checked time.