Skip to content

ref(browser): Restructure browser-utils by domain - #23071

Open
logaretm wants to merge 1 commit into
awad/webvitals-dep-onlyfrom
awad/webvitals-restructure
Open

ref(browser): Restructure browser-utils by domain#23071
logaretm wants to merge 1 commit into
awad/webvitals-dep-onlyfrom
awad/webvitals-restructure

Conversation

@logaretm

@logaretm logaretm commented Aug 5, 2026

Copy link
Copy Markdown
Member

Reorganizes browser-utils/src away from the catch-all metrics/ folder into clear domains: instrumentation/ (dom/history/location/xhr + the PerformanceObserver layer), web-vitals/ (tracking, spans, inp, lcp, reportEvents, and the helpers merged into one utils), and performance/ (entries, element/user/resource timing, shared utils).

The reasoning is we will actually have some metric emitting logic in there and we don't want it to be confused with pre-existing logic like in #22397

Following up on pulling in webvitals as a dependency, I took this chance to clean up our structure and it does have some minor bundle-size improvements. The bundle increases are due to #23070 which explains where the increase is coming from and why we are willing to absorb it.

@logaretm
logaretm force-pushed the awad/webvitals-restructure branch from af2fd8a to cc6aaf8 Compare August 5, 2026 16:47
@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 30.11 kB -0.04% -11 B 🔽
@sentry/browser - with treeshaking flags 28.31 kB -0.04% -10 B 🔽
@sentry/browser (incl. Tracing) 48.81 kB +2.68% +1.27 kB 🔺
@sentry/browser (incl. Tracing + Span Streaming) 48.83 kB +2.71% +1.28 kB 🔺
@sentry/browser (incl. Tracing, Profiling) 53.62 kB +2.55% +1.33 kB 🔺
@sentry/browser (incl. Tracing, Replay) 88.14 kB +1.41% +1.23 kB 🔺
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 77.57 kB +1.64% +1.25 kB 🔺
@sentry/browser (incl. Tracing, Replay with Canvas) 92.83 kB +1.35% +1.23 kB 🔺
@sentry/browser (incl. Tracing, Replay, Feedback) 105.49 kB +1.2% +1.24 kB 🔺
@sentry/browser (incl. Feedback) 47.44 kB -0.02% -7 B 🔽
@sentry/browser (incl. sendFeedback) 34.95 kB -0.04% -11 B 🔽
@sentry/browser (incl. FeedbackAsync) 40.1 kB -0.03% -9 B 🔽
@sentry/browser (incl. Metrics) 31.19 kB -0.04% -10 B 🔽
@sentry/browser (incl. Logs) 31.4 kB -0.04% -12 B 🔽
@sentry/browser (incl. Metrics & Logs) 32.1 kB -0.04% -11 B 🔽
@sentry/react 31.92 kB -0.03% -8 B 🔽
@sentry/react (incl. Tracing) 50.98 kB +2.39% +1.19 kB 🔺
@sentry/vue 35.19 kB -0.03% -8 B 🔽
@sentry/vue (incl. Tracing) 50.75 kB +2.5% +1.23 kB 🔺
@sentry/svelte 30.13 kB -0.04% -10 B 🔽
CDN Bundle 32.13 kB - -
CDN Bundle (incl. Tracing) 49.38 kB +3.3% +1.57 kB 🔺
CDN Bundle (incl. Logs, Metrics) 33.65 kB -0.07% -23 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics) 50.7 kB +3.09% +1.52 kB 🔺
CDN Bundle (incl. Replay, Logs, Metrics) 74.2 kB +1.62% +1.18 kB 🔺
CDN Bundle (incl. Tracing, Replay) 86.92 kB +1.73% +1.48 kB 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 88.2 kB +1.66% +1.43 kB 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) 92.76 kB +1.64% +1.49 kB 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 94.06 kB +1.6% +1.48 kB 🔺
CDN Bundle - uncompressed 95.28 kB +0.01% +2 B 🔺
CDN Bundle (incl. Tracing) - uncompressed 148.14 kB +3.77% +5.37 kB 🔺
CDN Bundle (incl. Logs, Metrics) - uncompressed 99.83 kB -0.07% -64 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 152.12 kB +3.66% +5.37 kB 🔺
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 228.76 kB +1.86% +4.17 kB 🔺
CDN Bundle (incl. Tracing, Replay) - uncompressed 267.4 kB +2.05% +5.37 kB 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 271.37 kB +2.02% +5.37 kB 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 281.1 kB +1.95% +5.36 kB 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 285.05 kB +1.92% +5.36 kB 🔺
@sentry/nextjs (client) 53.53 kB +2.24% +1.17 kB 🔺
@sentry/sveltekit (client) 49.19 kB +2.51% +1.2 kB 🔺
@sentry/core/server 65.54 kB - -
@sentry/core/browser 51.82 kB - -
@sentry/node 120.46 kB - -
@sentry/node/import (ESM hook with diagnostics-channel injection) 0 B added added
@sentry/node - without tracing 84.11 kB -0.01% -1 B 🔽
@sentry/aws-serverless 92.61 kB - -
@sentry/cloudflare (withSentry) - minified 217.81 kB - -
@sentry/cloudflare (withSentry) 537.06 kB - -

