Rajkumar Gour
Rajkumar Gour

Reputation: 1159

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.

Upvotes: 4

Views: 7524

Answers (3)

Aman Rathore
Aman Rathore

Reputation: 88

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);
        })
      );
    })
  );
});

Upvotes: 2

André Kelling
André Kelling

Reputation: 1736

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();
});

Upvotes: 1

imagio
imagio

Reputation: 1490

Does the cache usage grow every time you reload the page?

This may be due to a bug in chrome. It has been fixed and looks like it will go out in v65.

https://bugs.chromium.org/p/chromium/issues/detail?id=801024

Upvotes: 0

Related Questions