Frontend / Service workers

Service workers

A script the browser runs behind your site, between the page and the network. Watch it install, take control, answer requests from a cache, keep working offline and receive a push.

Intermediate7 steps~7 min
Page — the site as open in a tab
Service worker — background script in the middle
Network — the site’s server, when reachable
Cache — saved responses, kept on the device
// THE SHORT VERSION

In 30 seconds

A service worker is a script the browser runs in the background, apart from any page. It cannot touch the page itself. It reacts to events.

It is installed once, usually saving the site’s core files in a cache, then activated. After that, every network request the site’s pages make passes through it first.

Its code chooses how to answer each one: from the cache, from the network, or a mix. That is how a site can load instantly, open with no connection, and receive push messages when no tab is open.

Service workers, step by step

STEPS01 / 07

Simplified: real workers also control only pages inside their scope, delete outdated caches when they activate, and save fresh copies in the network-first strategy. Code lines are shortened.

STEP 01 / 07 A script outside the page
THE MATH
navigator.serviceWorker .register('/sw.js')
  1. Step 1: A script outside the page. The page asks the browser to register a script, sw.js. The browser downloads it and runs it apart from the page, on its own thread. It cannot touch the page’s elements, and it is not always running: the browser wakes it for events and lets it sleep in between.
  2. Step 2: Install: stock the cache. The first event the worker receives is install. It uses it to download the files the site cannot start without and to store copies in a cache it controls. If any download fails, the install fails and the browser tries again on a later visit.
  3. Step 3: Activate: take control. After a successful install the worker activates. From then on it controls the site’s pages, starting with their next load. An updated worker installs in the background but then waits, so that a page never switches workers mid-visit; it takes over once the old one’s tabs are closed.
  4. Step 4: Every request passes through. Once in control, the worker sits between the page and the network. Each request the page makes, for a stylesheet, an image or data, arrives at the worker as a fetch event first. The worker’s code decides where the answer comes from.
  5. Step 5: Strategy 1: cache first. For files that rarely change, look in the cache first. The copy stored at install is there, so the worker answers at once and the network is never contacted. Only on a miss does it fall through to a normal fetch.
  6. Step 6: Network first, cache offline. For content that changes, ask the network first and keep a copy of each good answer. When the device is offline the fetch fails, and the worker falls back to the saved copy, so the page still opens. A third strategy, stale-while-revalidate, answers from the cache and refreshes the copy in the background.
  7. Step 7: Push, with the page closed. Because the worker is tied to the site rather than to any one tab, the browser can wake it when no page of the site is open. A server sends a push message through the browser’s push service, the worker receives a push event and shows a notification. The user must have granted permission first.
VALUES SO FAR
threadits ownrule
DOMno accesslimit
cache v1····install
state····lifecycle
fetch····event
static····strategy
changing····strategy
offline····fallback
push····event

Spotted a mistake on this page?