Skip to content

feat(browser): Add bfcacheIntegration - #22397

Open
logaretm wants to merge 15 commits into
developfrom
awad/bfcache-integration
Open

feat(browser): Add bfcacheIntegration#22397
logaretm wants to merge 15 commits into
developfrom
awad/bfcache-integration

Conversation

@logaretm

@logaretm logaretm commented Jul 20, 2026

Copy link
Copy Markdown
Member

Adds an opt-in bfcacheIntegration that reports back/forward-cache health as metrics: a hit/miss navigation counter, the (Chromium-only) notRestoredReasons behind a miss, and the fallback reload cost on a miss.

Metrics are the source of truth rather than spans, because a bfcache hit restores an existing page and never creates a pageload span, so span-only instrumentation would drop every successful restore from the dataset.

New Metrics

Three metrics, all under the browser.bfcache.* namespace.

metric type emitted key attributes answers
browser.bfcache.navigation counter 1 per back/forward navigation browser.bfcache.outcome (hit/miss), browser.bfcache.not_restored_reason_count (miss only) hit/miss rate (total = ungrouped sum; hit% = outcome:hit / total)
browser.bfcache.not_restored counter 1 per reason (a miss can emit several) browser.bfcache.reason (verbatim), browser.bfcache.frame (top/child) which blockers, and where (top document vs a child/embed)
browser.bfcache.reload.duration distribution (ms) 1 per miss the cost of a miss

Conventions PR is here getsentry/sentry-conventions#513

An example of a dashboard that uses bfcache metrics would look like this:

CleanShot 2026-07-20 at 14 12 41@2x

We can pre-ship a sentry-provided dashboard to onboard users to this feature.

Resources

@logaretm logaretm changed the title awad/bfcache integration feat(browser): Add bfcacheMetricsIntegration Jul 20, 2026
@logaretm
logaretm force-pushed the awad/bfcache-integration branch from 59903b5 to f7104d6 Compare July 20, 2026 18:20
@logaretm
logaretm force-pushed the awad/bfcache-integration branch from f7104d6 to fc820d3 Compare July 20, 2026 18:30
@logaretm
logaretm force-pushed the awad/bfcache-integration branch from 4cb315e to 04c860f Compare August 4, 2026 14:06
@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

⚠️ Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

