We would like to know from where the initial html document itself was served, either directly from the browser cache, our cdn cache or from origin. The later are easy (since our cdn adds server-timing to indicate a cache hit/miss), but figuring out if the document was served directly from the browser turned out more difficult.
I know about developer tools/network tab and how Chrome for example can show this information, but this question is specifically about any JavaScript API that can be used to detect and report it to us.
So far I've tried three things:
var isCached = performance.getEntriesByType("navigation")[0].transferSize === 0;
from this answer, but today this seems to report the previous transferSize. When I try the same with the latest Chrome, I never gettransferSize === 0
, even when the devTools show me that it was cached. Possibly it only works for other resources, but not the html document itself.var isCached = window.performance.navigation.type === 2
according to this answer gets the navigation type (in this case backward/forward), which is not always a true indicator of the document being cached. E.g. clicking a link is of typenavigation
and can also be cached.Storing the timestamp in the document itself as suggested here on the server and comparing it does not work either, especially since we are using a cdn that has its own cache. We wouldn't be able to differentiate between a cached version from cdn or the browser.