Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(nextjs): Add new potential location for Next.js request AsyncLocalStorage #9006

Merged
merged 2 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export async function PATCH() {
return NextResponse.json({ name: 'John Doe' }, { status: 401 });
}

export async function DELETE() {
export async function DELETE(): Promise<Response> {
throw new Error('route-handler-edge-error');
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export async function PUT() {
export async function PUT(): Promise<Response> {
throw new Error('route-handler-error');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason the new version wants types here. I think nextjs has some kind of ts plugin that checks for this.

}
34 changes: 24 additions & 10 deletions packages/nextjs/src/config/loaders/wrappingLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const SENTRY_WRAPPER_MODULE_NAME = 'sentry-wrapper-module';
// Needs to end in .cjs in order for the `commonjs` plugin to pick it up
const WRAPPING_TARGET_MODULE_NAME = '__SENTRY_WRAPPING_TARGET_FILE__.cjs';

// Non-public API. Can be found here: https://github.com/vercel/next.js/blob/46151dd68b417e7850146d00354f89930d10b43b/packages/next/src/client/components/request-async-storage.ts
const NEXTJS_REQUEST_ASYNC_STORAGE_MODULE_PATH = 'next/dist/client/components/request-async-storage';
// This module is non-public API and may break
const nextjsRequestAsyncStorageModulePath = getRequestAsyncLocalStorageModule();

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

const requestAsyncStorageModuleExists = moduleExists(NEXTJS_REQUEST_ASYNC_STORAGE_MODULE_PATH);
let showedMissingAsyncStorageModuleWarning = false;

const sentryInitWrapperTemplatePath = path.resolve(__dirname, '..', 'templates', 'sentryInitWrapperTemplate.js');
Expand All @@ -54,13 +53,28 @@ type LoaderOptions = {
vercelCronsConfig?: VercelCronsConfig;
};

function moduleExists(id: string): boolean {
function getRequestAsyncLocalStorageModule(): string | undefined {
try {
require.resolve(id);
return true;
} catch (e) {
return false;
// Original location of that module
// https://github.com/vercel/next.js/blob/46151dd68b417e7850146d00354f89930d10b43b/packages/next/src/client/components/request-async-storage.ts
const location = 'next/dist/client/components/request-async-storage';
require.resolve(location);
return location;
} catch {
// noop
}

try {
// Introduced in Next.js 13.4.20
// https://github.com/vercel/next.js/blob/e1bc270830f2fc2df3542d4ef4c61b916c802df3/packages/next/src/client/components/request-async-storage.external.ts
const location = 'next/dist/client/components/request-async-storage.external';
require.resolve(location);
return location;
} catch {
// noop
}

return undefined;
}

/**
Expand Down Expand Up @@ -183,10 +197,10 @@ export default function wrappingLoader(
templateCode = routeHandlerWrapperTemplateCode;
}

if (requestAsyncStorageModuleExists) {
if (nextjsRequestAsyncStorageModulePath !== undefined) {
templateCode = templateCode.replace(
/__SENTRY_NEXTJS_REQUEST_ASYNC_STORAGE_SHIM__/g,
NEXTJS_REQUEST_ASYNC_STORAGE_MODULE_PATH,
nextjsRequestAsyncStorageModulePath,
);
} else {
if (!showedMissingAsyncStorageModuleWarning) {
Expand Down