Skip to content

Instantly share code, notes, and snippets.

@oliverturner
Forked from jeffposnick/totalCacheSize.js
Created October 11, 2019 19:46
Show Gist options
  • Save oliverturner/dbb6d6734d77ab75f8c6bb2a0716a326 to your computer and use it in GitHub Desktop.
Save oliverturner/dbb6d6734d77ab75f8c6bb2a0716a326 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