I simply want to save current state of my PWA in IndexedDB before leaving the page (when the user navigates to a new page, reloads the same page, or closes the tab/browser). But to my surprise, there is no reliable way to do this that works in all major browsers.
I've tested to capture window.onunload
, window.onbeforeunload
, window.onpagehide
, window.onblur
and document.onvisibilitychange
. I can cover almost all cases, but not page reload on iOS Safari.
There are plenty of posts about this problem on Stackoverflow. Is there still no solution?
window.addEventListener('unload', function () {
save();
});
window.addEventListener('beforeunload', function () {
save();
});
window.addEventListener(
'pagehide',
function () {
save();
},
false,
);
window.addEventListener('blur', function () {
save();
});
document.addEventListener('visibilitychange', function () {
save();
});