cleanupOutdatedCaches.mjs 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  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.mjs';
  8. import {logger} from 'workbox-core/_private/logger.mjs';
  9. import {deleteOutdatedCaches} from './utils/deleteOutdatedCaches.mjs';
  10. import './_version.mjs';
  11. /**
  12. * Adds an `activate` event listener which will clean up incompatible
  13. * precaches that were created by older versions of Workbox.
  14. *
  15. * @alias workbox.precaching.cleanupOutdatedCaches
  16. */
  17. export const cleanupOutdatedCaches = () => {
  18. addEventListener('activate', (event) => {
  19. const cacheName = cacheNames.getPrecacheName();
  20. event.waitUntil(deleteOutdatedCaches(cacheName).then((cachesDeleted) => {
  21. if (process.env.NODE_ENV !== 'production') {
  22. if (cachesDeleted.length > 0) {
  23. logger.log(`The following out-of-date precaches were cleaned up ` +
  24. `automatically:`, cachesDeleted);
  25. }
  26. }
  27. }));
  28. });
  29. };