Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Use visibility API instead of beforeunload #158

Merged
merged 9 commits into from
Jan 10, 2019

Conversation

ksheedlo
Copy link
Contributor

Using the beforeunload event prevents browsers from caching the page
in the page navigation cache:

https://developers.google.com/web/updates/2018/07/page-lifecycle-api#the-beforeunload-event

Page Visibility is a better alternative. This API is widely supported
except for old Android, where it's prefixed on 4.4 and unavailable on
lower versions. I'm happy to add fallbacks if any reviewers feel they're
necessary.

Using the `beforeunload` event prevents browsers from caching the page
in the page navigation cache:

https://developers.google.com/web/updates/2018/07/page-lifecycle-api#the-beforeunload-event

Page Visibility is a better alternative. This API is widely supported
except for old Android, where it's prefixed on 4.4 and unavailable on
lower versions. I'm happy to add fallbacks if any reviewers feel they're
necessary.
@leyanlo
Copy link

leyanlo commented Dec 19, 2018

+1. Would also like to avoid beforeunload because we're sometimes seeing alerts on Safari and IE11 when we try to reload or close the tab:

beforeunload-alert-safari

beforeunload-alert-ie11

@codecov
Copy link

codecov bot commented Dec 19, 2018

Codecov Report

Merging #158 into master will decrease coverage by 1.55%.
The diff coverage is 40%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #158      +/-   ##
==========================================
- Coverage   84.37%   82.82%   -1.56%     
==========================================
  Files           7        7              
  Lines         160      163       +3     
  Branches       24       25       +1     
==========================================
  Hits          135      135              
- Misses         18       21       +3     
  Partials        7        7
Impacted Files Coverage Δ
src/browser.js 80.55% <40%> (-7.33%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 30e27e5...917fa82. Read the comment docs.

@codecov
Copy link

codecov bot commented Dec 19, 2018

Codecov Report

Merging #158 into master will increase coverage by 1.15%.
The diff coverage is 83.33%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #158      +/-   ##
==========================================
+ Coverage   84.37%   85.53%   +1.15%     
==========================================
  Files           7        7              
  Lines         160      159       -1     
  Branches       24       25       +1     
==========================================
+ Hits          135      136       +1     
+ Misses         18       17       -1     
+ Partials        7        6       -1
Impacted Files Coverage Δ
src/browser.js 93.75% <83.33%> (+5.87%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 30e27e5...b50a07b. Read the comment docs.

Copy link

@leyanlo leyanlo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also could we update the README about flushing the queue on document.hidden now?

src/browser.js Outdated
@@ -50,6 +50,11 @@ class UniversalEmitter extends Emitter {
from(): UniversalEmitter {
return this;
}
flushBeforeTerminated = () => {
if (document.hidden) {
this.flushInternal();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this.flushInternal() vs this.flush()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bind is slow and we already have the this context

@CLAassistant
Copy link

CLAassistant commented Dec 21, 2018

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

@KevinGrandon KevinGrandon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this makes sense to me, and would be good to fix the caching issues. My only question, visibilitychange seems to not fire during a page navigation, where as beforeunload does. Are we now counting on the interval to handle events when the user navigates away?

@leyanlo
Copy link

leyanlo commented Jan 2, 2019

visibilitychange is supposed to fire on navigation (according to w3c), but there were some bugs filed against Chrome, Safari, and Edge when that wasn't the case:

Chrome should be fixed as of M56 (May 2017):
https://bugs.chromium.org/p/chromium/issues/detail?id=554834

Safari WebKit still has an open issue:
https://bugs.webkit.org/show_bug.cgi?id=151234

Unclear status for Edge, but was filed internally:
w3c/page-visibility#18 (comment)

This would definitely be a tradeoff and reduce the logging for IE/Safari to that only captured by the interval until they properly implement visibilitychange. But right now, IE/Safari pop up the Are you sure you want to leave this page? alert on navigation which I feel is a worse experience and worth the tradeoff to remove. Also the caching benefit is a nice bonus..

@KevinGrandon KevinGrandon added the CI label Jan 8, 2019
Copy link
Contributor

@KevinGrandon KevinGrandon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Android 4.4 and lower seem to hold around ~10% of android browser market share according to: https://fossbytes.com/most-popular-android-versions-always-updated/

We could add a fallback, but the market share does seem to be dropping quickly enough that I'm not sure the extra cruft is worth it. Thank you for the PR and iteration on testing.

@KevinGrandon
Copy link
Contributor

!merge

@old-fusion-bot old-fusion-bot bot merged commit b4c7b85 into fusionjs:master Jan 10, 2019
This was referenced Jan 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants