Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Service workers and offline web apps

After this lesson: I can explain how a page works offline with a service worker and a cache.

60 min lesson2 commandsMobile web lab: a responsive, installable web app to build in the browser

A service worker lets pages open offline

A service worker is a script that a browser keeps, separate from the page, which can answer the page's requests from a saved copy. This is how a web app can open on a train with no signal.

You will learn what it does, how it saves files, and how to choose what to do with each request.

By the end of this lesson

Note

I can explain how a page works offline with a service worker and a cache.

Key words

WordWhat it means
Service workera script the browser keeps that can answer a page's requests
Cachea store of saved copies of files
Offlinewith no working network connection
Installthe moment a new worker saves its files
Strategya rule for choosing between the network and the cache

The cells students fill in are left blank in this preview.

A service worker sits before the network

Normally a page asks the network for a file and waits. With a service worker, the request goes to the worker first. The worker can answer from the cache or pass the request on to the network.

Because it can answer without the network, a page can open when the phone has no signal.

Why service workers need a secure address

A service worker can answer any request for its site, so a fake one would be dangerous. A browser therefore allows it only on a secure address, which means https. A page on your own computer, called localhost, is also allowed for testing.

A browser may refuse a worker on a plain preview address. When that happens its developer console says so. On a secure address you would expect the worker to be listed as activated and running.

Ask the preview server for three files

Command
docker exec jupyter python3 steps/serve.py

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

What the server check prints

The script asks the lab's preview server for the page, the manifest and the worker file. Each answers with 200, which means it was found, and a content type.

The worker file is an ordinary file the server hands out. Whether the browser then runs it as a worker is the browser's decision, and it depends on the address.

A small worker, line by line

Code: web
const CACHE = "habits-v1";
const FILES = ["/", "/style.css", "/app.js"];

self.addEventListener("install", (event) => {
  event.waitUntil(caches.open(CACHE).then((c) => c.addAll(FILES)));
});

// install runs once. It opens a cache and saves every listed file.
// If one file cannot be saved, the whole install fails.

Check the files the worker will save

Command
docker exec jupyter python3 steps/offline.py

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

What the offline check prints

The script finds the cache name, habits-v1, and the list of eight files the worker saves. It checks that each file exists on disk.

Seven exist. One does not, /offline.html. That is a real fault, because a worker saves the whole list in one step. If one file is missing, the install fails and nothing works offline.

Worked example: three ways to answer a request

StrategyHow it worksGood for
Cache firstUse the saved copy. Ask the network only if there is noneStyles, scripts and icons that rarely change
Network firstAsk the network. Use the saved copy only if it failsPages and data that change often
Network onlyNever use the cacheSending a new item to a server

The cells students fill in are left blank in this preview.

A common mistake

Warning

Many people think a cached file updates itself. It does not. A cache first worker keeps serving the saved copy for ever. When you change a file, you must change the cache name, such as from habits-v1 to habits-v2, so a new install saves the new copy.

About this lab and its source: Mobile web lab: a responsive, installable web app to build in the browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.