Progressive Web Apps and Service Workers
Make web apps installable and offline-capable with a manifest and service worker caching, and re-engage users with push notifications.
Advanced · about 6 hours · difficulty 4/5
Learn first: Client-Side Storage, HTTP Caching, Compression and CDNs
A Progressive Web App (PWA) is a website that can be installed to the home screen or desktop, launch in its own window, work offline or on flaky networks and send notifications, all from one codebase.
The building blocks
- HTTPS (required).
- A web app manifest (
manifest.webmanifest): name, icons, theme colour,start_urlanddisplay: "standalone". This makes the app installable. - A service worker: a script that runs in the background, separate from pages, and can intercept network requests from your origin.
Service worker lifecycle
Register it from your page; it goes through install (pre-cache core assets), waiting and activate (clean old caches). A new version waits until all tabs using the old one close, unless you prompt the user to update and call skipWaiting. Getting updates wrong is the most common PWA bug, so plan the update flow.
Caching strategies
- Cache first for hashed static assets.
- Network first for HTML and API data that should be fresh, falling back to cache offline.
- Stale-while-revalidate for things like avatars and non-critical data.
- An offline fallback page for navigation requests.
Workbox, or vite-plugin-pwa which uses it, generates most of this for you.
Notifications and more
The Notifications API shows system notifications after the user grants permission; the Push API lets a server wake the service worker to show one even when the site is closed (iOS supports this for installed web apps). Ask for permission in response to a user action with a clear reason, never on page load. Background Sync can retry failed requests when connectivity returns (limited browser support).
Good enough to move on
You can turn an app into an installable PWA with an offline fallback, sensible caching strategies, a working update prompt and an opt-in notification.
Key concepts
- Web app manifest and installability
- Service worker registration and lifecycle
- Caching strategies
- Offline fallback pages
- Update flow and skipWaiting
- Workbox and vite-plugin-pwa
- Notifications and Push API
- Background Sync
Mini project
Turn your notes or expense app into a PWA with vite-plugin-pwa: installable, works offline with cached data, shows an 'update available' prompt and sends a reminder notification after the user opts in.
Check yourself
You deployed a fix but users keep seeing the old version of your PWA. What part of the service worker lifecycle explains this?
Free resources
- Learn PWA (web.dev)
- Service Worker API (MDN)
- Workbox (Chrome for Developers)