Skip to content

Commit 5a42bd9

Browse files
author
Luca Forstner
authored
fix(nextjs): Add new potential location for Next.js request AsyncLocalStorage (#9006)
1 parent 3db988f commit 5a42bd9

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

packages/e2e-tests/test-applications/nextjs-app-dir/app/route-handlers/[param]/edge/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ export async function PATCH() {
66
return NextResponse.json({ name: 'John Doe' }, { status: 401 });
77
}
88

9-
export async function DELETE() {
9+
export async function DELETE(): Promise<Response> {
1010
throw new Error('route-handler-edge-error');
1111
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export async function PUT() {
1+
export async function PUT(): Promise<Response> {
22
throw new Error('route-handler-error');
33
}

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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
1616
const 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

2121
const apiWrapperTemplatePath = path.resolve(__dirname, '..', 'templates', 'apiWrapperTemplate.js');
2222
const apiWrapperTemplateCode = fs.readFileSync(apiWrapperTemplatePath, { encoding: 'utf8' });
@@ -27,7 +27,6 @@ const pageWrapperTemplateCode = fs.readFileSync(pageWrapperTemplatePath, { encod
2727
const middlewareWrapperTemplatePath = path.resolve(__dirname, '..', 'templates', 'middlewareWrapperTemplate.js');
2828
const middlewareWrapperTemplateCode = fs.readFileSync(middlewareWrapperTemplatePath, { encoding: 'utf8' });
2929

30-
const requestAsyncStorageModuleExists = moduleExists(NEXTJS_REQUEST_ASYNC_STORAGE_MODULE_PATH);
3130
let showedMissingAsyncStorageModuleWarning = false;
3231

3332
const 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
/__SENTRY_NEXTJS_REQUEST_ASYNC_STORAGE_SHIM__/g,
189-
NEXTJS_REQUEST_ASYNC_STORAGE_MODULE_PATH,
203+
nextjsRequestAsyncStorageModulePath,
190204
);
191205
} else {
192206
if (!showedMissingAsyncStorageModuleWarning) {

0 commit comments

Comments
 (0)