@@ -15,8 +15,8 @@ const SENTRY_WRAPPER_MODULE_NAME = 'sentry-wrapper-module';
15
15
// Needs to end in .cjs in order for the `commonjs` plugin to pick it up
16
16
const WRAPPING_TARGET_MODULE_NAME = '__SENTRY_WRAPPING_TARGET_FILE__.cjs' ;
17
17
18
- // Non -public API. Can be found here: https://github.com/vercel/next.js/blob/46151dd68b417e7850146d00354f89930d10b43b/packages/next/src/client/components/request-async-storage.ts
19
- const NEXTJS_REQUEST_ASYNC_STORAGE_MODULE_PATH = 'next/dist/client/components/request-async-storage' ;
18
+ // This module is non -public API and may break
19
+ const nextjsRequestAsyncStorageModulePath = getRequestAsyncLocalStorageModule ( ) ;
20
20
21
21
const apiWrapperTemplatePath = path . resolve ( __dirname , '..' , 'templates' , 'apiWrapperTemplate.js' ) ;
22
22
const apiWrapperTemplateCode = fs . readFileSync ( apiWrapperTemplatePath , { encoding : 'utf8' } ) ;
@@ -27,7 +27,6 @@ const pageWrapperTemplateCode = fs.readFileSync(pageWrapperTemplatePath, { encod
27
27
const middlewareWrapperTemplatePath = path . resolve ( __dirname , '..' , 'templates' , 'middlewareWrapperTemplate.js' ) ;
28
28
const middlewareWrapperTemplateCode = fs . readFileSync ( middlewareWrapperTemplatePath , { encoding : 'utf8' } ) ;
29
29
30
- const requestAsyncStorageModuleExists = moduleExists ( NEXTJS_REQUEST_ASYNC_STORAGE_MODULE_PATH ) ;
31
30
let showedMissingAsyncStorageModuleWarning = false ;
32
31
33
32
const sentryInitWrapperTemplatePath = path . resolve ( __dirname , '..' , 'templates' , 'sentryInitWrapperTemplate.js' ) ;
@@ -54,13 +53,28 @@ type LoaderOptions = {
54
53
vercelCronsConfig ?: VercelCronsConfig ;
55
54
} ;
56
55
57
- function moduleExists ( id : string ) : boolean {
56
+ function getRequestAsyncLocalStorageModule ( ) : string | undefined {
58
57
try {
59
- require . resolve ( id ) ;
60
- return true ;
61
- } catch ( e ) {
62
- return false ;
58
+ // Original location of that module
59
+ // https://github.com/vercel/next.js/blob/46151dd68b417e7850146d00354f89930d10b43b/packages/next/src/client/components/request-async-storage.ts
60
+ const location = 'next/dist/client/components/request-async-storage' ;
61
+ require . resolve ( location ) ;
62
+ return location ;
63
+ } catch {
64
+ // noop
63
65
}
66
+
67
+ try {
68
+ // Introduced in Next.js 13.4.20
69
+ // https://github.com/vercel/next.js/blob/e1bc270830f2fc2df3542d4ef4c61b916c802df3/packages/next/src/client/components/request-async-storage.external.ts
70
+ const location = 'next/dist/client/components/request-async-storage.external' ;
71
+ require . resolve ( location ) ;
72
+ return location ;
73
+ } catch {
74
+ // noop
75
+ }
76
+
77
+ return undefined ;
64
78
}
65
79
66
80
/**
@@ -183,10 +197,10 @@ export default function wrappingLoader(
183
197
templateCode = routeHandlerWrapperTemplateCode ;
184
198
}
185
199
186
- if ( requestAsyncStorageModuleExists ) {
200
+ if ( nextjsRequestAsyncStorageModulePath !== undefined ) {
187
201
templateCode = templateCode . replace (
188
202
/ _ _ S E N T R Y _ N E X T J S _ R E Q U E S T _ A S Y N C _ S T O R A G E _ S H I M _ _ / g,
189
- NEXTJS_REQUEST_ASYNC_STORAGE_MODULE_PATH ,
203
+ nextjsRequestAsyncStorageModulePath ,
190
204
) ;
191
205
} else {
192
206
if ( ! showedMissingAsyncStorageModuleWarning ) {
0 commit comments