Konrad Jamrozik
Konrad Jamrozik

Reputation: 3586

How much time do I have to save data to local storage when browser visibility changes to hidden?

I want to save page data to local storage upon tab close or reload. I am saving data upon visibilitychange event to hidden. I've read this is the recommended way to do so per Page Lifecycle API and the article Don't lose user and app state, use Page Visibility.

Specifically, beforeunload is not recommended per its usage notes on MDN.

However, I observed that if I close the tab and spend too much time processing the data to be saved (as I am compressing it first, which takes over a second), it does not actually save.

The Page Lifecycle API states the following about Terminated state with comes after Hidden state:

and in-progress tasks may be killed if they run too long.

This documentation links to an article that mentions Event loop processing model, which in turn mentions computeDeadline but it is hard to understand.

How much time do I have to compress and save my data to local browser storage upon browser tab being closed, before the browser kills the processing?

Upvotes: 0

Views: 63

Answers (0)

Related Questions