Skip to content

fix(nextjs): Apply top-level bundleSizeOptimizations to webpack and Turbopack builds - #24818

Merged
andreiborza merged 4 commits into
developfrom
ab/nextjs-turbopack-treeshake
Oct 12, 2026
Merged

andreiborza merged 4 commits into
developfrom
ab/nextjs-turbopack-treeshake

Conversation

@andreiborza

@andreiborza andreiborza commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

What

The top-level bundleSizeOptimizations option now removes unused SDK code in both webpack and Turbopack builds. webpack.treeshake is deprecated. It still works, but bundleSizeOptimizations wins when you set both.

Why

On Turbopack, the tree-shaking options did nothing and gave no warning. The old option also lived under webpack, which is wrong for an option that works with both bundlers.

Closes: #24795

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 29.82 kB - -
@sentry/browser - with treeshaking flags 27.96 kB - -
@sentry/browser - with treeshaking flags tracing without tracing 27.87 kB - -
@sentry/browser (incl. Tracing) 51.89 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 51.91 kB - -
@sentry/browser (incl. Tracing, Profiling) 54.84 kB - -
@sentry/browser (incl. Tracing, Replay) 91.64 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 80.51 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 96.36 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 109.34 kB - -
@sentry/browser (incl. Feedback) 47.35 kB - -
@sentry/browser (incl. sendFeedback) 34.87 kB - -
@sentry/browser (incl. FeedbackAsync) 39.98 kB - -
@sentry/browser (incl. Metrics) 30.85 kB - -
@sentry/browser (incl. Logs) 31.14 kB - -
@sentry/browser (incl. Metrics & Logs) 31.78 kB - -
@sentry/react 31.66 kB - -
@sentry/react (incl. Tracing) 54.22 kB - -
@sentry/vue 37.89 kB - -
@sentry/vue (incl. Tracing) 54.81 kB - -
@sentry/svelte 29.85 kB - -
@sentry/remix (Remix 3 client bundle) 56.88 kB - -
CDN Bundle 31.57 kB - -
CDN Bundle (incl. Tracing) 52.4 kB - -
CDN Bundle (incl. Logs, Metrics) 33.76 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 54.35 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 74.68 kB - -
CDN Bundle (incl. Tracing, Replay) 90.07 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 92.01 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 96.23 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 98.2 kB - -
CDN Bundle - uncompressed 93.05 kB - -
CDN Bundle (incl. Tracing) - uncompressed 155.64 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 99.59 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 161.59 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 229.63 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 275.8 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 281.74 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 289.51 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 295.44 kB - -
@sentry/nextjs (client) 56.62 kB +0.05% +28 B 🔺
@sentry/sveltekit (client) 52.27 kB - -
@sentry/core/server 40.87 kB - -
@sentry/core/browser 13.73 kB - -
@sentry/node 151.73 kB +0.01% +9 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 83.77 kB - -
@sentry/node - without tracing 94.29 kB +0.02% +12 B 🔺
@sentry/node - without channel injection 129.91 kB +0.01% +8 B 🔺
@sentry/aws-serverless 102.45 kB +0.01% +1 B 🔺
@sentry/cloudflare (withSentry) - minified 210.34 kB - -
@sentry/cloudflare (withSentry) 521.4 kB - -
@sentry/nextjs/cloudflare (withSentry) - minified 228.01 kB - -

View base workflow run

@andreiborza
andreiborza marked this pull request as ready for review September 29, 2026 09:00
@andreiborza
andreiborza requested a review from a team as a code owner September 29, 2026 09:00
@andreiborza
andreiborza requested review from nicohrubec and s1gr1d and removed request for a team September 29, 2026 09:00

@nicohrubec nicohrubec left a comment

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.

do I understand correctly that with this PR users would set webpack: { treeshake: { removeTracing: true }}} and this setting would then be applied even if they are on turbopack? feels a bit weird to me, why not either allow to set this in the turbopack options directly or even move it to top level if this is now supported for both bundlers?

@andreiborza

Copy link
Copy Markdown
Member Author

@nicohrubec yes, good point. I'll move it up.

@andreiborza
andreiborza marked this pull request as draft September 30, 2026 07:23
@andreiborza andreiborza changed the title fix(nextjs): Apply treeshake options to Turbopack builds fix(nextjs): Apply top-level bundleSizeOptimizations to webpack and Turbopack builds Oct 8, 2026
@andreiborza
andreiborza marked this pull request as ready for review October 8, 2026 13:08
@andreiborza

Copy link
Copy Markdown
Member Author

