Skip to content

fix(browser): Defer sending session envelope until browser is idle#21844

Merged
Lms24 merged 6 commits into
developfrom
lms/fix-browser-defer-session-send
Jun 30, 2026
Merged

fix(browser): Defer sending session envelope until browser is idle#21844
Lms24 merged 6 commits into
developfrom
lms/fix-browser-defer-session-send

Conversation

@Lms24

@Lms24 Lms24 commented Jun 29, 2026

Copy link
Copy Markdown
Member

Uses the whenIdleOrHidden callback to send the initial session envelope when the browser has some idle time or at the latest, when the page is hidden. For old Safari versions, we fall back to setTimeout but otherwise we use requestIdleCallback (both within whenIdleOrHidden).

This should improve initial SDK initialization performance as well as reduce implicitly caused overhead (like increased LCP), due to the eagerly sent network request.

This should be a fairly safe change to make. I don't think we risk loosing a lot of sessions with it, given RIC should fire relatively soon. At the same time, I'm curious how much it improves lighthouse scores (which we can check easiest after merging).

closes #21817

@Lms24 Lms24 self-assigned this Jun 29, 2026
@github-actions

github-actions Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 27.62 kB +0.52% +142 B 🔺
@sentry/browser - with treeshaking flags 26.05 kB +0.56% +145 B 🔺
@sentry/browser (incl. Tracing) 46.07 kB +0.02% +6 B 🔺
@sentry/browser (incl. Tracing + Span Streaming) 47.82 kB +0.02% +5 B 🔺
@sentry/browser (incl. Tracing, Profiling) 50.84 kB +0.01% +5 B 🔺
@sentry/browser (incl. Tracing, Replay) 85.31 kB -0.01% -5 B 🔽
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 74.91 kB +0.01% +5 B 🔺
@sentry/browser (incl. Tracing, Replay with Canvas) 89.99 kB -0.02% -13 B 🔽
@sentry/browser (incl. Tracing, Replay, Feedback) 102.67 kB +0.01% +7 B 🔺
@sentry/browser (incl. Feedback) 44.8 kB +0.31% +137 B 🔺
@sentry/browser (incl. sendFeedback) 32.42 kB +0.49% +157 B 🔺
@sentry/browser (incl. FeedbackAsync) 37.55 kB +0.4% +147 B 🔺
@sentry/browser (incl. Metrics) 28.68 kB +0.5% +141 B 🔺
@sentry/browser (incl. Logs) 28.93 kB +0.52% +149 B 🔺
@sentry/browser (incl. Metrics & Logs) 29.61 kB +0.5% +145 B 🔺
@sentry/react 29.41 kB +0.48% +139 B 🔺
@sentry/react (incl. Tracing) 48.38 kB +0.03% +10 B 🔺
@sentry/vue 32.85 kB +0.45% +146 B 🔺
@sentry/vue (incl. Tracing) 47.93 kB -0.01% -4 B 🔽
@sentry/svelte 27.64 kB +0.53% +145 B 🔺
CDN Bundle 30.02 kB +0.47% +138 B 🔺
CDN Bundle (incl. Tracing) 48.02 kB +0.04% +16 B 🔺
CDN Bundle (incl. Logs, Metrics) 31.58 kB +0.46% +142 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics) 49.35 kB +0.04% +15 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics) 70.79 kB +0.03% +15 B 🔺
CDN Bundle (incl. Tracing, Replay) 85.51 kB +0.02% +14 B 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 86.79 kB +0.02% +14 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) 91.32 kB +0.02% +13 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 92.56 kB +0.02% +15 B 🔺
CDN Bundle - uncompressed 89.42 kB +0.53% +470 B 🔺
CDN Bundle (incl. Tracing) - uncompressed 145.35 kB +0.03% +37 B 🔺
CDN Bundle (incl. Logs, Metrics) - uncompressed 94.12 kB +0.51% +470 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 149.32 kB +0.03% +37 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 218.66 kB +0.02% +37 B 🔺
CDN Bundle (incl. Tracing, Replay) - uncompressed 264.36 kB +0.02% +37 B 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 268.32 kB +0.02% +37 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 278.06 kB +0.02% +37 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 282.01 kB +0.02% +37 B 🔺
@sentry/nextjs (client) 50.76 kB +0.02% +9 B 🔺
@sentry/sveltekit (client) 46.46 kB +0.01% +3 B 🔺
@sentry/core/server 77.75 kB - -
@sentry/core/browser 64.06 kB - -
@sentry/node-core 61.47 kB +0.01% +1 B 🔺
@sentry/node 122.79 kB +0.01% +1 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 69.95 kB - -
@sentry/node/light 50.45 kB - -
@sentry/node - without tracing 73.2 kB - -
@sentry/aws-serverless 84.09 kB - -
@sentry/cloudflare (withSentry) - minified 180.62 kB - -
@sentry/cloudflare (withSentry) 446.93 kB - -

