Caching responses
We have been able to store content, including data and site assets using web storage and IndexedDB for several years. Using either medium requires a library or custom logic to manage site assets, which is why the service worker specification includes a specialized Cache API.
The Cache interface provides a managed storage mechanism for Request/Response object pairs. These objects are also referred to as network addressable resources, but you can think about them as just files or API responses.
The Cache API has a natural advantage over IndexedDB
and localStorage
because it is designed to abstract, persisting these assets by the Request
and Response
objects. This means that you can reference the assets using more than just a single key value, like localStorage
provides.
Besides providing a managed interface to store assets, it also allows you to organize those assets into groups or caches. As we progress through the remaining chapters, you will see how to use this power to segregate...