Skip to content

Commit 1a6c26e

Browse files
authored
chore(nextjs): Remove require call on TypeScript file (#4498)
As part of server start-up on nextjs, we instrument various server methods for tracing, in a module called (appropriately enough) `instrumentServer`. Because even importing the `instrumentServer` module can throw errors in certain situations (during build, or in Next 12 on Vercel), the JS version of the module (which is what will exist at runtime) is conditionally required rather than imported at the top of the index file. Just for safety, that dynamic `require` is wrapped in a `try-catch`. Currently, in the catch block exists a fallback attempt to require the TS version of the module: https://github.com/getsentry/sentry-javascript/blob/ad37109568479bb8459b76f6bdfe0c0bd54e66dd/packages/nextjs/src/index.server.ts#L137-L151 This causes warnings in Next 12. Since there's no logical or replicable way to have the JS `require` fail but the TS `require` work (in spite of the comment, tests do not actually trigger this), and since no one can recall* why it's in there in the first place, this removes it, thus preventing the warnings. *Careful readers will notice that the PR which introduced this code was, as it turns out, authored by me. The reason I don't know why it's there in spite of that fact is because it was added by someone else, who then merged the PR.
1 parent ad37109 commit 1a6c26e

File tree

1 file changed

+2
-8
lines changed

1 file changed

+2
-8
lines changed

packages/nextjs/src/index.server.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,7 @@ if (!isVercel && !isBuild) {
140140
// eslint-disable-next-line @typescript-eslint/no-var-requires
141141
const { instrumentServer } = require('./utils/instrumentServer.js');
142142
instrumentServer();
143-
} catch {
144-
try {
145-
// eslint-disable-next-line @typescript-eslint/no-var-requires
146-
const { instrumentServer } = require('./utils/instrumentServer.ts');
147-
instrumentServer();
148-
} catch {
149-
// Server not instrumented. Not adding logs to avoid noise.
150-
}
143+
} catch (err) {
144+
logger.warn(`Error: Unable to instrument server for tracing. Got ${err}.`);
151145
}
152146
}

0 commit comments

Comments
 (0)