Chapter 9 · Offline Support
Revision sheet
Every lesson's idea and recap on one page. Read it before an interview, or print it.
-
9.1Service Workers
A background script that survives your page, proxies its requests, and updates on its own schedule.
- Install → waiting → activated → controlling; the waiting step is where deploys appear stuck.
- No DOM, no sync storage — the SW lives on its own thread with fetch, Cache Storage, and IndexedDB.
- Offline writes need a queue plus a conflict rule, not just a cache.
-
9.2Progressive Web Applications (PWAs)
A website that installs, launches from the home screen, works offline, and receives push.
- HTTPS + manifest + service worker is the whole installability bar.
- Every capability above the shell is progressive — feature-detect and degrade.
- Ask for push in context; a first-load prompt is a permanent denial.
-
9.3Offline Writes & the Sync Queue
Write locally first, queue every change, and replay the queue so the server applies each change exactly once.
- Local write and outbox entry in one transaction; the UI never waits for the network.
- Client-made IDs as idempotency keys make every retry safe.
- Push oldest-first, then pull from a cursor; park failures where the user can see them.
-
9.4Conflict Resolution
Two offline edits to the same thing will collide — choose the rule before it happens, per field, not per app.
- Conflicts are detected with base versions, never with device clocks.
- Choose the rule per field: last write wins, merge, ask, or CRDT.
- Whatever loses must be recoverable — silent loss is the real bug.
-
9.5Background Sync & Storage Limits
The browser decides when your queue runs and how long your data lives — design for both.
- Background Sync is a Chromium bonus;
online, visibility and app start are the floor. - Local data is best-effort until persisted — and Safari may clear it after 7 days.
- Unsent changes are irreplaceable: flush early, show them, never drop them.
- Background Sync is a Chromium bonus;
Capstone
Design an offline-first note app: cache strategy, write queue, sync watermark, and the conflict rule you'd defend.