Hand-written service workers are fine for small sites. Once you have hundreds of hashed files, several cache types and expiration rules, a library saves time and bugs.
Workbox
Workbox is a set of libraries originally built by Google's Chrome team. It has been the most widely used service worker library for years, and many build tools and frameworks use it underneath. Its main modules:
- Precaching (
workbox-precaching): takes a list of files with revision hashes, made at build time, and keeps them up to date. - Routing (
workbox-routing): match requests by URL, pattern or function. - Strategies (
workbox-strategies):CacheFirst,NetworkFirst,StaleWhileRevalidate,NetworkOnly,CacheOnly. - Plugins:
ExpirationPlugin,CacheableResponsePlugin,BackgroundSyncPlugin,BroadcastUpdatePlugin,RangeRequestsPlugin. - Build tools:
workbox-cli,workbox-build,workbox-webpack-pluginwithgenerateSW(writes the whole worker) andinjectManifest(adds the precache list to your own worker). - workbox-window: helps pages register the worker and react to updates.
import { precacheAndRoute } from 'workbox-precaching';
import { registerRoute } from 'workbox-routing';
import { NetworkFirst, StaleWhileRevalidate, CacheFirst } from 'workbox-strategies';
import { ExpirationPlugin } from 'workbox-expiration';
precacheAndRoute(self.__WB_MANIFEST);
registerRoute(({ request }) => request.mode === 'navigate', new NetworkFirst({ cacheName: 'pages' }));
registerRoute(({ request }) => request.destination === 'image',
new CacheFirst({ cacheName: 'images', plugins: [new ExpirationPlugin({ maxEntries: 60, maxAgeSeconds: 30 * 24 * 3600 })] }));
registerRoute(({ request }) => ['style', 'script'].includes(request.destination), new StaleWhileRevalidate({ cacheName: 'assets' }));
Workbox is still maintained, with a slower pace than in its early years (version 7.4 shipped in November 2025).
Serwist
Serwist is a fork of Workbox, started because Workbox development had slowed. Its API is close to Workbox's, but it is modernised: one serwist package instead of many modules, TypeScript first, ES modules, and integrations maintained in the same repository: @serwist/next, @serwist/vite, @serwist/svelte and others.
import { Serwist } from 'serwist';
import { defaultCache } from '@serwist/next/worker';
const serwist = new Serwist({
precacheEntries: self.__SW_MANIFEST,
skipWaiting: false,
clientsClaim: true,
navigationPreload: true,
runtimeCaching: defaultCache,
});
serwist.addEventListeners();
Which one?
| Situation | Choice |
|---|---|
| Vite, Nuxt, SvelteKit, Astro, React, Vue | vite-plugin-pwa (uses Workbox underneath) |
| Next.js | Serwist (@serwist/next) |
| Angular | @angular/service-worker |
| webpack, or an existing Workbox setup | Workbox, or migrate to Serwist when convenient |
| A small static site | Hand-written: use our generator |
Avoid the older next-pwa package for new projects: it is no longer actively maintained, and Serwist is its natural successor.