@@ -2,12 +2,29 @@ import * as Sentry from '@sentry/nextjs';
2
2
import type { WebFetchHeaders } from '@sentry/types' ;
3
3
// @ts -expect-error Because we cannot be sure if the RequestAsyncStorage module exists (it is not part of the Next.js public
4
4
// API) we use a shim if it doesn't exist. The logic for this is in the wrapping loader.
5
- import { requestAsyncStorage } from '__SENTRY_NEXTJS_REQUEST_ASYNC_STORAGE_SHIM__' ;
5
+ import * as origModule from '__SENTRY_NEXTJS_REQUEST_ASYNC_STORAGE_SHIM__' ;
6
6
// @ts -expect-error See above
7
7
import * as routeModule from '__SENTRY_WRAPPING_TARGET_FILE__' ;
8
8
9
9
import type { RequestAsyncStorage } from './requestAsyncStorageShim' ;
10
10
11
+ type NextAsyncStorageModule =
12
+ | {
13
+ workUnitAsyncStorage : RequestAsyncStorage ;
14
+ }
15
+ | {
16
+ requestAsyncStorage : RequestAsyncStorage ;
17
+ } ;
18
+
19
+ const asyncStorageModule = { ...origModule } as NextAsyncStorageModule ;
20
+
21
+ const requestAsyncStorage : RequestAsyncStorage | undefined =
22
+ 'workUnitAsyncStorage' in asyncStorageModule
23
+ ? asyncStorageModule . workUnitAsyncStorage
24
+ : 'requestAsyncStorage' in asyncStorageModule
25
+ ? asyncStorageModule . requestAsyncStorage
26
+ : undefined ;
27
+
11
28
function wrapHandler < T > ( handler : T , method : 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS' ) : T {
12
29
// Running the instrumentation code during the build phase will mark any function as "dynamic" because we're accessing
13
30
// the Request object. We do not want to turn handlers dynamic so we skip instrumentation in the build phase.
@@ -28,7 +45,7 @@ function wrapHandler<T>(handler: T, method: 'GET' | 'POST' | 'PUT' | 'PATCH' | '
28
45
// We try-catch here just in case the API around `requestAsyncStorage` changes unexpectedly since it is not public API
29
46
try {
30
47
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
31
- const requestAsyncStore = requestAsyncStorage . getStore ( ) as ReturnType < RequestAsyncStorage [ 'getStore' ] > ;
48
+ const requestAsyncStore = requestAsyncStorage ? .getStore ( ) as ReturnType < RequestAsyncStorage [ 'getStore' ] > ;
32
49
sentryTraceHeader = requestAsyncStore ?. headers . get ( 'sentry-trace' ) ?? undefined ;
33
50
baggageHeader = requestAsyncStore ?. headers . get ( 'baggage' ) ?? undefined ;
34
51
headers = requestAsyncStore ?. headers ;
@@ -54,8 +71,6 @@ export * from '__SENTRY_WRAPPING_TARGET_FILE__';
54
71
// @ts -expect-error This is the file we're wrapping
55
72
export { default } from '__SENTRY_WRAPPING_TARGET_FILE__' ;
56
73
57
- declare const requestAsyncStorage : RequestAsyncStorage ;
58
-
59
74
type RouteHandler = ( ...args : unknown [ ] ) => unknown ;
60
75
61
76
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
0 commit comments