Skip to content

Instantly share code, notes, and snippets.

@ebidel
Last active November 16, 2022 11:31
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ebidel/188a513b1cd5e77d4d1453a4b6d060b0 to your computer and use it in GitHub Desktop.
Save ebidel/188a513b1cd5e77d4d1453a4b6d060b0 to your computer and use it in GitHub Desktop.
Print service worker cache sizes and overall bytes cached.
/**
* @author ebidel@ (Eric Bidelman)
* License Apache-2.0
*/
// Prints the bytes cached by service worker. Breaks out each cache
// overall in-memory bytes used by the Cache Storage API for the site.
async function getCacheStoragesAssetTotalSize() {
// Note: opaque (i.e. cross-domain, without CORS) responses in the cache will return a size of 0.
const cacheNames = await caches.keys();
let total = 0;
const sizePromises = cacheNames.map(async cacheName => {
const cache = await caches.open(cacheName);
const keys = await cache.keys();
let cacheSize = 0;
await Promise.all(keys.map(async key => {
const response = await cache.match(key);
const blob = await response.blob();
total += blob.size;
cacheSize += blob.size;
}));
console.log(`Cache ${cacheName}: ${cacheSize} bytes`);
});
await Promise.all(sizePromises);
return `Total Cache Storage: ${total} bytes`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment