cleanupOutdatedCaches.js 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. /*
  2. Copyright 2019 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 { cacheNames } from 'workbox-core/_private/cacheNames.js';
  8. import { logger } from 'workbox-core/_private/logger.js';
  9. import { deleteOutdatedCaches } from './utils/deleteOutdatedCaches.js';
  10. import './_version.js';
  11. /**
  12. * Adds an `activate` event listener which will clean up incompatible
  13. * precaches that were created by older versions of Workbox.
  14. *
  15. * @memberof module:workbox-precaching
  16. */
  17. function cleanupOutdatedCaches() {
  18. // See https://github.com/Microsoft/TypeScript/issues/28357#issuecomment-436484705
  19. self.addEventListener('activate', ((event) => {
  20. const cacheName = cacheNames.getPrecacheName();
  21. event.waitUntil(deleteOutdatedCaches(cacheName).then((cachesDeleted) => {
  22. if (process.env.NODE_ENV !== 'production') {
  23. if (cachesDeleted.length > 0) {
  24. logger.log(`The following out-of-date precaches were cleaned up ` +
  25. `automatically:`, cachesDeleted);
  26. }
  27. }
  28. }));
  29. }));
  30. }
  31. export { cleanupOutdatedCaches };