Skip to content

Commit 622186c

Browse files
authored
fix(nextjs): Use webpack module paths to attempt to resolve internal request async storage module (#9100)
1 parent 705be9b commit 622186c

File tree

3 files changed

+32
-27
lines changed

3 files changed

+32
-27
lines changed

packages/nextjs/src/config/loaders/wrappingLoader.ts

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ const SENTRY_WRAPPER_MODULE_NAME = 'sentry-wrapper-module';
1515
// Needs to end in .cjs in order for the `commonjs` plugin to pick it up
1616
const WRAPPING_TARGET_MODULE_NAME = '__SENTRY_WRAPPING_TARGET_FILE__.cjs';
1717

18-
// This module is non-public API and may break
19-
const nextjsRequestAsyncStorageModulePath = getRequestAsyncLocalStorageModule();
20-
2118
const apiWrapperTemplatePath = path.resolve(__dirname, '..', 'templates', 'apiWrapperTemplate.js');
2219
const apiWrapperTemplateCode = fs.readFileSync(apiWrapperTemplatePath, { encoding: 'utf8' });
2320

@@ -51,32 +48,9 @@ type LoaderOptions = {
5148
wrappingTargetKind: 'page' | 'api-route' | 'middleware' | 'server-component' | 'sentry-init' | 'route-handler';
5249
sentryConfigFilePath?: string;
5350
vercelCronsConfig?: VercelCronsConfig;
51+
nextjsRequestAsyncStorageModulePath?: string;
5452
};
5553

56-
function getRequestAsyncLocalStorageModule(): string | undefined {
57-
try {
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
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;
78-
}
79-
8054
/**
8155
* Replace the loaded file with a wrapped version the original file. In the wrapped version, the original file is loaded,
8256
* any data-fetching functions (`getInitialProps`, `getStaticProps`, and `getServerSideProps`) or API routes it contains
@@ -98,6 +72,7 @@ export default function wrappingLoader(
9872
wrappingTargetKind,
9973
sentryConfigFilePath,
10074
vercelCronsConfig,
75+
nextjsRequestAsyncStorageModulePath,
10176
} = 'getOptions' in this ? this.getOptions() : this.query;
10277

10378
this.async();

packages/nextjs/src/config/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ export type WebpackConfigObject = {
163163
target: string;
164164
context: string;
165165
resolve?: {
166+
modules?: string[];
166167
alias?: { [key: string]: string | boolean };
167168
};
168169
module?: {

packages/nextjs/src/config/webpack.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ export function constructWebpackConfigFunction(
126126
pageExtensionRegex,
127127
excludeServerRoutes: userSentryOptions.excludeServerRoutes,
128128
sentryConfigFilePath: getUserConfigFilePath(projectDir, runtime),
129+
nextjsRequestAsyncStorageModulePath: getRequestAsyncLocalStorageModuleLocation(rawNewConfig.resolve?.modules),
129130
};
130131

131132
const normalizeLoaderResourcePath = (resourcePath: string): string => {
@@ -975,3 +976,31 @@ function addValueInjectionLoader(
975976
},
976977
);
977978
}
979+
980+
function getRequestAsyncLocalStorageModuleLocation(modules: string[] | undefined): string | undefined {
981+
if (modules === undefined) {
982+
return undefined;
983+
}
984+
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+
}
994+
995+
try {
996+
// Introduced in Next.js 13.4.20
997+
// 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+
}
1004+
1005+
return undefined;
1006+
}

0 commit comments

Comments
 (0)