@@ -126,7 +126,7 @@ export function constructWebpackConfigFunction(
126126 pageExtensionRegex,
127127 excludeServerRoutes : userSentryOptions . excludeServerRoutes ,
128128 sentryConfigFilePath : getUserConfigFilePath ( projectDir , runtime ) ,
129- nextjsRequestAsyncStorageModulePath : getRequestAsyncLocalStorageModuleLocation ( rawNewConfig . resolve ?. modules ) ,
129+ nextjsRequestAsyncStorageModulePath : getRequestAsyncStorageModuleLocation ( rawNewConfig . resolve ?. modules ) ,
130130 } ;
131131
132132 const normalizeLoaderResourcePath = ( resourcePath : string ) : string => {
@@ -977,30 +977,39 @@ function addValueInjectionLoader(
977977 ) ;
978978}
979979
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 ) {
982984 return undefined ;
983985 }
984986
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+ }
994997
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' ,
9961009 // Introduced in Next.js 13.4.20
9971010 // 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+ ] ;
10041013
1005- return undefined ;
1014+ return potentialRequestAsyncStorageLocations . find ( potentialLocation => moduleIsWebpackResolvable ( potentialLocation ) ) ;
10061015}
0 commit comments