View base workflow run

@Lms24 Lms24 marked this pull request as ready for review June 29, 2026 12:48
@Lms24 Lms24 requested a review from a team as a code owner June 29, 2026 12:48
@Lms24 Lms24 requested review from logaretm and mydea and removed request for a team June 29, 2026 12:48
@Lms24 Lms24 changed the title fix(browser): Defer browser session envelope until browser is idle fix(browser): Defer sending browser session envelope until browser is idle Jun 29, 2026
@Lms24 Lms24 changed the title fix(browser): Defer sending browser session envelope until browser is idle fix(browser): Defer sending session envelope until browser is idle Jun 29, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 9de6194. Configure here.

Comment thread packages/browser/src/integrations/browsersession.ts
Comment thread packages/browser/src/integrations/browsersession.ts Outdated
@Lms24 Lms24 requested a review from a team as a code owner June 29, 2026 16:35
@Lms24 Lms24 requested review from chargome and s1gr1d and removed request for a team June 29, 2026 16:35
Lms24 and others added 5 commits June 29, 2026 18:38
When using the default `lifecycle: 'route'`, a navigation that happens before
the deferred initial `whenIdleOrHidden` callback fires already starts and sends
a new session. The deferred callback then re-sent that same (current) session,
producing a duplicate envelope. Guard the deferred capture so it skips once a
session has been sent, and mark the session as sent in the navigation handler.

Co-Authored-By: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Replace the inline `import()` type annotation in the browserSession unit test
with a top-level type import to satisfy `consistent-type-imports`.

Reorder the expected logs in the debug integration test: now that the initial
session capture is deferred, the "Discarded session" warning is emitted after
the `BrowserSession` integration is installed rather than before it.

Co-Authored-By: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
The deferred browser session capture schedules a `whenIdleOrHidden` timer that
calls `WINDOW.removeEventListener` when it fires. These nextjs tests stub a fake
window with `addEventListener` but not `removeEventListener`, so the deferred
timer threw an unhandled `TypeError` after the test. Stub and restore
`removeEventListener` alongside `addEventListener`.

Co-Authored-By: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
@Lms24 Lms24 force-pushed the lms/fix-browser-defer-session-send branch from 87bc0d0 to dadb1bb Compare June 29, 2026 16:38
let originalDocument: unknown;
let originalLocation: unknown;
let originalAddEventListener: unknown;
let originalRemoveEventListener: unknown;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

is this related to this change?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yes, it needs to be pulled in because whenIdleOrHidden registers and removes event listeners

@Lms24 Lms24 merged commit 9d2ed38 into develop Jun 30, 2026
289 checks passed
@Lms24 Lms24 deleted the lms/fix-browser-defer-session-send branch June 30, 2026 08:44
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.

Browser Session envelope is sent directly at SDK init time, causing SDK overhead increases

3 participants