Skip to content

fix: fall back to fetch when sendBeacon payload exceeds 64 KB limit#777

Open
TrevorBurnham wants to merge 1 commit intoaws-observability:mainfrom
TrevorBurnham:beacon-length-limit
Open

fix: fall back to fetch when sendBeacon payload exceeds 64 KB limit#777
TrevorBurnham wants to merge 1 commit intoaws-observability:mainfrom
TrevorBurnham:beacon-length-limit

Conversation

@TrevorBurnham
Copy link

Resolves #761

Problem

navigator.sendBeacon has a payload size limit of approximately 64 KiB on most modern browsers. When the serialized event batch exceeds this limit, sendBeacon silently returns false, causing the request to be dropped. While the existing code does catch beacon failures and fall back to fetch, the failure only occurs after the (potentially expensive) request presigning step has already completed — wasting time during a critical window like page unload.

Solution

Add a proactive payload size check in DataPlaneClient.sendBeacon that rejects immediately when the serialized body exceeds 64,000 bytes (a conservative threshold below the 64 KiB browser limit to avoid edge-case variance across browsers). This lets the existing .catch() fallback in Dispatch.dispatchBeacon and flushSync route the request through fetch without wasting time on presigning a request that would fail anyway.

The size is measured with new Blob([serializedRequest]).size to correctly account for multi-byte characters.

Changes

  • src/dispatch/DataPlaneClient.ts — Added BEACON_PAYLOAD_LIMIT constant (64,000 bytes) and a size guard at the top of sendBeacon that rejects before presigning when the payload is too large.
  • src/dispatch/__tests__/DataPlaneClient.test.ts — Added two tests: one verifying that oversized payloads are rejected (and the beacon handler is never called), and one confirming normal-sized payloads still go through beacon as before.

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

navigator.sendBeacon silently fails when the payload exceeds ~65 KB.
Add a proactive size check in DataPlaneClient.sendBeacon that rejects
before presigning when the serialized body exceeds 64,000 bytes,
allowing the existing catch handler to fall back to fetch.

Resolves aws-observability#761
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.

[Bug]: SendBeacon 65 KB limit

1 participant