Skip to content

fix(nextjs): Strip sourceMappingURL comments after deleting source maps in turbopack builds - #19814

Merged
chargome merged 1 commit into
developfrom
cg/fix-next-client-sourcemap-url
Mar 16, 2026
Merged

fix(nextjs): Strip sourceMappingURL comments after deleting source maps in turbopack builds#19814
chargome merged 1 commit into
developfrom
cg/fix-next-client-sourcemap-url

Conversation

@chargome

Copy link
Copy Markdown
Member

When withSentryConfig is used with Next.js 16 + turbopack, the SDK auto-enables productionBrowserSourceMaps: true and deleteSourcemapsAfterUpload: true. After the build, .map files are deleted, but the corresponding //# sourceMappingURL=... comments remain in the JS chunks.

When browsers (e.g. Chrome DevTools) request these nonexistent .map files, Next.js 16 (turbopack) does not return a 404. Instead, the request falls through to the app router, where catch-all routes handle it. Since middleware matchers typically exclude _next paths, middleware does not run for
these requests, causing SSR failures for middleware-dependent features (e.g. Clerk's auth() throws because clerkMiddleware() headers are absent).

This fix strips dangling sourceMappingURL comments from client JS/CSS files after source map deletion, but only for turbopack builds where the issue manifests.

  • Scoped to turbopack only to minimize blast radius
  • Runs after deleteArtifacts() in the post-build hook
  • Handles JS, MJS, CJS, and CSS files recursively in static/

closes#19600

@chargome
chargome requested a review from logaretmMarch 16, 2026 10:54
@chargomechargome self-assigned this Mar 16, 2026
@github-actions

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.

PathSize% ChangeChange
@sentry/browser25.64 kB--
@sentry/browser - with treeshaking flags24.14 kB--
@sentry/browser (incl. Tracing)42.62 kB--
@sentry/browser (incl. Tracing, Profiling)47.28 kB--
@sentry/browser (incl. Tracing, Replay)81.42 kB--
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags71 kB--
@sentry/browser (incl. Tracing, Replay with Canvas)86.12 kB--
@sentry/browser (incl. Tracing, Replay, Feedback)98.37 kB--
@sentry/browser (incl. Feedback)42.45 kB--
@sentry/browser (incl. sendFeedback)30.31 kB--
@sentry/browser (incl. FeedbackAsync)35.36 kB--
@sentry/browser (incl. Metrics)26.92 kB--
@sentry/browser (incl. Logs)27.07 kB--
@sentry/browser (incl. Metrics & Logs)27.74 kB--
@sentry/react27.39 kB--
@sentry/react (incl. Tracing)44.95 kB--
@sentry/vue30.08 kB--
@sentry/vue (incl. Tracing)44.48 kB--
@sentry/svelte25.66 kB--
CDN Bundle28.27 kB--
CDN Bundle (incl. Tracing)43.5 kB--
CDN Bundle (incl. Logs, Metrics)29.13 kB--
CDN Bundle (incl. Tracing, Logs, Metrics)44.34 kB--
CDN Bundle (incl. Replay, Logs, Metrics)68.2 kB--
CDN Bundle (incl. Tracing, Replay)80.32 kB--
CDN Bundle (incl. Tracing, Replay, Logs, Metrics)81.22 kB--
CDN Bundle (incl. Tracing, Replay, Feedback)85.86 kB--
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics)86.76 kB--
CDN Bundle - uncompressed82.56 kB--
CDN Bundle (incl. Tracing) - uncompressed128.5 kB--
CDN Bundle (incl. Logs, Metrics) - uncompressed85.43 kB--
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed131.37 kB--
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed209.06 kB--
CDN Bundle (incl. Tracing, Replay) - uncompressed245.35 kB--
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed248.21 kB--
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed258.26 kB--
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed261.11 kB--
@sentry/nextjs (client)47.37 kB--
@sentry/sveltekit (client)43.07 kB--
@sentry/node-core56.34 kB+0.02%+11 B 🔺
@sentry/node173.23 kB+0.03%+48 B 🔺
@sentry/node - without tracing96.35 kB+0.01%+5 B 🔺
@sentry/aws-serverless113.34 kB+0.01%+7 B 🔺

View base workflow run

@chargome
chargome merged commit 399cc85 into developMar 16, 2026
74 checks passed
@chargome
chargome deleted the cg/fix-next-client-sourcemap-url branch March 16, 2026 17:02
@masterjanic

Copy link
Copy Markdown

@chargome This is causing heavy issues when Subresource Integrity (SRI) is enabled, because it modifies the files after build and the generated hashes don't match anymore!

Sign up for freeto 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.

Turbopack + @sentry/nextjs causes Clerk middleware detection to fail on /.well-known request when catch-all route exists

3 participants

@chargome@masterjanic@logaretm