Path Size % Change Change
@sentry/browser 30.33 kB -0.04% -11 B 🔽
@sentry/browser - with treeshaking flags 28.54 kB -0.04% -9 B 🔽
@sentry/browser - with treeshaking flags tracing without tracing 26.86 kB -0.03% -6 B 🔽
@sentry/browser (incl. Tracing) 49.08 kB +2.67% +1.28 kB 🔺
@sentry/browser (incl. Tracing + Span Streaming) 49.1 kB +2.68% +1.28 kB 🔺
@sentry/browser (incl. Tracing, Profiling) 53.88 kB +2.53% +1.32 kB 🔺
@sentry/browser (incl. Tracing, Replay) 88.43 kB +1.43% +1.24 kB 🔺
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 77.83 kB +1.59% +1.22 kB 🔺
@sentry/browser (incl. Tracing, Replay with Canvas) 93.12 kB +1.32% +1.21 kB 🔺
@sentry/browser (incl. Tracing, Replay, Feedback) 105.81 kB +1.2% +1.25 kB 🔺
@sentry/browser (incl. Feedback) 47.67 kB -0.02% -7 B 🔽
@sentry/browser (incl. sendFeedback) 35.16 kB -0.04% -11 B 🔽
@sentry/browser (incl. FeedbackAsync) 40.33 kB -0.02% -8 B 🔽
@sentry/browser (incl. Metrics) 31.4 kB -0.03% -9 B 🔽
@sentry/browser (incl. Logs) 31.65 kB -0.04% -10 B 🔽
@sentry/browser (incl. Metrics & Logs) 32.31 kB -0.03% -9 B 🔽
@sentry/react 32.14 kB -0.03% -9 B 🔽
@sentry/react (incl. Tracing) 51.27 kB +2.41% +1.2 kB 🔺
@sentry/vue 35.44 kB -0.03% -10 B 🔽
@sentry/vue (incl. Tracing) 51.05 kB +2.47% +1.23 kB 🔺
@sentry/svelte 30.36 kB -0.04% -11 B 🔽
CDN Bundle 32.34 kB +0.01% +2 B 🔺
CDN Bundle (incl. Tracing) 49.66 kB +3.18% +1.53 kB 🔺
CDN Bundle (incl. Logs, Metrics) 33.87 kB -0.04% -11 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics) 50.97 kB +3.05% +1.51 kB 🔺
CDN Bundle (incl. Replay, Logs, Metrics) 74.4 kB +1.61% +1.17 kB 🔺
CDN Bundle (incl. Tracing, Replay) 87.19 kB +1.7% +1.45 kB 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 88.47 kB +1.67% +1.45 kB 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) 93.07 kB +1.66% +1.51 kB 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 94.33 kB +1.59% +1.47 kB 🔺
CDN Bundle - uncompressed 95.88 kB +0.01% +2 B 🔺
CDN Bundle (incl. Tracing) - uncompressed 148.98 kB +3.77% +5.4 kB 🔺
CDN Bundle (incl. Logs, Metrics) - uncompressed 100.47 kB -0.04% -34 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 152.96 kB +3.66% +5.4 kB 🔺
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 229.39 kB +1.87% +4.2 kB 🔺
CDN Bundle (incl. Tracing, Replay) - uncompressed 268.24 kB +2.06% +5.4 kB 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 272.2 kB +2.03% +5.4 kB 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 281.94 kB +1.95% +5.39 kB 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 285.89 kB +1.93% +5.39 kB 🔺
@sentry/nextjs (client) 53.83 kB +2.3% +1.21 kB 🔺
@sentry/sveltekit (client) 49.47 kB +2.5% +1.2 kB 🔺
@sentry/core/server 65.77 kB - -
@sentry/core/browser 52 kB - -
@sentry/node 119.43 kB - -
@sentry/node/import (ESM hook with diagnostics-channel injection) 0 B added added
@sentry/node - without tracing 83.41 kB -0.01% -1 B 🔽
@sentry/aws-serverless 92.74 kB -0.01% -1 B 🔽
@sentry/cloudflare (withSentry) - minified 214.89 kB - -
@sentry/cloudflare (withSentry) 530.77 kB - -

View base workflow run

@logaretm

logaretm commented Aug 6, 2026

Copy link
Copy Markdown
Member Author

bugbot run

Comment thread packages/browser/src/integrations/bfcache.ts
@logaretm
logaretm marked this pull request as ready for review August 6, 2026 21:16
@logaretm
logaretm requested a review from a team as a code owner August 6, 2026 21:16
@logaretm
logaretm requested review from Lms24 and msonnb and removed request for a team August 6, 2026 21:16
logaretm added a commit that referenced this pull request Aug 7, 2026
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.
Add an opt-in browser integration that tracks back/forward cache health
as metrics:

- browser.bfcache.navigation: hit/miss counter by navigation type
- browser.bfcache.not_restored: Chromium notRestoredReasons per miss
- browser.bfcache.miss.reload.duration: fallback reload cost on a miss

Hits are detected via pageshow.persisted; misses via a back_forward
navigation entry. Includes unit tests for the notRestoredReasons parser
and a Playwright suite for hit/miss detection.
Adds a dedicated `browser-bfcache` e2e app that exercises
`bfcacheMetricsIntegration` against a genuine back/forward cache: real
cross-document navigation served by `vite preview`, restored via
renderer-initiated `history.back()`, under the full Chrome-for-Testing
binary (`channel: 'chromium'`) with the disable flag stripped and
`BackForwardCache` enabled.

