Skip to content

fix(nextjs): Keep the Pages Router runtime out of App Router client bundles - #24188

Merged
chargome merged 1 commit into
developfrom
chargome/fix/nextjs-static-pages-router-split
Sep 9, 2026
Merged

fix(nextjs): Keep the Pages Router runtime out of App Router client bundles#24188
chargome merged 1 commit into
developfrom
chargome/fix/nextjs-static-pages-router-split

Conversation

@chargome

@chargome chargome commented Sep 8, 2026

Copy link
Copy Markdown
Member

The client SDK statically imports next/router for Pages Router navigation spans, which pulls Next.js' whole Pages Router client runtime (~80 KB raw) into every App Router client bundle.

The navigation instrumentation now lives in its own module and is called behind a build-time constant. withSentryConfig sets _sentryHasPagesRouter to 'false' when the project has an app directory and no page files outside pages/api, so webpack and Turbopack drop the module and its next/router import. Pageload instrumentation stays, since App Router builds still serve 404.html/500.html through the Pages Router. Mixed app/pages projects are unaffected.

Chosen over the dynamic import() in #24033 to keep the SDK statically analyzable, keep navigation listener registration synchronous, and leave Pages Router apps untouched.

Fixes #24032
Refs #24033

@chargome chargome self-assigned this Sep 8, 2026
…undles

The client SDK statically imports `next/router` for Pages Router navigation
spans, and the app/pages decision is only made at runtime. That put Next.js'
entire Pages Router client runtime (~80 KB raw) into the client bundle of every
App Router app.

Move the navigation instrumentation, the only code needing `next/router`, into
its own module and call it behind a build-time constant. `withSentryConfig`
sets `_sentryHasPagesRouter` to `'false'` when the project has an `app`
directory and no page files outside `pages/api`, so the dead branch lets
webpack and Turbopack drop the module and the import. Pageload instrumentation
stays, since App Router builds still render `404.html` and `500.html` through
the Pages Router.

Fixes #24032
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@chargome
chargome force-pushed the chargome/fix/nextjs-static-pages-router-split branch from bdb77bd to b52c073 Compare September 8, 2026 08:37
@github-actions

github-actions Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 28.8 kB added added
@sentry/browser - with treeshaking flags 27.11 kB added added
@sentry/browser - with treeshaking flags tracing without tracing 27 kB added added
@sentry/browser (incl. Tracing) 49.22 kB added added
@sentry/browser (incl. Tracing + Span Streaming) 49.22 kB added added
@sentry/browser (incl. Tracing, Profiling) 52.12 kB added added
@sentry/browser (incl. Tracing, Replay) 88.76 kB added added
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 77.95 kB added added
@sentry/browser (incl. Tracing, Replay with Canvas) 93.44 kB added added
@sentry/browser (incl. Tracing, Replay, Feedback) 106.37 kB added added
@sentry/browser (incl. Feedback) 46.3 kB added added
@sentry/browser (incl. sendFeedback) 33.86 kB added added
@sentry/browser (incl. FeedbackAsync) 38.97 kB added added
@sentry/browser (incl. Metrics) 29.82 kB added added
@sentry/browser (incl. Logs) 30.09 kB added added
@sentry/browser (incl. Metrics & Logs) 30.75 kB added added
@sentry/react 30.55 kB added added
@sentry/react (incl. Tracing) 51.56 kB added added
@sentry/vue 36.05 kB added added
@sentry/vue (incl. Tracing) 51.48 kB added added
@sentry/svelte 28.83 kB added added
CDN Bundle 30.55 kB added added
CDN Bundle (incl. Tracing) 49.74 kB added added
CDN Bundle (incl. Logs, Metrics) 32.82 kB added added
CDN Bundle (incl. Tracing, Logs, Metrics) 51.7 kB added added
CDN Bundle (incl. Replay, Logs, Metrics) 73.48 kB added added
CDN Bundle (incl. Tracing, Replay) 87.29 kB added added
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 89.18 kB added added
CDN Bundle (incl. Tracing, Replay, Feedback) 93.22 kB added added
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 95.21 kB added added
CDN Bundle - uncompressed 90.46 kB added added
CDN Bundle (incl. Tracing) - uncompressed 148.17 kB added added
CDN Bundle (incl. Logs, Metrics) - uncompressed 97.03 kB added added
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 154.14 kB added added
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 226.29 kB added added
CDN Bundle (incl. Tracing, Replay) - uncompressed 267.76 kB added added
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 273.71 kB added added
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 281.46 kB added added
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 287.4 kB added added
@sentry/nextjs (client) 54.06 kB added added
@sentry/sveltekit (client) 49.65 kB added added
@sentry/core/server 36.99 kB added added
@sentry/core/browser 13.55 kB added added
@sentry/node 127.73 kB added added
@sentry/node/import (ESM hook with diagnostics-channel injection) 81.61 kB added added
@sentry/node - without tracing 88.73 kB added added
@sentry/node - without channel injection 106.98 kB added added
@sentry/aws-serverless 97.12 kB added added
@sentry/cloudflare (withSentry) - minified 201.95 kB added added
@sentry/cloudflare (withSentry) 502.67 kB added added

View base workflow run

@chargome

chargome commented Sep 8, 2026

Copy link
Copy Markdown
Member Author

bugbot run

@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.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit b52c073. Configure here.

@chargome
chargome marked this pull request as ready for review September 8, 2026 09:31
@chargome
chargome requested a review from a team as a code owner September 8, 2026 09:31
@chargome
chargome requested review from logaretm, nicohrubec and s1gr1d and removed request for a team September 8, 2026 09:31
@chargome
chargome merged commit c3e0818 into develop Sep 9, 2026
87 checks passed
@chargome
chargome deleted the chargome/fix/nextjs-static-pages-router-split branch September 9, 2026 07:58
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.

@sentry/nextjs bundles the whole Pages Router runtime (next/router, ~87 KB raw / 36 KB gzip) into every App Router app

2 participants