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

fix: no keep alive for large payloads #1416

Merged
merged 5 commits into from
Sep 16, 2024
Merged

fix: no keep alive for large payloads #1416

merged 5 commits into from
Sep 16, 2024

Conversation

pauldambra
Copy link
Member

@pauldambra pauldambra commented Sep 13, 2024

we set keepalive to true for any request that is a POST

the fetch spec at https://fetch.spec.whatwg.org/#http-network-or-cache-fetch says

If contentLength is non-null and httpRequest’s keepalive is true, then:
If the sum of contentLength and inflightKeepaliveBytes is greater than 64 kibibytes, then return a network error.

read more at e.g. https://javascript.info/fetch-api#:~:text=But%20the%20keepalive%20option%20tells,for%20keepalive%20requests%20is%2064KB.

it is likely that a replay payload would be >64kb compressed

not all browsers obey this spec equally (of course) so it won't always fail, so it's not broken consistently enough to stand out.

but locally I can see network requests failing for this reason

fetch keepalive is intended for situations including those like analytics (👋 posthog) where you might want to allow a request to finish even though the page is navigating so we don't want to turn it off completely

lets estimate body size and set keepalive when its safe (which it should be for most analytics events)

Copy link

vercel bot commented Sep 13, 2024

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

Name Status Preview Updated (UTC)
posthog-js ✅ Ready (Inspect) Visit Preview Sep 15, 2024 2:39pm

Copy link

github-actions bot commented Sep 13, 2024

Size Change: +516 B (+0.04%)

Total Size: 1.21 MB

Filename Size Change
dist/array.full.js 346 kB +129 B (+0.04%)
dist/array.js 162 kB +129 B (+0.08%)
dist/main.js 163 kB +129 B (+0.08%)
dist/module.js 162 kB +129 B (+0.08%)
ℹ️ View Unchanged
Filename Size
dist/exception-autocapture.js 10.4 kB
dist/recorder-v2.js 110 kB
dist/recorder.js 111 kB
dist/surveys-preview.js 59.8 kB
dist/surveys.js 66 kB
dist/tracing-headers.js 8.26 kB
dist/web-vitals.js 10.3 kB

compressed-size-action

// so let's get the best of both worlds and only set keepalive for POST requests
// where the body is less than 64kb
// NB this is fetch keepalive and not http keepalive
keepalive: options.method === 'POST' && (estimatedSize || 0) < 64 * 1024,
Copy link
Contributor

Choose a reason for hiding this comment

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

Could abstract this but doesn't seem necessary given the comment explains a lot

Suggested change
keepalive: options.method === 'POST' && (estimatedSize || 0) < 64 * 1024,
keepalive: options.method === 'POST' && (estimatedSize || 0) < MAX_KEEPALIVE_KB,

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah, i'm always torn by the tension between "clarify intent" and "have fewest parts"
if you make it a const it's arguably clearer but i need to look at multiple places to be sure i understand

@pauldambra pauldambra added the bump patch Bump patch version when this PR gets merged label Sep 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bump patch Bump patch version when this PR gets merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants