From the course: JavaScript: Service Workers

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Implement a cache-first policy

Implement a cache-first policy - JavaScript Tutorial

From the course: JavaScript: Service Workers

Start my 1-month free trial

Implement a cache-first policy

- [Instructor] Back in our California project we have already saved all the files in the cache, but no one is serving those files. So we actually need to listen for the fetch event handler, the same that we did before. And we're going to do this, we are going to respond with, and we need to respond with a promise of a response or an actual response. So we need to go to the cache and see if it's there. So to go to the cache we need to use caches.match. Caches.match will look for a request in that cache, and it will return a promise with the found object if it was found. For example in this case we want to search for the request that the browser is actually asking for. So event.request, and we're going to match that request. In this case we are not saying in which cache we want to search, so this API will look into all the caches available in our origin. So if we want to specify exactly the cache, we can also do that first. Opening the cache and then executing match inside the cache…

Contents