PostHog / posthog-js

Send usage data from your web app or site to PostHog, with autocapture.
https://posthog.com/docs/libraries/js
Other
278 stars 114 forks source link

fix: no keep alive for large payloads #1416

Closed pauldambra closed 2 weeks ago

pauldambra commented 2 weeks ago

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)

vercel[bot] commented 2 weeks ago

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
github-actions[bot] commented 2 weeks ago

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