do I understand correctly that with this PR users would set webpack: { treeshake: { removeTracing: true }}} and this setting would then be applied even if they are on turbopack? feels a bit weird to me, why not either allow to set this in the turbopack options directly or even move it to top level if this is now supported for both bundlers?

I reworked this by moving it up as a toplevel flag and deprecated the old treeshake flag.

Comment thread packages/nextjs/src/config/types.ts Outdated
/**
* Tree-shaking options to help reduce the size of the Sentry SDK bundle.
*
* @deprecated Use the top-level `bundleSizeOptimizations` option instead, which works for both webpack and Turbopack builds.

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.

We moved this from top level to webpack specific not too long ago, I would like to avoid deprecating this right away again to not upset users. We can keep both top level and this one in parallel and just document top level for now and ship deprecation closer to v12.

Also I think the deprecation notice needs to be applied to nested options here too afaik so this gets picked up in IDEs.

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.

Right, makes sense. I removed the deprecation and left a TODO in 64cdaf2

consoleSandbox(() => {
// eslint-disable-next-line no-console
console.warn(
'[@sentry/nextjs] You have enabled `debug: true`, but Sentry debug logging was removed from your bundle (likely via `webpack.treeshake.removeDebugLogging: true`). Set that option to `false` to see Sentry debug output.',

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.

The old option in still usable, right? Maybe we should mention both options in the log? Also with a todo so we don't forget to remove it.

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 still works. I added both options to the log, with a TODO to remove the old one in v12, in c85dff4

@andreiborza
andreiborza merged commit 4aa5981 into develop Oct 12, 2026
359 checks passed
@andreiborza
andreiborza deleted the ab/nextjs-turbopack-treeshake branch October 12, 2026 09:12
andreiborza pushed a commit that referenced this pull request Oct 12, 2026
… and Turbopack builds (#25261)

Backport of: #24818

## Differences to the original PR

- `packages/nextjs/src/{client,edge,server}/index.ts`: the `debug: true`
warning still mentions `withSentryConfig({ disableLogger: true })`. v10
still has the deprecated `disableLogger` option, which develop removed.
The message now lists `bundleSizeOptimizations.excludeDebugStatements:
true`, `withSentryConfig({ disableLogger: true })`, and
`webpack.treeshake.removeDebugLogging: true`.
- `packages/nextjs/src/config/types.ts`: left out the TODO comment
change on the closing brace of
`_experimental.turbopackReactComponentAnnotation`. On v10 that line has
no `TODO(v12)` comment to replace. This only affects a comment.
-
`packages/nextjs/src/config/withSentryConfig/getFinalConfigObjectBundlerUtils.ts`:
the `../util` import leaves out `supportsTurbopackRuleCondition`, which
v10 doesn't have. The new `getTreeshakeDefines` import is unchanged.
- `packages/nextjs/test/config/webpack/constructWebpackConfig.test.ts`:
the new test spies on `core.loadModule` instead of
`coreServer.loadModule`. On v10 this test file imports `@sentry/core` as
`core` and has no `coreServer` import.
- `packages/nextjs/test/config/withSentryConfig.test.ts`: the existing
v10 test `adds a turbopack note when the deprecated option only applies
to webpack` now passes `{ ...exportedNextConfig }` instead of the shared
fixture. On v10, `disableLogger` maps to
`webpack.treeshake.removeDebugLogging`. With Turbopack enabled, the new
`maybeSetTurbopackTreeshakeDefines` writes `compiler.define` onto the
passed config. That change leaked into the shared fixture and broke the
new Turbopack treeshake tests. This only changes tests.

Reviewer note: because `disableLogger` maps to
`webpack.treeshake.removeDebugLogging` on v10, `disableLogger: true` now
also removes debug logging in Turbopack builds. The existing deprecation
warning for `disableLogger` still adds "(Not supported with
Turbopack.)". This backport leaves that warning unchanged.

<!-- junior-request-attribution:start -->
via Junior system actor `junior`.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!--
junior-conversation-id:agent-dispatch%3Adispatch_cc76606421a00682d726df899f995e6a
-->

--

[View Junior
Session](https://junior-prod.sentry.dev/conversations/agent-dispatch%3Adispatch_cc76606421a00682d726df899f995e6a)
[[Sentry]](https://sentry.sentry.io/explore/conversations/agent-dispatch%3Adispatch_cc76606421a00682d726df899f995e6a/?project=4510944073809921)

<!-- junior-session-footer:end -->

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
@linear-code

linear-code Bot commented Oct 12, 2026

Copy link
Copy Markdown

JS-3914

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 v11: tracing modules bundle even with tracesSampleRate: 0 — the default-integration import graph is static

4 participants