View base workflow run

@logaretm
logaretm force-pushed the awad/webvitals-restructure branch from cc6aaf8 to 071799f Compare August 5, 2026 17:35
@logaretm
logaretm force-pushed the awad/webvitals-restructure branch 2 times, most recently from e1990b4 to 794433e Compare August 5, 2026 19:00
@logaretm
logaretm force-pushed the awad/webvitals-restructure branch from 794433e to 7a246f5 Compare August 5, 2026 19:16
Reorganize browser-utils/src away from the catch-all metrics/ folder into
clear domains:

- instrumentation/ for event-source hooks (dom, history, location, xhr) plus
  the PerformanceObserver layer, renamed from metrics/instrument.ts to
  instrumentation/performanceObserver.ts to stop colliding with the folder
- web-vitals/ for web-vital tracking, spans, inp, lcp, reportEvents, and the
  browser helpers merged into a single utils.ts
- performance/ for generic performance-entry enrichment: entries, element/
  user/resource timing, and shared utils

browserMetrics.ts is split into web-vitals/tracking.ts and performance/
entries.ts, and utils.ts into web-vitals/reportEvents.ts and performance/
utils.ts. Public API (index.ts re-exports) is unchanged; only internal paths
and the test tree move.
@logaretm
logaretm force-pushed the awad/webvitals-restructure branch from 7a246f5 to 12d0fc5 Compare August 5, 2026 19:44
@logaretm
logaretm marked this pull request as ready for review August 5, 2026 20:05
@logaretm
logaretm requested a review from a team as a code owner August 5, 2026 20:05
@logaretm
logaretm requested review from Lms24 and msonnb and removed request for a team August 5, 2026 20:05
Comment on lines +71 to +79
export const onHidden = (cb: OnHiddenCallback) => {
const onHiddenCallback = (event: Event) => {
if (WINDOW.document?.visibilityState === 'hidden') {
cb(event);
}
};

addPageListener('visibilitychange', onHiddenCallback, { capture: true });
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bug: The new onHidden function adds a visibilitychange event listener that is never removed, causing a minor memory leak each time it's called.
Severity: LOW

Suggested Fix

The onHidden function should return a function that, when called, removes the event listener it added. The caller, listenForWebVitalReportEvents, should then invoke this cleanup function after the web vital has been reported to ensure the listener is properly garbage collected. Alternatively, use the existing getVisibilityWatcher().onHidden method which already manages listener cleanup.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: packages/browser-utils/src/web-vitals/utils.ts#L71-L79

Potential issue: The new `onHidden` function adds a `visibilitychange` event listener
via `addPageListener` but never removes it. This function is called by
`listenForWebVitalReportEvents` for LCP and CLS tracking, resulting in new, persistent
event listeners being added on each call. While a guard (`_runCollectorCallbackOnce`)
prevents the callback from executing more than once, the listener object itself is never
garbage collected, leading to a memory leak. In a typical page load, this results in a
couple of unremoved listeners, which is a minor issue. However, in long-lived
single-page applications or if tracking functions are called multiple times, the
accumulation of listeners could become problematic.

Also affects:

  • packages/browser-utils/src/web-vitals/reportEvents.ts:34~36

Did we get this right? 👍 / 👎 to inform future reviews.

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.

1 participant