0

the code below checks whether a url is loaded and then logs to the console. I would like to know if there is simple, clean method to check if a page is loaded from bfcache or http cache? Firefox documentation states that the load event should not be triggered if I go from URL A to B and then hit the back button to URL A, but this is not my experience, both load and PageShow is logged regardless, does anyone know why?

var tabs = require("sdk/tabs");

function onOpen(tab) {
  tab.on("pageshow", logPageShow);
  tab.on("load", logLoading);
}

function logPageShow(tab) {
  console.log(tab.url + " -- loaded (maybe from bfcache?) ");
}

function logLoading(tab) {
  console.log(tab.url + " -- loaded (not from bfcache) ");
}

tabs.on('open', onOpen);
tiajuana6
  • 27
  • 7
  • If you are going to make a statement about the documentation stating something, please provide a link to the documentation where such is stated. – Makyen Jul 25 '16 at 14:40

1 Answers1

1

I am not sure whether there is any purposeful API for that but a workaround that came to mind is to check value of the performance.timing.responseEnd - performance.timing.requestStart. If it is <= 5 then most likely it is HTTP or back-forward cache. Otherwise, it is a download from the web.

A way to recognize a return to the page through a back button instead of opening up a clean URL is to use history API. For example:

// on page load
var hasCameBack = window.history && window.history.state && window.history.state.customFlag;
if (!hasComeBack) {
    // most likely, user has come by following a hyperlink or entering
    // a URL into browser's address bar. 
    // we flag the page's state so that a back/forward navigation
    // would reveal that on a comeback-kind of visist.
    if (window.history) {
        window.history.replaceState({ customFlag: true }, null, null);
    }
}
else {
    // handle the comeback visit situation
}

See also Manipulating the browser history article at MDN.

Andrew Sklyarevsky
  • 2,095
  • 14
  • 17