Covers hits, misses with real `notRestoredReasons`, and deliberately
bfcache-ineligible pages via `?botch=`: an `unload` listener (stable
blocker) and an open WebSocket (blocks only before Chrome 149, so that
assertion is version-gated). `Cache-Control: no-store` no longer blocks.

Removes the simulated browser-integration bfcache suite, which faked the
restore with a synthetic `pageshow` and a manual flush; it is superseded
by the real assertions here. The reason-parser unit test stays.
The unload miss deterministically also yields a privacy-masked reason.
Assert the integration reports it as a `masked` frame, giving real-browser
coverage of the masked-frame classification path (previously only unit-tested).
A plain open IndexedDB connection (and even an in-flight transaction) no
longer blocks bfcache in current Chrome, contrary to web.dev's list. The
condition that still blocks is a connection holding up a version upgrade,
which yields an `idbversionchangeevent` reason. Add a deterministic e2e
case for it and document how real Chrome diverges from the article.
A no-store (CCNS) page is only evicted from bfcache once a cookie changes;
neither the header nor a cookie change blocks on its own. Add a `?botch=nostore`
case that serves the document with `Cache-Control: no-store` (via a preview
middleware, since a static server can't set per-request headers) and mutates a
cookie, asserting the `response-cache-control-no-store` reason.

Also trim the version-specific blocker details from the README so they don't
drift; the botch cases and their assertions are the source of truth.
`browser.bfcache.navigation_type` was 1:1 with `browser.bfcache.outcome`
(hit <-> back-forward-cache, miss <-> back-forward), so it added a second
attribute encoding the same bit. Keep only `outcome`; navigation-type
vocabulary belongs on web-vital samples (future soft-navigation work), not
on bfcache counters. Also drops an unnecessary PageTransitionEvent cast.
Adds two real-browser e2e cases: an embedded bfcache-eligible iframe keeps
the top page restorable (hit), and an iframe with an unload listener makes
the top page ineligible with the reason reported from the child frame. This
gives the `frame: 'child'` classification real coverage (previously unit-only).
`frame` no longer special-cases the `masked` reason value; it now reflects
only the frame's position in the notRestoredReasons tree (`top`/`child`).
The `masked` value already lives on the `reason` attribute, so tagging the
frame as `masked` too was redundant. The integration no longer inspects any
reason string - reasons pass through verbatim.
…utes

Undefined attribute values are dropped downstream, so assign them
directly instead of spreading conditional objects. Use the
SENTRY_SEGMENT_NAME constant from @sentry/conventions for the route
attribute instead of a hardcoded string.
Capping the not-restored reasons by default silently dropped data, so a
page blocked for more reasons than the cap looked cleaner than it was.
Report every reason by default and treat `maxReasons` as an opt-in cap.

The platform bounds `notRestoredReasons` to a small standardized set per
navigation, so reporting all carries no realistic cardinality risk. A cap
below 1 drops everything, so clamp it to 1 and warn on debug builds.
…on name

Compute the transaction name once per pageshow and thread it through the
metric helpers instead of re-reading the scope for each metric. Extract
the not-restored reason emission into its own helper and correct the
pageshow event type.
…tion

Name the integration after what it observes, consistent with
webVitalsIntegration and the rest of the integration exports. bfcache is
the platform term, spelled lowercase as one word.
Moves the pageshow registration from per-client setup to setupOnce so a
single listener is registered regardless of how many clients are created,
avoiding duplicate bfcache metrics on reinitialization.
…nd miss

Covers three segment-name behaviors: a hit carries the parameterized route
still on the scope after the freeze, a hit without a routing integration
falls back to the raw pathname, and a miss reload falls back to the reloaded
page's pathname rather than any pre-freeze route.
@logaretm
logaretm force-pushed the awad/bfcache-integration branch from 27e3472 to a692d13 Compare August 7, 2026 15:20

@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 a692d13. Configure here.

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