Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between caches.match and cache.match

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;
        });
      });
    })
  );
});
like image 691
SimplifyJS Avatar asked Sep 03 '26 05:09

SimplifyJS


1 Answers

cache.match searches for an item in a specific cache, while caches.match searches all caches for a match.

like image 199
geddski Avatar answered Sep 04 '26 18:09

geddski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!