Service worker events
A service worker has several events. You can use these to manage your application's cache. We have already looked at using theinstallandactivateevents to precache responses in Chapter 5, The Service Worker Life Cycle.
The all-star service worker event is fetch
. This event fires each time a network-addressable asset (URL) is requested. By adding a fetch
event handler to your service worker, you can intercept all network requests, triggering a workflow to determine how to respond:
self.addEventListener("fetch", event => { //process request and return best response });
As you learned Chapter 6, Mastering the Cache API – Managing Web Assets in a Podcast Application, you can use the Fetch API's custom request
and response
objects to inspect requests and create or clone network responses.
The event variable supplied by the fetch
event handler has a request property. This property is a request
object. You can use the request
object to determine how you will return the...