liveBook · Manning

Service worker

As explained in Vanilla Web

A service worker is a browser-managed background script that can intercept network requests and support offline behavior, caching, and other progressive web application features.

A service worker is a browser-managed worker script that can control pages after installation and activation. It intercepts requests within its scope, but cannot access the DOM. Treat it as an optional enhancement: the first visit uses the network, and the app’s core experience should still work if the worker is unavailable or not ready. section 14.8 section 14.8.1

Figure 14.1 The service worker sits between the web page and the network, including your own web server and third-party servers such as CDNs.
ch14 F001 firtman

The diagram shows the worker between a page and network servers, including third-party servers. section 14.8

Registration and control depend on the worker’s lifecycle and scope:

  • Register the worker from the page after checking browser support; its location determines which paths it can control. section 14.8.3
  • During installation, perform setup such as opening a versioned cache. Activation follows when older pages no longer use the previous worker, unless that wait is deliberately shortened. section 14.8.1

An offline-capable catalog

A conservative worker precaches the app shell, data file, and JavaScript modules. Navigations try the network first, then fall back to the cached shell; an offline page is the last resort. The app still works online when service workers are unsupported. section 15.8

How Vanilla Web covers it

How deep it goes
Explains registration, installation, activation, scope, and request strategies, then demonstrates a conservative offline setup.
Examples
Uses scoped registration and an offline catalog with a precached shell, network-first navigation, and fallback pages.
What you can do after
Configure a scoped worker that precaches app resources and serves a cached fallback when navigation cannot reach the network.
How it connects
Connects service workers to Cache Storage, fetch, messaging, IndexedDB, and web app manifests.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Vanilla Web. The section links point to the parts of the book it draws on.