Skip to content

Commit 90bd190

Browse files
authored
fix(nextjs): Fix runtime error for static pages (#7476)
1 parent 5f6e5b5 commit 90bd190

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

packages/nextjs/src/config/templates/serverComponentWrapperTemplate.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,15 @@ if (typeof serverComponent === 'function') {
3838
// If we call the headers function inside the build phase, Next.js will automatically mark the server component as
3939
// dynamic(SSR) which we do not want in case the users have a static component.
4040
if (process.env.NEXT_PHASE !== 'phase-production-build') {
41-
const headersList = headers();
42-
sentryTraceHeader = headersList.get('sentry-trace');
43-
baggageHeader = headersList.get('baggage');
41+
// try/catch because calling headers() when a previously statically generated page is being revalidated causes a
42+
// runtime error in next.js as switching a page from static to dynamic during runtime is not allowed
43+
try {
44+
const headersList = headers();
45+
sentryTraceHeader = headersList.get('sentry-trace');
46+
baggageHeader = headersList.get('baggage');
47+
} catch {
48+
/** empty */
49+
}
4450
}
4551

4652
return Sentry.wrapServerComponentWithSentry(originalFunction, {

0 commit comments

Comments
 (0)