@@ -13,10 +13,20 @@ import * as wrapee from '__SENTRY_WRAPPING_TARGET_FILE__';
13
13
import * as Sentry from '@sentry/nextjs' ;
14
14
// @ts -ignore This template is only used with the app directory so we know that this dependency exists.
15
15
// eslint-disable-next-line import/no-unresolved
16
+ import { staticGenerationAsyncStorage } from 'next/dist/client/components/static-generation-async-storage' ;
17
+ // @ts -ignore This template is only used with the app directory so we know that this dependency exists.
18
+ // eslint-disable-next-line import/no-unresolved
16
19
import { headers } from 'next/headers' ;
17
20
18
21
declare function headers ( ) : { get : ( header : string ) => string | undefined } ;
19
22
23
+ declare const staticGenerationAsyncStorage : {
24
+ getStore : ( ) => {
25
+ isStaticGeneration : boolean ;
26
+ dynamicShouldError : boolean ;
27
+ } ;
28
+ } ;
29
+
20
30
type ServerComponentModule = {
21
31
default : unknown ;
22
32
} ;
@@ -38,9 +48,15 @@ if (typeof serverComponent === 'function') {
38
48
// If we call the headers function inside the build phase, Next.js will automatically mark the server component as
39
49
// dynamic(SSR) which we do not want in case the users have a static component.
40
50
if ( process . env . NEXT_PHASE !== 'phase-production-build' ) {
41
- const headersList = headers ( ) ;
42
- sentryTraceHeader = headersList . get ( 'sentry-trace' ) ;
43
- baggageHeader = headersList . get ( 'baggage' ) ;
51
+ // The headers function can also not be used for revalidaton of previously statically generated pages as that
52
+ // results in a Runtime error because the page switches from static to dynamic
53
+ const staticGenerationStore = staticGenerationAsyncStorage . getStore ( ) ;
54
+ const dynamicAllowed = ! staticGenerationStore ?. isStaticGeneration && ! staticGenerationStore ?. dynamicShouldError ;
55
+ if ( dynamicAllowed ) {
56
+ const headersList = headers ( ) ;
57
+ sentryTraceHeader = headersList . get ( 'sentry-trace' ) ;
58
+ baggageHeader = headersList . get ( 'baggage' ) ;
59
+ }
44
60
}
45
61
46
62
return Sentry . wrapServerComponentWithSentry ( originalFunction , {
0 commit comments