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

feat: Add useBeacon config for visibilitychange dispatch behavior. #194

Merged
merged 2 commits into from
Aug 9, 2022

Conversation

qhanam
Copy link
Member

@qhanam qhanam commented Aug 8, 2022

When the page transitions to a hidden state and fires the visibilitychange event, the page may be in the process of unloading, so the web client flushes it's event cache. In this scenario, the approach recommended by MDN is to use sendBeacon so that the request will complete after the page is unloaded. However, ad blockers prevent sendBeacon from functioning, so this request may not be sent.

We therefore have two bad options:

  1. Use sendBeacon and accept missing data when ad blockers are used and the page loses visibility
  2. Use fetch and accept missing data when the page is unloaded before fetch completes
    A third option is to send both, however this would increase bandwidch and require deduping server side.

This change adds a config option that controls whether fetch or sendBeacon is used in this scenario, so that application owners can select the behavior that best fits their application.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

// case. However, ad-blockers prevent sendBeacon from functioning.
// We therefore have two bad options:
//
// (1) Use sendBeacon and accept missing data when ad blockers are
Copy link
Member

Choose a reason for hiding this comment

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

question: What does accept missing data mean?

);
document.addEventListener('pagehide', this.dispatchBeaconFailSilent);
Copy link
Member

Choose a reason for hiding this comment

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

question: Are we removing this since visibilitychange implies pagehide?

Copy link
Member

Choose a reason for hiding this comment

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

If so, should we add a unit test case to verify pagehide is handled properly as intended?

Copy link
Member Author

Choose a reason for hiding this comment

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

That was the idea, although as I look at the compatibility tables, pagehide is supported in older browser versions which the web client technically supports, so perhaps we should keep the event listener for pagehide.

@qhanam qhanam merged commit 00ef55f into aws-observability:main Aug 9, 2022
@ps863 ps863 mentioned this pull request Jan 13, 2023
@qhanam qhanam deleted the visibilitychange-fetch branch May 26, 2023 21:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants