Skip to content

fix(browser-utils): Ensure web vital client hooks unsubscribe correctly #17272

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

Merged
merged 1 commit into from
Aug 1, 2025

Conversation

Lms24
Copy link
Member

@Lms24 Lms24 commented Jul 31, 2025

So this was a fun one to track down 😅

In our standalone span web vitals code, we register multiple client hooks to listen e.g. for afterStartPageloadSpan events. This hook will only fire once (by design), so we want to unsbscribe from it afterwards.

Howerver, we register two callbacks (one for LCP, once for CLS). Because we used to unsubscribe synchronously from within the client hook callback, we synchronously removed the callback from the client's hooks array. This synchronous array mutation (shrinking) caused the second callback to no longer be executed.

This surfaced by the LCP span being sent but the CLS span not being sent, due to the CLS span's hook callback no longer being called.

This PR fixes this incorrect unsubscription by deferring the unsubscription calls to the next tick. This way, the array mutation no longer happens synchronously and all remaining callback hooks are invoked correctly.

If you're confused by this, rest assured, I was too 😅 Happy to explain better/in-person on request :D

closes https://linear.app/getsentry/issue/JS-811/investigate-missing-standalone-cls-spans-in-latest-sdk-versions

Copy link

linear bot commented Jul 31, 2025

Copy link
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 23.77 kB - -
@sentry/browser - with treeshaking flags 22.35 kB - -
@sentry/browser (incl. Tracing) 39.43 kB +0.06% +20 B 🔺
@sentry/browser (incl. Tracing, Replay) 77.52 kB +0.03% +19 B 🔺
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 67.39 kB +0.05% +30 B 🔺
@sentry/browser (incl. Tracing, Replay with Canvas) 82.22 kB +0.02% +16 B 🔺
@sentry/browser (incl. Tracing, Replay, Feedback) 94.35 kB +0.03% +26 B 🔺
@sentry/browser (incl. Feedback) 40.45 kB - -
@sentry/browser (incl. sendFeedback) 28.45 kB - -
@sentry/browser (incl. FeedbackAsync) 33.34 kB - -
@sentry/react 25.5 kB - -
@sentry/react (incl. Tracing) 41.4 kB +0.05% +17 B 🔺
@sentry/vue 28.2 kB - -
@sentry/vue (incl. Tracing) 41.23 kB +0.05% +17 B 🔺
@sentry/svelte 23.79 kB - -
CDN Bundle 25.28 kB - -
CDN Bundle (incl. Tracing) 39.3 kB +0.05% +16 B 🔺
CDN Bundle (incl. Tracing, Replay) 75.38 kB +0.02% +10 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) 80.82 kB +0.02% +15 B 🔺
CDN Bundle - uncompressed 73.86 kB - -
CDN Bundle (incl. Tracing) - uncompressed 116.35 kB +0.05% +47 B 🔺
CDN Bundle (incl. Tracing, Replay) - uncompressed 230.56 kB +0.03% +47 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 243.37 kB +0.02% +47 B 🔺
@sentry/nextjs (client) 43.46 kB +0.05% +19 B 🔺
@sentry/sveltekit (client) 39.87 kB +0.06% +21 B 🔺
@sentry/node-core 47.53 kB - -
@sentry/node 146.19 kB - -
@sentry/node - without tracing 91.63 kB - -
@sentry/aws-serverless 103.08 kB - -

View base workflow run

@Lms24 Lms24 self-assigned this Jul 31, 2025
@Lms24 Lms24 requested review from edwardgou-sentry, a team, mydea, AbhiPrasad and s1gr1d and removed request for a team and mydea July 31, 2025 15:27
Copy link
Member

@AbhiPrasad AbhiPrasad left a comment

Choose a reason for hiding this comment

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

Honestly feels like we shouldn't be using arrays at all, but approving to unblock

@Lms24
Copy link
Member Author

Lms24 commented Jul 31, 2025

Yeah you're right, we should think of a more robust way of storing and removing the subscribers. I'll merge this for now to unblock but will think about a safer approach to solve this more generally.

Opened #17276 to track.

@Lms24 Lms24 merged commit 52be861 into develop Aug 1, 2025
268 of 269 checks passed
@Lms24 Lms24 deleted the lms/fix-browser-utils-wv-unsubscribe branch August 1, 2025 07:23
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.

3 participants