0

var element = document.getElementById("temp");
if (sessionStorage.getItem("hello") === "world") {
  element.innerHTML = "sessionStorage is available";
} else {
  sessionStorage.setItem("hello", "world");
  element.innerHTML = "sessionStorage is NOT available";
}
<span id="temp"></span>

Currently this sessionStorage survives refreshes but doesn't survive tab/browser closure (as expected). However, it also survives "hard refreshes" (getting a fresh version of the page with all cache deleted; Ctrl+Shift+R or Cmd+Shift+R).

However I want it to be deleted after a hard refresh too. It should only survive normal refreshes.

How can I do that with sessionStorage? And if it is not possible, what are the possible alternatives?

joulev
  • 19
  • 3
  • two years ago, someone asked what is at the core of your question (detecting hard refresh, what you do if you detect a hard refresh is inconsequential) - https://stackoverflow.com/questions/54040182/detect-a-hard-reload-with-javascript – Bravo Aug 11 '21 at 02:49
  • 1
    there's a comment about "performance counters" - an interesting one is `performance.getEntriesByType("navigation")[0].domainLookupEnd` - usually seems to be `1` or `2` on a soft reload and 50+ on a hard one – Bravo Aug 11 '21 at 02:57
  • @Bravo Thanks for the link, for some reasons I couldn't find it on Google. Thanks for the performance counter too, I will have a look at it! – joulev Aug 11 '21 at 03:05
  • 1
    though I've seen hard refresh result in domainLookupEnd as low as 14 ... so, it may not be that useful ... I'd suggest looking into some other values in `performance.getEntriesByType("navigation")[0]` as well – Bravo Aug 11 '21 at 03:06

0 Answers0