Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/nextjs/src/config/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export function constructWebpackConfigFunction(

if (runtime !== 'client') {
warnAboutDeprecatedConfigFiles(projectDir, runtime);
warnAboutMissingonRequestErrorHandler(projectDir);
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's do this only for runtime === 'nodejs'.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

the runtime is either: client | edge | server

I used server for now

Copy link
Contributor

Choose a reason for hiding this comment

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

oh right. Yeah perfect

}

let rawNewConfig = { ...incomingConfig };
Expand Down Expand Up @@ -435,6 +436,33 @@ async function addSentryToClientEntryProperty(
return newEntryProperty;
}

/**
* Make sure the instrumentation file has a `onRequestError` Handler
*
* @param projectDir The root directory of the project, where config files would be located
*/
function warnAboutMissingonRequestErrorHandler(projectDir: string): void {
const instrumentationPaths = [
['src', 'instrumentation.ts'],
['src', 'instrumentation.js'],
['instrumentation.ts'],
['instrumentation.js'],
];
Comment on lines +447 to +452

Choose a reason for hiding this comment

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

Looks like this approach does not respect pageExtensions in Next.js config. I created #15652 to track this bug.

function hasOnRequestErrorHandler(pathSegments: string[]): boolean {
const filePath = path.resolve(projectDir, ...pathSegments);
try {
const content = fs.readFileSync(filePath, 'utf8');
return content.includes('export const onRequestError');
} catch (error) {
return false;
}
}
if (!instrumentationPaths.some(hasOnRequestErrorHandler)) {
// eslint-disable-next-line no-console
console.warn(`${chalk.yellow('[@sentry/nextjs]')} Missing 'onRequestError' handler in instrumentation file.`);
}
}

/**
* Searches for old `sentry.(server|edge).config.ts` files and Next.js instrumentation hooks and warns if there are "old"
* config files and no signs of them inside the instrumentation hook.
Expand Down
Loading