Skip to content

Instantly share code, notes, and snippets.

@hawx1993
Created March 25, 2020 14:00
Show Gist options
  • Select an option

  • Save hawx1993/0ff469835d19ec037de462270fcbfcf8 to your computer and use it in GitHub Desktop.

Select an option

Save hawx1993/0ff469835d19ec037de462270fcbfcf8 to your computer and use it in GitHub Desktop.
app.ts
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