What you'll learn: By the end of this lesson you will be able to design the visible side of an offline-capable front end: show a connection hint without trusting it, label cached data with its age, give every change a status, keep drafts from vanishing, provide a fallback for pages that were never cached, and announce all of it without stealing focus.
Think of a departures board at a station. When it loses its feed it shows the last update time instead of "all trains fine". An offline-capable app is that board: the sync engineering lives elsewhere, and this lesson is about what the screen says.
The running example is the order tracking and reorder page of the grocery marketplace front end used in this course, on a phone that loses signal in lifts and car parks.
The core idea
- Tell the truth about three things: whether the connection seems to be there, how old the data on screen is, and what happened to the user's last action.
- Offline is a state of the data, not only of the network. A bad connection and no connection look the same to the user: slow, then wrong.
- Never make the user redo work. An action taken offline is accepted, kept on the device and shown as waiting; a draft survives a reload.
Storage and merging are in Offline-First Apps: Sync and Conflict Resolution, the queue in Offline Queues and Background Sync, and loading and failed states in Error, Loading and Empty States.
The patterns
| Pattern | What the user sees | What it relies on |
|---|---|---|
| Connection hint | "You may be offline", never a lock | online and offline events, failed requests |
| Data age | "Data is 14 min old" | A saved-at time stored with the cached response |
| Change status | Saved here, waiting, synced, needs attention | An outbox with a status per entry |
| Blocked action | The button says why it cannot work offline | A rule list: payment and stock need the server |
| Draft keeping | Typed text survives a reload | Local storage written on input |
| Offline fallback | A friendly page, not the browser's error | A service worker fallback response |
The connection hint is a hint
navigator.onLine returns true for online and false for offline, and MDN calls it "inherently unreliable": a connection to a local network counts as online even if that network has no internet access, and on Windows the status depends on reaching a Microsoft server, which a firewall or VPN may block. MDN's advice is to "not disable features based on the online status, only provide hints when the user may seem offline". When the value changes the browser fires online or offline on window.
So the hint can change a banner, but it must not decide whether an action is attempted: try the request, and let a failure move the app into its offline behaviour.
Figure 1. What the screen reads
How to read this: three independent sources feed one view. The connection hint is one input among three, and the least reliable.
Illustration 1: the minimal example
The banner text is a pure function of those facts. Run in Node:
function banner({ hint, queued, failed, ageMin }) {
const out = [];
if (hint === "offline") out.push("You may be offline. Changes are saved on this device.");
if (queued) out.push(`${queued} waiting to sync.`);
if (failed) out.push(`${failed} need attention.`);
if (ageMin !== null) out.push(`Data is ${ageMin} min old.`);
return out.join(" ") || "(no banner)";
}
const cases = [
{ hint: "online", queued: 0, failed: 0, ageMin: null },
{ hint: "offline", queued: 1, failed: 0, ageMin: 14 },
{ hint: "online", queued: 3, failed: 0, ageMin: null },
{ hint: "online", queued: 0, failed: 2, ageMin: null },
];
for (const c of cases) console.log(JSON.stringify(c).padEnd(55), "->", banner(c));
Output, from running it in Node 26.13.1:
{"hint":"online","queued":0,"failed":0,"ageMin":null} -> (no banner)
{"hint":"offline","queued":1,"failed":0,"ageMin":14} -> You may be offline. Changes are saved on this device. 1 waiting to sync. Data is 14 min old.
{"hint":"online","queued":3,"failed":0,"ageMin":null} -> 3 waiting to sync.
{"hint":"online","queued":0,"failed":2,"ageMin":null} -> 2 need attention.
Data age and status per change
The offline cookbook calls "cache then network" the idea "to show the cached data first, then update the page when and if the network data arrives". The interface's share is the age label, so the user can judge the numbers; order status and stock are where stale data hurts.
For changes the user made, each entry needs a status shown next to the item, not only in a banner.
Figure 2. The life of one change, as the user sees it
How to read this: "needs attention" must never be silent, because the person may have left the page. A change is not "synced" until the server says so.
Actions that cannot wait
Taking a payment or checking stock needs the server now. Offline, the honest options are to block the action and say why while keeping the user's input, or to accept an intent that will be confirmed later, with wording that says so. A button that silently does nothing is the worst option. The Background Synchronization API can run deferred work once the connection is stable, but MDN marks it as not Baseline because it does not work in some widely used browsers, so the interface cannot depend on it.
Illustration 2: the realistic example
A shopper in a lift taps "Reorder" for last week's basket. The cached order shows with its age label. The reorder is an intent that costs nothing to hold, so it joins the outbox as "saved on this device" and the banner counts one waiting change. Payment is not part of it: the confirmation says the order will be placed when the connection returns and the shopper confirms.
Drafts, storage and the fallback page
Text a user is typing, such as a delivery note, should be written to local storage as they type. But storage is not guaranteed. MDN says best-effort data lasts only while the origin is below its quota, the device has enough space and the user does not delete it, and eviction takes all of an origin's data at once. navigator.storage.persist() resolves true only if granted, and MDN says the browser "may or may not honor the request". Safari can also delete script-written data for an origin with no user interaction in seven days of browser use. Treat unsent work as the valuable data, and tell the user if a queued change is ever lost (IndexedDB and Storage Quotas).
A page never visited is in no cache. With a service worker, the cookbook's "generic fallback" applies: "If you fail to serve something from the cache or network, provide a generic fallback". The fallback says what is available offline and links to it (Service Worker Caching Strategies).
Figure 3. Choosing the offline behaviour of a screen
cached?"} S -->|"no"| F["Fallback page"] S -->|"yes"| A{"What does the
user do here?"} A -->|"read"| R["Cached data
+ age label"] A -->|"change, can wait"| Q["Queue it
+ status"] A -->|"change, needs server"| B["Block, say why,
keep input"]
How to read this: the choice is per screen and per action. One page can read from a cache, queue one button and block another.
Status changes must reach screen-reader users too. MDN says role="status" has an implicit aria-live of polite and aria-atomic of true, and not to give focus to a status when it updates; keep the container in the page before its text changes. No screen reader was tested here.
Trade-offs
| Choice | Gain | Cost |
|---|---|---|
Disable features when onLine is false |
Simple | Blocks users whose network works |
| Age label on cached data | User can judge trust | A saved-at field |
| Queue with status per change | Nothing lost | A queue and a status UI |
Build it
This lesson has no lab: it is a set of interface rules. The only code is the banner function above, run in plain Node; nothing about layout, appearance or assistive technology was run or seen.
In practice: the reorder button that went grey
This is a realistic scenario, not a case history. It is a composite built from common production patterns. It describes no specific company or product, and every number in it is an illustrative assumption.
The situation
The reorder button is disabled whenever navigator.onLine is false, and an "Offline" screen replaces the list. Shoppers on a corporate network report offline while the rest of the web works.
Starting architecture
Figure 4. Starting point: one flag controls the page
How to read this: the warning boxes show where an unreliable value gates a request that would have worked.
Options considered
| Option | What it fixes | What it costs | Risk |
|---|---|---|---|
| Keep the flag, tune the copy | Wording | Nothing | Users stay locked out |
| Hint only; failures decide | No false lock-outs | A slow-failing request | Long waits |
| Hint plus outbox and age labels | Lock-outs and lost work | Outbox, status UI | More to test |
The decision
| Field | Decision record |
|---|---|
| Context | The flag is wrong for some networks and disables a working button. |
| Decision | Use the flag only for a soft banner. Always try the request; on failure queue the reorder with a visible status and label cached data with its age. |
| Consequences, good | No false lock-outs; queued reorders are visible. |
| Consequences, bad | A failing request can take time to fail; a timeout is needed. |
| Revisit when | Requests must be deferred even when the connection is fine. |
Rollout and verification
- Write the status copy for each change state and the age label.
- Test the banner function for every combination.
- Force the cases by hand: airplane mode, a blocked API host, a server error, a reload with a queued change.
- Check with a screen reader that status text is polite and focus stays put.
The 2 a.m. failure
A release renders the status region only when a message exists. Screen-reader users stop hearing "waiting to sync" because the container is new each time.
First three checks: (1) is the live container in the page before its text changes; (2) does focus move; (3) does the queued count match the stored outbox. Mitigation: keep an always-present container. Follow-up: a test that it exists on first render.
Architect's notes
- Hint, not gate. Let failed requests decide behaviour, not the flag.
Common mistakes
- Disabling features when
navigator.onLineis false. - Marking a change synced before the server said so.
- Moving focus to announce a status.
- Assuming local storage is permanent.
Key takeaways
- Treat the connection flag as a hint; a failed request is the evidence.
- Show the age of cached data and a status for every change.
- Offer only the actions that can be kept, and explain the others.
Practice exercises
Exercise 1: Starter (30 min). Add a banner case for a change waiting more than an hour.
Exercise 2: Intermediate (2 hours). For the order tracking page, list each button as read, queue or block, with the sentence the user sees.
Exercise 3: Advanced (half day). Design what happens when a queued change is lost because storage was cleared.
Related reading
In this course:
- Offline-First Apps: Sync and Conflict Resolution: where the local copy and merging live.
- Offline Queues and Background Sync: the outbox behind the status labels.
- Service Worker Caching Strategies: the fallback and cache patterns.
- Optimistic Updates and Rollback: showing a change before the server confirms it.
Official documentation
- MDN: Navigator.onLine: why the value is only a hint.
- MDN: StorageManager.persist(): asking for persistent storage.
- MDN: Storage quotas and eviction criteria: best-effort and persistent data.
- web.dev: The Offline Cookbook: cache then network and the generic fallback.
- MDN: Background Synchronization API: deferring work until the connection is stable.
- MDN: ARIA status role: polite announcements.