Skip to content

Instantly share code, notes, and snippets.

@jeffposnick
Created October 11, 2019 19:12
Show Gist options
  • Save jeffposnick/9bc877a477031872ec8fb9851f81a526 to your computer and use it in GitHub Desktop.
Save jeffposnick/9bc877a477031872ec8fb9851f81a526 to your computer and use it in GitHub Desktop.
Snippet to get the total size of everything in the Cache Storage API for the current origin
async function totalCacheSize() {
let size = 0;
const cacheNames = await caches.keys();
for (const cacheName of cacheNames) {
const cache = await caches.open(cacheName);
const cachedRequests = await cache.keys();
for (const cachedRequest of cachedRequests) {
const cachedResponse = await cache.match(cachedRequest);
const responseBlob = await cachedResponse.blob();
size += responseBlob.size;
}
}
return totalContentSize;
}
// Can the be used in the JS console:
// totalContentSize().then(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment