@@ -126,7 +126,7 @@ export function constructWebpackConfigFunction(
126
126
pageExtensionRegex,
127
127
excludeServerRoutes : userSentryOptions . excludeServerRoutes ,
128
128
sentryConfigFilePath : getUserConfigFilePath ( projectDir , runtime ) ,
129
- nextjsRequestAsyncStorageModulePath : getRequestAsyncLocalStorageModuleLocation ( rawNewConfig . resolve ?. modules ) ,
129
+ nextjsRequestAsyncStorageModulePath : getRequestAsyncStorageModuleLocation ( rawNewConfig . resolve ?. modules ) ,
130
130
} ;
131
131
132
132
const normalizeLoaderResourcePath = ( resourcePath : string ) : string => {
@@ -977,30 +977,39 @@ function addValueInjectionLoader(
977
977
) ;
978
978
}
979
979
980
- function getRequestAsyncLocalStorageModuleLocation ( modules : string [ ] | undefined ) : string | undefined {
981
- if ( modules === undefined ) {
980
+ function getRequestAsyncStorageModuleLocation (
981
+ webpackResolvableModuleLocations : string [ ] | undefined ,
982
+ ) : string | undefined {
983
+ if ( webpackResolvableModuleLocations === undefined ) {
982
984
return undefined ;
983
985
}
984
986
985
- try {
986
- // Original location of that module
987
- // https://github.com/vercel/next.js/blob/46151dd68b417e7850146d00354f89930d10b43b/packages/next/src/client/components/request-async-storage.ts
988
- const location = 'next/dist/client/components/request-async-storage' ;
989
- require . resolve ( location , { paths : modules } ) ;
990
- return location ;
991
- } catch {
992
- // noop
993
- }
987
+ const absoluteWebpackResolvableModuleLocations = webpackResolvableModuleLocations . map ( m => path . resolve ( m ) ) ;
988
+ const moduleIsWebpackResolvable = ( moduleId : string ) : boolean => {
989
+ let requireResolveLocation : string ;
990
+ try {
991
+ // This will throw if the location is not resolvable at all.
992
+ // We provide a `paths` filter in order to maximally limit the potential locations to the locations webpack would check.
993
+ requireResolveLocation = require . resolve ( moduleId , { paths : webpackResolvableModuleLocations } ) ;
994
+ } catch {
995
+ return false ;
996
+ }
994
997
995
- try {
998
+ // Since the require.resolve approach still looks in "global" node_modules locations like for example "/user/lib/node"
999
+ // we further need to filter by locations that start with the locations that webpack would check for.
1000
+ return absoluteWebpackResolvableModuleLocations . some ( resolvableModuleLocation =>
1001
+ requireResolveLocation . startsWith ( resolvableModuleLocation ) ,
1002
+ ) ;
1003
+ } ;
1004
+
1005
+ const potentialRequestAsyncStorageLocations = [
1006
+ // Original location of RequestAsyncStorage
1007
+ // https://github.com/vercel/next.js/blob/46151dd68b417e7850146d00354f89930d10b43b/packages/next/src/client/components/request-async-storage.ts
1008
+ 'next/dist/client/components/request-async-storage' ,
996
1009
// Introduced in Next.js 13.4.20
997
1010
// https://github.com/vercel/next.js/blob/e1bc270830f2fc2df3542d4ef4c61b916c802df3/packages/next/src/client/components/request-async-storage.external.ts
998
- const location = 'next/dist/client/components/request-async-storage.external' ;
999
- require . resolve ( location , { paths : modules } ) ;
1000
- return location ;
1001
- } catch {
1002
- // noop
1003
- }
1011
+ 'next/dist/client/components/request-async-storage.external' ,
1012
+ ] ;
1004
1013
1005
- return undefined ;
1014
+ return potentialRequestAsyncStorageLocations . find ( potentialLocation => moduleIsWebpackResolvable ( potentialLocation ) ) ;
1006
1015
}
0 commit comments