Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.