Skip to content

Instantly share code, notes, and snippets.

@jeffposnick
Last active August 10, 2016 20:56
Show Gist options
  • Save jeffposnick/34bc77369c0e53a9d975cfdd76338ed8 to your computer and use it in GitHub Desktop.
Save jeffposnick/34bc77369c0e53a9d975cfdd76338ed8 to your computer and use it in GitHub Desktop.
Total size, in bytes, of all entries in the Cache Storage API
// Note that any opaque (i.e. cross-domain, without CORS) responses in the cache will return a size of 0.
caches.keys().then(cacheNames => {
let total = 0;
return Promise.all(
cacheNames.map(cacheName => {
// Change this to match the cache name filter you want.
if (!cacheName.includes('sw-precache')) {
return;
}
return caches.open(cacheName).then(cache => {
return cache.keys().then(keys => {
return Promise.all(
keys.map(key => {
return cache.match(key)
.then(response => response.arrayBuffer())
.then(buffer => total += buffer.byteLength);
})
);
});
});
})
).then(() => console.log(`Total bytes: ${total}`));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment