Skip to content

fix: call sendBeacon synchronously #631

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

Merged
merged 1 commit into from
Mar 27, 2025

Conversation

williazz
Copy link
Contributor

@williazz williazz commented Mar 27, 2025

Summary

SendBeacon() is the industry standard way to report data AFTER the user closed the page. This thing has never worked for us before because we called it asynchronously inside a promise, meaning the browser rarely ever received the job.

In addition, this API is becoming increasingly important, because it is becoming a popular pattern to record events locally without ever publishing them to data analytical services (e.g. RUM) until the last possible moment.

To fix it, you need to call it synchronously so the browser can queue the job.

Reading

  1. https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon
  2. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Execution_model#job_queue_and_event_loop

Testing

This is fairly hard do an integration test for. There is already coverage on page hide, but not for closing the page.

I manually verified this by recording INP event as a candidate but made sure PutRumEvents was never called by the browser.

After closing the page, I saw that my event was successfully ingested. (Tested this many times).

Screenshot 2025-03-27 at 11 44 51 AM

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

@williazz williazz requested review from ps863 and limhjgrace March 27, 2025 18:47
@williazz williazz merged commit 26aad1c into aws-observability:main Mar 27, 2025
3 checks passed
williazz added a commit to williazz/aws-rum-web that referenced this pull request Mar 29, 2025
williazz added a commit to williazz/aws-rum-web that referenced this pull request Mar 31, 2025
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.

3 participants