-
-
Save antenando/821f96c0fff4e18211e0843f3f83a9b1 to your computer and use it in GitHub Desktop.
Print service worker cache sizes and overall bytes cached.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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 buffer = await response.arrayBuffer(); | |
total += buffer.byteLength; | |
cacheSize += buffer.byteLength; | |
})); | |
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