| [9af201e] | 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 | */
|
|---|
| 8 |
|
|---|
| 9 | import '../_version.js';
|
|---|
| 10 |
|
|---|
| 11 | // Give TypeScript the correct global.
|
|---|
| 12 | declare let self: ServiceWorkerGlobalScope;
|
|---|
| 13 |
|
|---|
| 14 | const SUBSTRING_TO_FIND = '-precache-';
|
|---|
| 15 |
|
|---|
| 16 | /**
|
|---|
| 17 | * Cleans up incompatible precaches that were created by older versions of
|
|---|
| 18 | * Workbox, by a service worker registered under the current scope.
|
|---|
| 19 | *
|
|---|
| 20 | * This is meant to be called as part of the `activate` event.
|
|---|
| 21 | *
|
|---|
| 22 | * This should be safe to use as long as you don't include `substringToFind`
|
|---|
| 23 | * (defaulting to `-precache-`) in your non-precache cache names.
|
|---|
| 24 | *
|
|---|
| 25 | * @param {string} currentPrecacheName The cache name currently in use for
|
|---|
| 26 | * precaching. This cache won't be deleted.
|
|---|
| 27 | * @param {string} [substringToFind='-precache-'] Cache names which include this
|
|---|
| 28 | * substring will be deleted (excluding `currentPrecacheName`).
|
|---|
| 29 | * @return {Array<string>} A list of all the cache names that were deleted.
|
|---|
| 30 | *
|
|---|
| 31 | * @private
|
|---|
| 32 | * @memberof workbox-precaching
|
|---|
| 33 | */
|
|---|
| 34 | const deleteOutdatedCaches = async (
|
|---|
| 35 | currentPrecacheName: string,
|
|---|
| 36 | substringToFind: string = SUBSTRING_TO_FIND,
|
|---|
| 37 | ): Promise<string[]> => {
|
|---|
| 38 | const cacheNames = await self.caches.keys();
|
|---|
| 39 |
|
|---|
| 40 | const cacheNamesToDelete = cacheNames.filter((cacheName) => {
|
|---|
| 41 | return (
|
|---|
| 42 | cacheName.includes(substringToFind) &&
|
|---|
| 43 | cacheName.includes(self.registration.scope) &&
|
|---|
| 44 | cacheName !== currentPrecacheName
|
|---|
| 45 | );
|
|---|
| 46 | });
|
|---|
| 47 |
|
|---|
| 48 | await Promise.all(
|
|---|
| 49 | cacheNamesToDelete.map((cacheName) => self.caches.delete(cacheName)),
|
|---|
| 50 | );
|
|---|
| 51 |
|
|---|
| 52 | return cacheNamesToDelete;
|
|---|
| 53 | };
|
|---|
| 54 |
|
|---|
| 55 | export {deleteOutdatedCaches};
|
|---|