Last active
April 18, 2017 14:20
-
-
Save ithinkihaveacat/f7eecb609aed3b4c85385c6a13a18157 to your computer and use it in GitHub Desktop.
Simple functions for querying the SW cache
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
// Simple functions for querying the SW cache. | |
// Only approximate! (1) Only includes bodies. (2) Can't count opaque resources. | |
function cacheDump(c) { | |
return c.keys().then(a => { | |
return Promise.all(a.map(req => | |
c.match(req).then(res => res.clone().blob().then(b => [req.url, b.size])) | |
)).then(a => new Map(a)); | |
}); | |
} | |
function cacheSize(c) { | |
return c.keys().then(a => { | |
return Promise.all( | |
a.map(req => c.match(req).then(res => res.clone().blob().then(b => b.size))) | |
).then(a => a.reduce((acc, n) => acc + n, 0)); | |
}); | |
} | |
function cachesDump() { | |
return caches.keys().then(a => { | |
return Promise.all(a.map(n => { | |
return caches.open(n).then(c => cacheDump(c)).then(v => [n, v]); | |
})).then(a => new Map(a)); | |
}); | |
} | |
function cachesSize() { | |
return caches.keys().then(a => { | |
return Promise.all( | |
a.map(n => caches.open(n).then(c => cacheSize(c))) | |
).then(a => a.reduce((acc, n) => acc + n, 0)); | |
}); | |
} | |
function cachesStats() { | |
return caches.keys().then(a => { | |
return Promise.all( | |
a.map(n => caches.open(n).then(c => { | |
return Promise.all([cacheDump(c), cacheSize(c)]).then(v => { | |
return new Map([ | |
["entries", [...v[0].entries()].sort((a, b) => b[1] - a[1])], | |
["size", v[1]] | |
]); | |
}).then(v => [n, v]); | |
})) | |
).then(a => new Map(a)); | |
}); | |
} | |
cachesSize().then(console.log); // total size of all caches |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment