Describe the Service Worker lifecycle and cache versioning

Tests your grasp of the Service Worker lifecycle and cache versioning. A strong answer sequences register, install, activate, and fetch; notes install precaches assets while activate purges old caches and finalizes takeover.
What's really being asked
This question probes whether you understand the event-driven lifecycle of a Service Worker as a proxy layer, not just the API signatures. Interviewers want to see that you know why activation is deferred, how cache versioning avoids storage bloat and corruption, and how the worker transitions from installation to controlling pages.
The full answer
First, the four main phases in order. Registration via navigator.serviceWorker.register fetches the script. Second, the install event fires first and is used to precache core static assets into CacheStorage so the app shell is available offline. Third, the activate event is critical because the new worker will not take over until all pages controlled by the previous worker have closed; during activation you delete old caches that do not match the current cache name to prevent stale assets and quota exhaustion. Fourth, the fetch event intercepts network requests after activation and allows cache-first, network-first, or stale-while-revalidate strategies. A senior candidate also mentions skipWaiting to force immediate activation and clients.claim to take control of unclaimed clients.
The mistakes people make
Saying that a newly installed worker immediately controls all open tabs without mentioning the waiting phase. Claiming that install and activate are synchronous or happen at the same time. Stating that Service Workers have DOM access or run on the main thread. Failing to mention cache cleanup during activate, which leads to unbounded storage growth and version mismatches between HTML and cached assets.
What usually comes next
How does skipWaiting differ from clients.claim? What cache strategy would you use for API responses versus static assets? How do you handle a failed install or a partial cache? How does the browser decide when to check for an updated service worker? What is navigation preload and when would you use it?
A concrete example
Suppose your app uses cache-v1 for install. You update the shell and register cache-v2. During the new install event you populate cache-v2 with index.html and bundle.js. The new worker then waits because a user still has a tab open with the old worker. Once that tab closes, activate fires; inside it you call caches.keys and delete any cache whose name is not cache-v2. Now fetch events serve from cache-v2, ensuring the HTML and JS versions never mismatch.
Interview question
A Service Worker updates its cache from cache-v1 to cache-v2. When does the new worker begin handling fetch events, and when should cache-v1 be removed?
- a.Only if skipWaiting is explicitly called; otherwise cache-v1 remains active indefinitely across sessions.
- b.As soon as the worker script is registered; cache-v1 is kept as a fallback during fetch.
- c.Immediately after the install event populates cache-v2; cache-v1 is deleted during install to prevent storage bloat.
- d.After all pages controlled by the previous worker have closed; cache-v1 is removed during the activate event.Correct
Why? this is the answer
The new worker waits until all tabs using the previous worker close, then activates and deletes old caches like cache-v1 to prevent version mismatches and quota exhaustion. Option C is tempting but wrong because installation does not grant immediate control, and deleting cache-v1 during install would break existing tabs still served by the old worker.
Just read this? Test yourself on what you have been reading.
Read the original → developer.mozilla.org
- #service worker
- #cache api
- #pwa
- #offline first
- #web apis
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles