Is there a way to check if the page was loaded from browser cache?
Asked Answered
Y

2

9

The issue the question originates from is the following. I'm using TiddlyWiki (Classic) SPA on my Android device and usually use it with FireFox and its TiddlyFox extension for saving. For some reasons I'd like to be able to work with (and save) my TWs using other browsers, so I'm testing it with a PHP back-end (my fork of MicroTiddlyServer, but its code is not important here, I believe, + this PHP server).

In my tests I've noticed that although saving works fine, sometimes (at least when the PHP server is unloaded from memory due to this ugly Android "optimization" which seems to be not configurable) a TW is loaded from cache and because of that it is loaded as it was before the latest saving, not after.

So, what I want is to detect if the page was loaded in an ordinary way or from browser cache. Is it possible to check this via JavaScript?

As a worse alternative I can inject a timestamp via MTS and check it in a TW on load, but I'd like to avoid this complication (which involves both front-end and back-end and adds more TW file manipulation).

Youlandayoulton answered 24/5, 2016 at 23:7 Comment(2)
It looks duplicated, have you checked #9870832 and #260543Heartburning
(I've ended up with adding appropriate headers to the PHP part so that the page is always served without cache)Youlandayoulton
A
1

With the new Resource Timing Level 2 spec you can use the transfer size property to check if the page is loaded from cache:

var isCached = performance.getEntriesByType("navigation")[0].transferSize === 0;

Spec: https://www.w3.org/TR/resource-timing/#dom-performanceresourcetiming-transfersize

Abjure answered 17/2, 2023 at 10:17 Comment(0)
H
0

If you use the remote debugger in chrome you can see the network requests and determine if you item is cached or not. Firefox seems to have a remote debugger as well.

Hygrostat answered 5/6, 2016 at 5:14 Comment(1)
Unfortunately, I'm looking for a way to do this via JavaScript (so that I can notify the user), preferrably without any additional action from back-end (I'm learning if this is possible)Youlandayoulton

© 2022 - 2024 — McMap. All rights reserved.