Lesson 27 of 30
A Service Worker is a script that sits between your app and the network, on its own thread — the mechanism that makes offline support, background sync, and push notifications possible on the web.
13 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 2
What's the actual difference between a Web Worker and a Service Worker?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
A Service Worker is a special kind of Web Worker (from the earlier worker lesson) that specifically acts as a programmable network proxy for your app — it can intercept every request your page makes and decide: serve from cache, go to the network, or some combination of both.
// sw.js — a minimal offline-first cache strategy
self.addEventListener("fetch", (event) => {
event.respondWith(
caches.match(event.request).then((cached) => cached || fetch(event.request))
);
});
This intercepts every request; if a cached response exists, serve it instantly (works offline); otherwise fall back to the real network. A Progressive Web App (PWA) is the broader package this enables: a web app that can be installed to a home screen, works offline (or gracefully degrades), and can receive push notifications — using only web technology, no app-store build required.
navigator.serviceWorker.register('/sw.js') — installs a script that runs separately from the page, even after it's closed.
"How would you make a web app work offline?" is a direct, practical question, and Service Workers are the concrete underlying mechanism — knowing they're a distinct concept from Web Workers, with a genuinely different job, is a common, easy-to-verify point of confusion worth getting right.