Service workers explained in plain terms means a script that a browser runs in the background, separate from any open page, to manage what a website stores and serves. The central question is how a site keeps working when the connection drops, and the short answer is that the service worker intercepts requests and answers them from a local store when the network is gone.
Most websites stop the moment the connection fails. A page half-loaded stalls, a link tap leads to an error, and anything not already on screen is out of reach. That fragility is normal for the web, because a browser usually fetches every page fresh from a server each time it is asked for.
A service worker changes that pattern. Once a site registers one, the browser keeps it ready in the background, so it can serve saved pages, show notifications, and sync small updates without a page being open. It is the quiet engine behind offline websites, installed web apps, and push messages.
Key takeaways
- A service worker is a script the browser runs in the background, separate from any open web page.
- It intercepts network requests and can answer them from a local cache instead of the server.
- That cache is what lets a website show saved pages when the connection is down.
- Service workers also handle push notifications and background sync when no page is open.
- They only work on secure connections and only for pages on the same site that registered them.
What service workers are
A service worker is a JavaScript file that a website asks the browser to install and run on its behalf. It does not appear on screen, and it has no direct access to the page you are looking at. Instead, it sits between the page and the network, deciding what to keep.
The separation matters. A normal script stops when you close the tab, but a service worker can be woken by the browser when an event arrives, such as a push message. That is why a site can notify you even when you have not opened it for days.
Service workers are built on open web standards, and they are supported by the major browsers on phones and computers. They are a core part of progressive web apps, which use them to offer offline pages and an installed, app-like experience.
The idea is sometimes confused with a web worker, which is a different tool. A web worker runs heavy work off the main thread so a page stays smooth, while a service worker acts as a programmable middleman for network traffic. The two solve different problems, even though both run apart from the page itself.
How service workers work
Registration comes first. When you visit a site, its page asks the browser to register a service worker file from the same site. The browser downloads that file, checks it, and installs it in the background. On later visits, the browser starts the worker before the page loads, so it is ready to handle requests.
Once active, the worker can listen for fetch events, which fire each time the page asks for something: a page, an image, a style sheet, or a piece of data. The worker can let the request pass to the network, answer it from the cache, or fetch it and store a copy. Which choice it makes depends on the strategy the site owner wrote into the script.
One common strategy is cache first for files that rarely change, such as logos and styles, and network first for content that must be fresh, such as a news front page. Another is to try the network, and fall back to the cache only when the network fails. Each approach trades freshness against speed and offline reliability in a different way.
When a new version of the worker is published, the browser installs it alongside the old one, but the old one keeps serving pages until every tab using it is closed. That handover avoids breaking a visit halfway through, although it can delay an update for visitors who keep a tab open. Owners plan for this by keeping old and new versions able to work with the same cached files.
Push and sync use the same background role. A server can send a push message that wakes the worker, which then shows a notification, and the worker can hold small updates until the connection returns, then send them. Our guide to caching explains the storage behind this, since a service worker is only useful if the right files are saved before they are needed.

