Skip to content

Instantly share code, notes, and snippets.

@cvazac
Created December 21, 2018 20:18
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 cvazac/db99a3fcfd502d3965d21cac7fb99143 to your computer and use it in GitHub Desktop.
Save cvazac/db99a3fcfd502d3965d21cac7fb99143 to your computer and use it in GitHub Desktop.
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