We use caches.match(event.request) in the service worker to do "Cache only strategy". I noticed that we also return cache.match('someURL') right after then of caches.open("cache-name") promises. This is quite confusing.
What is the difference between caches.match(event.request) and cache.match('someURL'). What is the use case for each of it?
Example cases:
Caches.match
self.addEventListener('fetch', function(event) {
event.respondWith(caches.match(event.request));
});
Cache.match
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.open('mysite-dynamic').then(function(cache) {
return cache.match(event.request).then(function (response) {
return response || fetch(event.request).then(function(response) {
cache.put(event.request, response.clone());
return response;
});
});
})
);
});
cache.match searches for an item in a specific cache, while caches.match searches all caches for a match.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With