Skip to content

Instantly share code, notes, and snippets.

@marvindanig
Forked from ebidel/sw_caching_size.js
Created December 16, 2017 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marvindanig/34bb02e05b57ebf979077fe91fcd1159 to your computer and use it in GitHub Desktop.
Save marvindanig/34bb02e05b57ebf979077fe91fcd1159 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 size of each cache in the Cache Storage API and the overall bytes cached.
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.size();
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