deleteOutdatedCaches.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. /*
  2. Copyright 2018 Google LLC
  3. Use of this source code is governed by an MIT-style
  4. license that can be found in the LICENSE file or at
  5. https://opensource.org/licenses/MIT.
  6. */
  7. import '../_version.js';
  8. const SUBSTRING_TO_FIND = '-precache-';
  9. /**
  10. * Cleans up incompatible precaches that were created by older versions of
  11. * Workbox, by a service worker registered under the current scope.
  12. *
  13. * This is meant to be called as part of the `activate` event.
  14. *
  15. * This should be safe to use as long as you don't include `substringToFind`
  16. * (defaulting to `-precache-`) in your non-precache cache names.
  17. *
  18. * @param {string} currentPrecacheName The cache name currently in use for
  19. * precaching. This cache won't be deleted.
  20. * @param {string} [substringToFind='-precache-'] Cache names which include this
  21. * substring will be deleted (excluding `currentPrecacheName`).
  22. * @return {Array<string>} A list of all the cache names that were deleted.
  23. *
  24. * @private
  25. * @memberof module:workbox-precaching
  26. */
  27. const deleteOutdatedCaches = async (currentPrecacheName, substringToFind = SUBSTRING_TO_FIND) => {
  28. const cacheNames = await self.caches.keys();
  29. const cacheNamesToDelete = cacheNames.filter((cacheName) => {
  30. return cacheName.includes(substringToFind) &&
  31. cacheName.includes(self.registration.scope) &&
  32. cacheName !== currentPrecacheName;
  33. });
  34. await Promise.all(cacheNamesToDelete.map((cacheName) => self.caches.delete(cacheName)));
  35. return cacheNamesToDelete;
  36. };
  37. export { deleteOutdatedCaches };