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 sendBeacon request parameters hook #555

Merged
merged 5 commits into from
Mar 2, 2024
Merged

Conversation

hoebbelsB
Copy link
Contributor

@hoebbelsB hoebbelsB commented Feb 15, 2024

What is it?

  • Feature / enhancement
  • Bug
  • Docs / tests

Description

This change introduces a new hook called resolveSendBeaconRequestParameters on the PartytownConfig. This enables consumers to modify a subset of the RequestInit parameters being used by the fetch request that polyfills the navigator.sendBeacon API in the worker context, e.g. setting keepalive: false.

Alternative solution

An alternative solution would be to just set keepalive: false as default. But as I am not aware of the reasoning behind your defaults, I thought it's a good idea to make it user configurable.

Use cases and why

The current implementation of the sendBeacon has static values for the RequestInit parameters it uses to initialize the post request. The goal is to make those parameters being user configurable.
I am not 100% sure why you have chosen to go with those default values, but I've notice that espcially keepalive: true is causing troubles in combination with google analytics (analytics.js).

I've already pointed out the behavior it causes in this issue #492, but at that point I didn't know the root cause is keepalive: true.

Short description of what happens with some visuals.

The application starts behaving normal, the first couple of requests sent via sendBeacon are actually fine and not producing any errors. Until a certain amount of events was sent, most of the following requests are simply in pending state forever.

image

The "failing" requests are also producing errors like crazy

image

Lastly, I've figured out that also the "good" requests are having issues, as they seem to actually never finish until they run into some timeout.

image

After manually setting keepalive: false and override the script locally, the application suddenly recovers and all consequent requests are perfectly fine. It looks like the never finishing requests are filling some queue which causes consequent requests to be stalled.

image

Checklist:

  • My code follows the developer guidelines of this project
  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • Added new tests to cover the fix / functionality

This change introduces a new hook called `resolveSendBeaconRequestParameters`. This enables consumers to modify a subset of the RequestInit parameters being used by the fetch request that polyfills the navigator.sendBeacon API in the worker context, e.g. setting keepalive: false
Copy link

vercel bot commented Feb 15, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
partytown ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 26, 2024 4:43pm

@gioboa
Copy link
Member

gioboa commented Feb 17, 2024

Thank you @hoebbelsB
Can you add a specific test for this new configuration please?

@hoebbelsB
Copy link
Contributor Author

I just went on vacation after opening the pr. Will update with tests when I'm back :)

@gioboa
Copy link
Member

gioboa commented Feb 24, 2024

Thanks for your help, have a nice vacation 🏝️

@hoebbelsB
Copy link
Contributor Author

hey @gioboa just added the tests, please check if this is sufficient :)

Copy link
Member

@gioboa gioboa left a comment

Choose a reason for hiding this comment

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

@hoebbelsB the core team verified it, that's a great piece of code.
Thanks for your help.

@gioboa gioboa merged commit e48d5ba into QwikDev:main Mar 2, 2024
11 checks passed
@replete
Copy link

replete commented Apr 23, 2024

Amazing work, thanks so much for this PR!

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.

4 participants