Created
December 21, 2018 20:18
-
-
Save cvazac/db99a3fcfd502d3965d21cac7fb99143 to your computer and use it in GitHub Desktop.
Determine if resource was cached in browser using RT
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
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