Comparing service workers and normal scripts
Service workers and the ordinary scripts on a web page both run code in the browser, but they live in different places and answer to different rules. A page script works on what you can see, while a service worker works on the traffic between the page and the network, mostly out of sight.
| Aspect | Service worker | Normal page script |
|---|---|---|
| Where it runs | In the background, separate from any open page | Inside the page, tied to one open tab |
| When it stops | Kept by the browser and woken by events | Stops as soon as the tab is closed |
| Network control | Can intercept requests and serve cached replies | Can only ask the network for data, not intercept it |
| Page access | No direct access to the page or its content | Full access to the page it runs on |
| Typical jobs | Offline pages, push notifications, background sync | Menus, forms, animations, and page interaction |
Where service workers are used
News and publishing sites use service workers so readers can keep reading when the connection drops. An article opened on a train can finish loading from the cache, and the front page can show a saved copy with a note that it may be out of date. That small kindness keeps readers on the site instead of losing them to an error page.
Shops use them to keep browsing fast and resilient. Product images, styles, and the basket page can be served from the cache, so a shopper on a weak signal still moves through the store. If the connection fails at checkout, the worker can hold the attempt and sync it when the signal returns, rather than losing the order.
Travel services rely on them where connections are poor by nature. A boarding pass, a ticket, or a timetable saved in advance stays available in an airport or on a train, and a push message can warn of a delay or a gate change. The worker makes the saved copy trustworthy by keeping it separate from live data that must be fresh.
Tools and simple apps use service workers to feel instant. A note-taking app, a calculator, or a checklist that opens from the cache starts at once, with no wait for the network. Our guide to how the web works covers the request-and-response pattern these tools sit on top of, and the worker is the layer that decides when that pattern can be skipped.
Strengths and limits
The greatest strength of a service worker is resilience. A website no longer fails completely when the network does, because the worker can serve a saved version of the pages people need most. That matters most on mobile networks, in lifts, on trains, and in places where the signal comes and goes.
Speed is the second strength. Files served from a local cache arrive far faster than files fetched over a network, so repeat visits feel instant. A worker can also fetch and store files quietly during an earlier visit, so the next visit has everything ready before the visitor asks for it.
The main limit is complexity. Writing a worker that caches the right files, serves fresh content when it matters, and updates cleanly is harder than it looks. A mistake can leave visitors stuck on an old version, or served a mix of old and new files that do not fit together. Testing on real devices and slow networks is the only reliable way to catch those faults.
There is also a limit in scope and safety. A worker only controls pages on the site that registered it, and only over a secure connection, which is a deliberate guard against abuse. A worker with wide power in the wrong hands could alter what visitors see, so browsers enforce those limits strictly. Our guide to content delivery networks explains the faster delivery that pairs with this model, since a worker works best when the first visit already loads quickly.
Service workers checklist
- Serve the site over a secure connection, since a service worker cannot register without one.
- Register the worker from a page on the same site, and keep its file at the right scope.
- Cache only the core files first, such as the home page, styles, images, and an offline page.
- Choose a caching strategy per file type, favouring fresh content where freshness matters.
- Give cached pages a clear note when they may be out of date, so visitors are not misled.
- Version the cache, and delete old copies when a new worker takes over.
- Test with the connection switched off, on a slow network, and after an update.
- Keep push notifications useful and infrequent, with a clear way to switch them off.
- Check the worker in each major browser, since behaviour and tools differ between them.
Frequently asked questions
What is a service worker in simple terms?
A service worker is a script that runs in the background of your browser and manages a website’s network traffic. It can store pages and files, serve them when the connection is down, and show notifications when the site is not open.
Do service workers work when the browser is closed?
Mostly no. A worker can be woken briefly by an event such as a push message, but it does not run all the time. Once it has shown a notification or finished its task, the browser puts it back to sleep.
Can a service worker see what I type on a page?
No. A service worker has no direct access to the page or to what you type into it. It can only see the network requests the page makes, such as asking for an image or a page.
Why does a website need a secure connection for a service worker?
Because a worker has wide power over what visitors are shown, browsers only allow it on secure connections. That stops an attacker on an unprotected network from registering a worker and altering a site.
What happens when a service worker is updated?
The browser installs the new worker alongside the old one, and the old one keeps serving pages until its tabs are closed. The new worker then takes over, usually with a fresh cache, so visitors get the new version on their next visit.
Do all websites need a service worker?
No. A simple site that is only read online gains little from one. Service workers suit sites where offline reading, fast repeat visits, or notifications matter, such as news, shops, travel, and installed web apps.
The bottom line
Service workers explained in one line is this: a background script that stands between a website and the network, serving saved copies when the connection fails and handling notifications and sync when no page is open.
The trade is clear. You gain resilience, speed, and offline use, and you take on real complexity in caching, updates, and testing. For sites where the connection cannot be trusted, that trade is worth making.
This article is general information about how service workers work, not advice about building software for a particular website.







