July 2018
Intermediate to advanced
354 pages
8h 51m
English
The keys method returns a promise with an array of names (strings) for each named cache. This list can be iterated over for processing.
The following example is placed within the service worker activate event handler. It removes caches that were crafted for a previous service worker version. This concept was covered in Chapter 5, The Service Worker Life Cycle:
caches.keys().then(function (cacheNames) {
cacheNames.forEach(function (value) {
if (value.indexOf(version) < 0) {
caches.delete(value);
}
});
return;
})
Notice the delete method does not have any processing after a cache is deleted. If you wanted to log any problems regarding deleting a cache, you could do that here.
Read now
Unlock full access