Part IV — The Reach · Lesson 9.5
Background Sync & Storage Limits
In one lineThe browser decides when your queue runs and how long your data lives — design for both.
Where you've seen itA chat PWA in a tunnel. On Chrome the message goes out after you've closed the tab; on Safari it waits until you open the app again.
Diagram
Lessons in this diagram9.3 Offline Writes & the Sync Queue
STORAGE YOU CAN LOSE — the browser may delete it without asking
best-effort (default) evicted under disk pressure: least-recently
used sites first, a whole site's data at once
persisted storage.persist() granted → kept until the
user clears it
Safari, not installed script-written storage cleared after 7 days
of Safari use with no interaction on the site
navigator.storage.estimate() → { usage, quota } measure itHow it works
- Register a sync after every queued write where
SyncManagerexists; the service worker'ssynchandler calls the sameflush()the page uses. - Always wire the fallbacks as well: flush on
online, when the tab becomes visible, and on app start — Background Sync is Chromium-only. - Finish inside
event.waitUntil(). If the promise rejects, the browser retries later on its own schedule and stops after a few attempts (event.lastChance). - Ask for
persist()at a meaningful moment — after the first offline save, not on first load. - Unsent changes are the only data you can't re-fetch — flush early and show "2 notes not synced" rather than hoping.
// page: queue the write, then ask the browser to flush when it can
await outbox.add(op);
const reg = await navigator.serviceWorker.ready;
if ('sync' in reg) await reg.sync.register('flush-outbox');
else flush(); // Safari, Firefox: try now
addEventListener('online', flush); // …and on every reconnect
// sw.js: the browser wakes this when the device is back online
self.addEventListener('sync', (event) => {
if (event.tag === 'flush-outbox') event.waitUntil(flush());
});Trade-offs
| Mechanism | Limit |
|---|---|
| Background Sync | Chromium only; no guarantee of when it runs |
| Periodic Background Sync | Chromium, installed PWAs only; the browser picks the interval |
online and visibility events | Only while a page is open |
storage.persist() | Can be refused; granted by heuristics, or a prompt in Firefox |
Installing helps on iOS: a web app added to the Home Screen is not subject to Safari's 7-day storage cap — another reason to make install easy (9.2).
Interview angle
"Your offline notes app loses users' unsent notes on iPhone. Why, and what do you do?"
- Safari has no Background Sync, so the queue only flushes while the app is open — and if the site isn't installed, its storage can be cleared after 7 days of Safari use without the user interacting with it.
- Flush on open, on
onlineand on visibility change; encourage Add to Home Screen, and callpersist()after the first offline save. - Show unsent items clearly so nothing disappears silently, and keep the outbox small by collapsing edits (9.3).
Recap
- 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.