Search⌘ K
AI Features

Retrieving Items from the Cache

Explore how to handle fetch events in service workers to retrieve resources from the cache. This lesson helps you understand caches.match and respondWith methods to implement offline-first behavior in PWAs, ensuring assets are served from cache when available.

Finding an item in the cache

In the service worker’s fetch event handler, we can listen to the fetch events and check our cached assets to see whether the requested resource is available in the cache. We’ll use the caches.match() method to search all our sub-caches for the requested resource. This mechanism of checking and responding with cached assets is implemented below.

The figure below illustrates that the service worker ...