deleteOutdatedCaches.mjs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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.mjs';
  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 (
  28. currentPrecacheName,
  29. substringToFind = SUBSTRING_TO_FIND) => {
  30. const cacheNames = await caches.keys();
  31. const cacheNamesToDelete = cacheNames.filter((cacheName) => {
  32. return cacheName.includes(substringToFind) &&
  33. cacheName.includes(self.registration.scope) &&
  34. cacheName !== currentPrecacheName;
  35. });
  36. await Promise.all(
  37. cacheNamesToDelete.map((cacheName) => caches.delete(cacheName)));
  38. return cacheNamesToDelete;
  39. };
  40. export {deleteOutdatedCaches};