Currently piwik.js unconditionally adds a listener to the
beforeunload event. Unfortunately, this prevents "Back-Forward Cache"/"Page Cache"/"Fast History Navigation" on Firefox/Safari/Opera respectively.
beforeunload event is only used to trigger
unload plugins, would it be acceptable to defer registering the
beforeunload event listener until
addPlugin is called with an
unload plugin? That way plugins will still get the event, but Piwik won't disable the bfcache unless a plugin requires it.
Even if we completely removed the plugin support, we would still require the event listener. The wait loop in the before unload handler is an important piece of functionality, i.e., required for outlink tracking (where supported by the browser).
So, yes, it prevents bfcaching, but this is as designed/intended. The immediate workaround is to use Piwik's server-side logging capability. Removing/disabling the before unload event listener is a compat buster, so won't happen in the 2.0.x branch.
Can this be fixed, if so do you know how?
This requires further investigation to see how well supported it is across browsers.
Latest Firefox and Safari fire pageshow event before onload, and pagehide before unload.
It might be something like:
Add event listener for 'pagehide' Pagehide handler: - remove unload handler (if still registered) - call the unload handler directly
(Similarly for pageshow.)
Theoretically, this would allow the page to be cacheable by bfcache. Downsides: may introduce a delay for Back/Forward navigation. (Could mitigate this by keeping track of outstanding requests.)
Thanks Anthon. It sounds a bit complex so won't fix for now. Thanks for the discussion! If you have a patch please reopen