Instantly share code, notes, and snippets.

Embed
What would you like to do?
Determine if resource was cached in browser using RT
function wasServedFromBrowserCache(url) {
const {transferSize, decodedBodySize, duration} = performance.getEntriesByName(url)[0]
// if we transferred bytes, it must not be a cache hit
// (will return false for 304 Not Modified)
if (transferSize > 0) return false;
// if the body size is non-zero, it must mean this is a
// ResourceTiming2 browser, this was same-origin or TAO,
// and transferSize was 0, so it was in the cache
if (decodedBodySize > 0) return true;
// fall back to duration checking (non-RT2 or cross-origin)
return duration < 30;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment