Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle cases where Safari doesn't dispatch the pagehide or visibilitychange events #2

philipwalton opened this issue Aug 4, 2018 · 0 comments


Copy link

philipwalton commented Aug 4, 2018

On desktop Safari, when closing a tab by clicking the (X) on the tab UI itself, no visibilitychange or pagehide event is dispatched, so the page becomes terminated but the reported state never transitions away from active.

In order to at least provide a guarantee of hidden, we should listen for the beforeunload event and report a change to hidden if no event handlers set a returnValue or call preventDefault().

The following code achieves this:

addEventListener('beforeunload', (evt) => {
  setTimeout(() => {
    if (!(evt.defaultPrevented || evt.returnValue)) {
      // The state is now going to either FROZEN or TERMINATED.
  }, 0);

But since adding an unconditional beforeunloaded handler will break the page navigation cache in Firefox, we should only add this logic in Safari.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet

No branches or pull requests

1 participant