Skip to content
This repository has been archived by the owner on Jun 28, 2024. It is now read-only.

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

Closed
philipwalton opened this issue Aug 4, 2018 · 0 comments
Assignees

Comments

@philipwalton
Copy link
Contributor

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 subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant