Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to delete service worker cache on localhost

please see the code of service worker below:

var CACHE_NAME = 'my-site-cache-v18';
var urlsToCache = [
  '1.jpg',
  '2.png'
 ];

self.addEventListener('install', function(event) {
  // Perform install steps
  event.waitUntil(
    caches.open(CACHE_NAME)
      .then(function(cache) {
        console.log('Opened cache');
        return cache.addAll(urlsToCache);
      })
  );
});


self.addEventListener('activate', function(event) {
  event.waitUntil(
    caches.keys().then(function(cacheNames) {
      return Promise.all(
        cacheNames.filter(function(cacheName) {
        }).map(function(cacheName) {
          return caches.delete(cacheName);
        })
      );
    })
  );
});

self.addEventListener('fetch', function(event) {
  event.respondWith(
    caches.match(event.request)
      .then(function(response) {
        if (response) {
          return response;
        }
        return fetch(event.request);
      }
    )
  );
});

The issue I am facing is in the inspect element of chrome the cache storage graph is continue growingenter image description here and when I see the the cache storage in file explorer old folders are not deleted. every time i refresh the page creates a new folder. enter image description here

these encrypted folders are increasing every time I change the "CACHE_NAME" (the verion of the cache).

Please Help. I tried a lot but unable to solve it.

like image 817
Rajkumar Gour Avatar asked Oct 19 '25 03:10

Rajkumar Gour


2 Answers

To use the filter(), the arguement function you are passing in the filter() does not return anything, whereas you need to return a cacheName value from the function.

 cacheNames.filter(function(cacheName) {
    //you need to return the cachename which you want to 
    //delete from the cache by specifying the conditions
    }).map(function(cacheName) {
          return caches.delete(cacheName);
    })

for example-

var staticCacheName = 'my-site-v18';
self.addEventListener('install', function(event) {
  var urlsToCache = [
    '1.jpg',
    '2.jpg'
  ];

  event.waitUntil(
  caches.open(staticCacheName).then(function(cache){
    return cache.addAll(urlsToCache);
  }))

});

self.addEventListener('activate',function(event){
  console.log('activating');
  event.waitUntil(
    caches.keys().then(function(cacheNames){
      console.log(cacheNames);
      return Promise.all( 
        cacheNames.filter(function(cacheName){
        return cacheName.startsWith('my-site') && cacheName != staticCacheName ;
        }
        ).map(function(cacheName){
          return caches.delete(cacheName);
        })
      );
    })
  );
});
like image 72
Aman Rathore Avatar answered Oct 21 '25 15:10

Aman Rathore


you might need to change your activation event and try another approach.

i use it like that:

self.addEventListener('activate', function(e) {
    console.log('[ServiceWorker] Activate');

    e.waitUntil(
        caches.keys().then(function(keyList) {
          return Promise.all(keyList.map(function(key) {
            if (key !== cacheName) {
              console.log('[ServiceWorker] Removing old cache', key);
              return caches.delete(key);
            }
          }));
        })
    );

    return self.clients.claim();
});
like image 33
André Kelling Avatar answered Oct 21 '25 15:10

André Kelling



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!