@@ -15,8 +15,8 @@ const SENTRY_WRAPPER_MODULE_NAME = 'sentry-wrapper-module';
1515// Needs to end in .cjs in order for the `commonjs` plugin to pick it up
1616const WRAPPING_TARGET_MODULE_NAME = '__SENTRY_WRAPPING_TARGET_FILE__.cjs' ;
1717
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 ( ) ;
2020
2121const apiWrapperTemplatePath = path . resolve ( __dirname , '..' , 'templates' , 'apiWrapperTemplate.js' ) ;
2222const apiWrapperTemplateCode = fs . readFileSync ( apiWrapperTemplatePath , { encoding : 'utf8' } ) ;
@@ -27,7 +27,6 @@ const pageWrapperTemplateCode = fs.readFileSync(pageWrapperTemplatePath, { encod
2727const middlewareWrapperTemplatePath = path . resolve ( __dirname , '..' , 'templates' , 'middlewareWrapperTemplate.js' ) ;
2828const middlewareWrapperTemplateCode = fs . readFileSync ( middlewareWrapperTemplatePath , { encoding : 'utf8' } ) ;
2929
30- const requestAsyncStorageModuleExists = moduleExists ( NEXTJS_REQUEST_ASYNC_STORAGE_MODULE_PATH ) ;
3130let showedMissingAsyncStorageModuleWarning = false ;
3231
3332const sentryInitWrapperTemplatePath = path . resolve ( __dirname , '..' , 'templates' , 'sentryInitWrapperTemplate.js' ) ;
@@ -54,13 +53,28 @@ type LoaderOptions = {
5453 vercelCronsConfig ?: VercelCronsConfig ;
5554} ;
5655
57- function moduleExists ( id : string ) : boolean {
56+ function getRequestAsyncLocalStorageModule ( ) : string | undefined {
5857 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
6365 }
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 ;
6478}
6579
6680/**
@@ -183,10 +197,10 @@ export default function wrappingLoader(
183197 templateCode = routeHandlerWrapperTemplateCode ;
184198 }
185199
186- if ( requestAsyncStorageModuleExists ) {
200+ if ( nextjsRequestAsyncStorageModulePath !== undefined ) {
187201 templateCode = templateCode . replace (
188202 / _ _ 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 ,
190204 ) ;
191205 } else {
192206 if ( ! showedMissingAsyncStorageModuleWarning ) {
0 commit comments