Created
March 25, 2020 14:00
-
-
Save hawx1993/0ff469835d19ec037de462270fcbfcf8 to your computer and use it in GitHub Desktop.
app.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const SUBSTRING_TO_FIND = '-precache-'; | |
| export default async function deleteWorkboxPreCaches( | |
| filename: string, | |
| substringToFind: string = SUBSTRING_TO_FIND, | |
| ) { | |
| const cacheNames = await caches.keys(); | |
| const cacheNamesToDelete = cacheNames.filter(cacheName => { | |
| return cacheName.includes(substringToFind); | |
| }); | |
| await Promise.all( | |
| cacheNamesToDelete.map(cacheName => | |
| (async () => { | |
| let cacheHasBeDeleted = false; | |
| if (filename) { | |
| const cache = await caches.open(cacheName); | |
| const cacheKeys = await cache.keys(); | |
| const deleteCaches: Promise<boolean>[] = []; | |
| cacheKeys.forEach(key => { | |
| if (key.url && key.url.includes(filename)) { | |
| cacheHasBeDeleted = true; | |
| deleteCaches.push(cache.delete(key)); | |
| } | |
| }); | |
| await Promise.all(deleteCaches); | |
| } | |
| if (!cacheHasBeDeleted) { | |
| await caches.delete(cacheName); | |
| } | |
| })(), | |
| ), | |
| ); | |
| return cacheNamesToDelete; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment