Skip to content

chore(nextjs): Remove require call on TypeScript file #4498

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

Merged

Conversation

lobsterkatie
Copy link
Member

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:

// Dynamically require the file because even importing from it causes Next 12 to crash on Vercel.
// In environments where the JS file doesn't exist, such as testing, import the TS file.
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { instrumentServer } = require('./utils/instrumentServer.js');
instrumentServer();
} catch {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { instrumentServer } = require('./utils/instrumentServer.ts');
instrumentServer();
} catch {
// Server not instrumented. Not adding logs to avoid noise.
}
}

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 PR 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.

Note: I thought it was worthwhile to log a warning when instrumenting the server fails, so I added that back in. It doesn't have the debug-build guard the way all other log statements currently do, but that's because the debug-build build process will have changed by the time it's relevant (in v7).

@github-actions
Copy link
Contributor

github-actions bot commented Feb 4, 2022

size-limit report

Path Base Size (ad37109) Current Size Change
@sentry/browser - CDN Bundle (gzipped) 19.68 KB 19.68 KB +0.02% 🔺
@sentry/browser - CDN Bundle (minified) 62.89 KB 62.9 KB +0.01% 🔺
@sentry/browser - Webpack 22.21 KB 22.21 KB 0%
@sentry/browser - Webpack - gzip = false 76.02 KB 76.02 KB 0%
@sentry/react - Webpack 22.24 KB 22.24 KB 0%
@sentry/nextjs Client - Webpack 46.24 KB 46.24 KB 0%
@sentry/browser + @sentry/tracing - CDN Bundle (gzipped) 28.22 KB 28.23 KB +0.02% 🔺

@lobsterkatie lobsterkatie merged commit 1a6c26e into master Feb 4, 2022
@lobsterkatie lobsterkatie deleted the kmclb-nextjs-stop-importing-ts-instrumentServer branch February 4, 2022 14:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants