Reliable requests · mid
Offline support: service worker lifecycle and caching strategies
A service worker is a script that sits between your pages and the network, so it can cache the app shell and answer requests offline using strategies like cache-first, network-first and stale-while-revalidate, while data and queued writes live in IndexedDB.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Lifecycle: register -> install (precache the app shell with cache.addAll) -> waiting (an updated worker waits until all tabs using the old one close) -> activate (delete old caches) -> fetch events. skipWaiting() and clients.claim() take over sooner; show a 'new version available, reload' prompt instead of surprising users.
- Scope: a worker controls URLs under its folder (default) and needs HTTPS (localhost is allowed).
- Strategies: cache-first for hashed static assets; network-first for HTML and important API data (fallback to cache offline); stale-while-revalidate for non-critical data like avatars; network-only for payments and auth; cache-only for precached files.
- The Cache API is separate from the HTTP cache; Cache-Control does not expire Cache API entries, so version your caches and clean them up.
- Data offline: IndexedDB for records; an outbox queue for writes made offline, replayed on reconnect (Background Sync is Chromium-only, so also retry on 'online' and app start); handle conflicts (last-write-wins or merge).
- Workbox gives tested implementations of these strategies; frameworks offer PWA plugins.
The red pen
Common traps: mistake → fix
Mistake: "Cache API responses forever for offline"
Fix: users see stale data with no invalidation; set expiry and versioning.
Mistake: "A new deploy takes effect at once"
Fix: the new service worker waits until old tabs close unless you handle updates.
Mistake: "Cache the HTML with cache-first"
Fix: users can be stuck on an old app version; use network-first for HTML.
Easy to hard
Questions interviewers ask, easy to hard
- What is a service worker and how is it different from a web worker?
- Explain the service worker lifecycle and the update problem.
- Which caching strategy for HTML, JS bundles, product images and the cart API?
- Make the app usable offline on flaky networks, including writes made offline.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.