Skip to content

Commit fe2d143

Browse files
committed
fix(nextjs): Fix resolution of request async storage module
1 parent c9aaf8b commit fe2d143

File tree

3 files changed

+51
-28
lines changed

3 files changed

+51
-28
lines changed

packages/nextjs/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@
3434
"@sentry/vercel-edge": "7.74.0",
3535
"@sentry/webpack-plugin": "1.20.0",
3636
"chalk": "3.0.0",
37+
"resolve": "1.22.8",
3738
"rollup": "2.78.0",
3839
"stacktrace-parser": "^0.1.10",
3940
"tslib": "^2.4.1 || ^1.9.3"
4041
},
4142
"devDependencies": {
43+
"@types/resolve": "1.20.3",
4244
"@types/webpack": "^4.41.31",
4345
"eslint-plugin-react": "^7.31.11",
4446
"next": "10.1.3"

packages/nextjs/src/config/webpack.ts

+39-27
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type SentryCliPlugin from '@sentry/webpack-plugin';
66
import * as chalk from 'chalk';
77
import * as fs from 'fs';
88
import * as path from 'path';
9+
import { sync as resolveSync } from 'resolve';
910

1011
import type { VercelCronsConfig } from '../common/types';
1112
// Note: If you need to import a type from Webpack, do it in `types.ts` and export it from there. Otherwise, our
@@ -126,7 +127,10 @@ export function constructWebpackConfigFunction(
126127
pageExtensionRegex,
127128
excludeServerRoutes: userSentryOptions.excludeServerRoutes,
128129
sentryConfigFilePath: getUserConfigFilePath(projectDir, runtime),
129-
nextjsRequestAsyncStorageModulePath: getRequestAsyncStorageModuleLocation(rawNewConfig.resolve?.modules),
130+
nextjsRequestAsyncStorageModulePath: getRequestAsyncStorageModuleLocation(
131+
projectDir,
132+
rawNewConfig.resolve?.modules,
133+
),
130134
};
131135

132136
const normalizeLoaderResourcePath = (resourcePath: string): string => {
@@ -977,39 +981,47 @@ function addValueInjectionLoader(
977981
);
978982
}
979983

984+
function resolveNextPackageDirFromDirectory(basedir: string): string | undefined {
985+
try {
986+
return path.dirname(resolveSync('next/package.json', { basedir }));
987+
} catch {
988+
// Should not happen in theory
989+
return undefined;
990+
}
991+
}
992+
993+
const POTENTIAL_REQUEST_ASNYC_STORAGE_LOCATIONS = [
994+
// Original location of RequestAsyncStorage
995+
// https://github.com/vercel/next.js/blob/46151dd68b417e7850146d00354f89930d10b43b/packages/next/src/client/components/request-async-storage.ts
996+
'next/dist/client/components/request-async-storage.js',
997+
// Introduced in Next.js 13.4.20
998+
// https://github.com/vercel/next.js/blob/e1bc270830f2fc2df3542d4ef4c61b916c802df3/packages/next/src/client/components/request-async-storage.external.ts
999+
'next/dist/client/components/request-async-storage.external.js',
1000+
];
1001+
9801002
function getRequestAsyncStorageModuleLocation(
1003+
webpackContextDir: string,
9811004
webpackResolvableModuleLocations: string[] | undefined,
9821005
): string | undefined {
9831006
if (webpackResolvableModuleLocations === undefined) {
9841007
return undefined;
9851008
}
9861009

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-
}
997-
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-
};
1010+
const absoluteWebpackResolvableModuleLocations = webpackResolvableModuleLocations.map(loc =>
1011+
path.resolve(webpackContextDir, loc),
1012+
);
10041013

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',
1009-
// Introduced in Next.js 13.4.20
1010-
// https://github.com/vercel/next.js/blob/e1bc270830f2fc2df3542d4ef4c61b916c802df3/packages/next/src/client/components/request-async-storage.external.ts
1011-
'next/dist/client/components/request-async-storage.external',
1012-
];
1014+
for (const webpackResolvableLocation of absoluteWebpackResolvableModuleLocations) {
1015+
const nextPackageDir = resolveNextPackageDirFromDirectory(webpackResolvableLocation);
1016+
if (nextPackageDir) {
1017+
const asyncLocalStorageLocation = POTENTIAL_REQUEST_ASNYC_STORAGE_LOCATIONS.find(loc =>
1018+
fs.existsSync(path.join(nextPackageDir, '..', loc)),
1019+
);
1020+
if (asyncLocalStorageLocation) {
1021+
return asyncLocalStorageLocation;
1022+
}
1023+
}
1024+
}
10131025

1014-
return potentialRequestAsyncStorageLocations.find(potentialLocation => moduleIsWebpackResolvable(potentialLocation));
1026+
return undefined;
10151027
}

yarn.lock

+10-1
Original file line numberDiff line numberDiff line change
@@ -6206,7 +6206,7 @@
62066206
dependencies:
62076207
"@types/node" "*"
62086208

6209-
"@types/resolve@^1.17.0":
6209+
"@types/resolve@1.20.3", "@types/resolve@^1.17.0":
62106210
version "1.20.3"
62116211
resolved "https://registry.yarnpkg.com/@types/resolve/-/resolve-1.20.3.tgz#066742d69a0bbba8c5d7d517f82e1140ddeb3c3c"
62126212
integrity sha512-NH5oErHOtHZYcjCtg69t26aXEk4BN2zLWqf7wnDZ+dpe0iR7Rds1SPGEItl3fca21oOe0n3OCnZ4W7jBxu7FOw==
@@ -26364,6 +26364,15 @@ [email protected]:
2636426364
is-core-module "^2.2.0"
2636526365
path-parse "^1.0.6"
2636626366

26367+
26368+
version "1.22.8"
26369+
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.22.8.tgz#b6c87a9f2aa06dfab52e3d70ac8cde321fa5a48d"
26370+
integrity sha512-oKWePCxqpd6FlLvGV1VU0x7bkPmmCNolxzjMf4NczoDnQcIWrAF+cPtZn5i6n+RfD2d9i0tzpKnG6Yk168yIyw==
26371+
dependencies:
26372+
is-core-module "^2.13.0"
26373+
path-parse "^1.0.7"
26374+
supports-preserve-symlinks-flag "^1.0.0"
26375+
2636726376
resolve@^1.1.6, resolve@^1.1.7, resolve@^1.10.0, resolve@^1.11.1, resolve@^1.12.0, resolve@^1.13.1, resolve@^1.14.2, resolve@^1.17.0, resolve@^1.19.0, resolve@^1.20.0, resolve@^1.22.0, resolve@^1.22.1, resolve@^1.3.2, resolve@^1.4.0, resolve@^1.5.0, resolve@^1.8.1:
2636826377
version "1.22.1"
2636926378
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.22.1.tgz#27cb2ebb53f91abb49470a928bba7558066ac177"

0 commit comments

Comments
 (0)