UNPKG

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