Skip to content

Commit fa992bd

Browse files
authored
fix(nextjs): Stop injecting sentry into API middleware (getsentry#4517)
During the build of a nextjs app, our SDK injects the user's `sentry.server.config.js` (which contains the call to `Sentry.init()`) into all API routes, so that `withSentry` will work. That said, we don't yet support nextjs middleware[1], so we shouldn't be injecting the file into middleware pages, even if they're in the `api/` folder. This prevents that by adding another condition to `shouldAddSentryToEntryPoint`. Note: This came up as part of the issue referenced below, and it does fix it, but only in a temporary way. The underlying issue is (I believe) something outside of sentry. I've encouraged the user to debug it, so that when we start supporting middleware (and therefore remove this filter), he doesn't run into problems again. See this comment: getsentry#4507 (comment). Fixes getsentry#4507. [1] https://nextjs.org/docs/middleware
1 parent 52520ba commit fa992bd

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

packages/nextjs/src/config/webpack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ function checkWebpackPluginOverrides(
262262
function shouldAddSentryToEntryPoint(entryPointName: string, isServer: boolean): boolean {
263263
return (
264264
entryPointName === 'pages/_app' ||
265-
entryPointName.includes('pages/api') ||
265+
(entryPointName.includes('pages/api') && !entryPointName.includes('_middleware')) ||
266266
(isServer && entryPointName === 'pages/_error')
267267
);
268268
}

packages/nextjs/test/config.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ const serverWebpackConfig = {
9292
Promise.resolve({
9393
'pages/_error': 'private-next-pages/_error.js',
9494
'pages/_app': ['./node_modules/smellOVision/index.js', 'private-next-pages/_app.js'],
95+
'pages/api/_middleware': 'private-next-pages/api/_middleware.js',
9596
'pages/api/simulator/dogStats/[name]': { import: 'private-next-pages/api/simulator/dogStats/[name].js' },
9697
'pages/api/simulator/leaderboard': {
9798
import: ['./node_modules/dogPoints/converter.js', 'private-next-pages/api/simulator/leaderboard.js'],
@@ -448,6 +449,21 @@ describe('webpack config', () => {
448449
);
449450
});
450451

452+
it('does not inject user config file into API middleware', async () => {
453+
const finalWebpackConfig = await materializeFinalWebpackConfig({
454+
userNextConfig,
455+
incomingWebpackConfig: serverWebpackConfig,
456+
incomingWebpackBuildContext: serverBuildContext,
457+
});
458+
459+
expect(finalWebpackConfig.entry).toEqual(
460+
expect.objectContaining({
461+
// no injected file
462+
'pages/api/_middleware': 'private-next-pages/api/_middleware.js',
463+
}),
464+
);
465+
});
466+
451467
it('does not inject anything into non-_app, non-_error, non-API routes', async () => {
452468
const finalWebpackConfig = await materializeFinalWebpackConfig({
453469
userNextConfig,

0 commit comments

Comments
 (0)