The Service Worker Cookbook is a collection of working, practical examples of using service workers in modern web apps.
Tip: Open your Developer Tools console to view
fetch events and informative messages about what each recipe's service worker is doing!
This recipe shows how to have the service worker immediately take control of the page without waiting for a navigation event.
This recipe shows how to communicate between the service worker and a page and shows how to use a service worker to relay messages between pages.
This recipe shows 2 standard ways of loading a remote resource and one way to use service worker as a proxy middleware.
This recipe provides a way to learn how to use service workers (SW) through showing the flow diagram of SW workflow explained on the Mozilla Developer Network, and logging on screen the steps taken by a real Web App running service workers.
This recipe illustrates fetching a JSON file during service worker installation and adding all resources to cache. This recipe also immediately claims the service worker for faster activation.
This recipe shows how to serve content from the cache when the user is offline.
This basic recipe illustrates caching critical resources for offline use and then notifying the user that they may go offline and enjoy the same experience.
This recipe shows a service worker acting like a remote server.
Perform API usage logging without interfering with the UI layer by adding a service worker to gather the usage and use the sync API to upload gathered data from time to time.
This recipe shows a Service Worker containing network logic to dynamically select the best content provider accordingly to server availability.
This recipe illustrates how to cache contents from a zipfile.
This recipe shows how a Service Worker can act as a dependency injector, avoiding hard wiring dependencies for high level components.
This recipe shows how to enqueue requests while in offline in an outbox-like buffer to perform the operations once the connection is regained.
This recipe illustrates methods to return network requests from either the cache or network.
The recipe demonstrates one recommendation from the NGA. A cache containing the interpolated templates in order to avoid model fetching and render times upon successive requests.
Simplest example of Web Push API usage. Send notifications to users even when your page is not open.
Show rich push notifications, defining the language of the notification, a vibration pattern, an image to associate to the notification. See https://notifications.spec.whatwg.org/#api for the other parameters you can set (e.g. a set of actions that can be activated from the notification).
Send push notifications with a payload. This recipe shows how to send and receive a string, but data can be extracted from a Push message in a variety of formats (string, ArrayBuffer, Blob, JSON).
Send push notifications and retrieve a payload once a notification is received.
Use the notification tag to replace old notifications with new ones. Allows you to show only up-to-date information to your users or collapse multiple notifications into a single one.
Experiment with the quota management policies of different browsers. Try sending many notifications (visible or invisible) and see what happens if you keep the tab open vs close it, or if you click on some notifications vs you click on none of them.
Control the clients of a service worker when the user clicks on a notification generated from a push event. Allows you to focus the tab of your app or even re-open it if it was closed.
This recipe shows how to use push notifications with subscription management.