I need to save my session token in localStorage
in order to get it back when user reloads the page:
window.addEventListener('beforeunload', this.savetoken.bind(this));
window.addEventListener('unload', this.savetoken.bind(this));
function async savetoken() {
const token_ = await this.getToken();
localStorage.setItem('sbk:token', token_);
console.log('token saved in storage!!!', token_);
}
In desktop Chrome this work's perfectly, but in mobile versione (Android Chrome 85) the unload/beforeunload event doesn't get fired when closing browser or tab, it works only when refreshing the page.
Is there a way to get around this?
As https://developer.chrome.com/blog/page-lifecycle-api/#the-unload-event says
You can try
visibilitychange
and/orpagehide
events to save data. And as a fallback you can save data periodically, e.g. once in a minute viasetInterval()
.