Skip to content

fix(nextjs): Stop injecting sentry into API middleware #4517

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
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/nextjs/src/config/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ function checkWebpackPluginOverrides(
function shouldAddSentryToEntryPoint(entryPointName: string, isServer: boolean): boolean {
return (
entryPointName === 'pages/_app' ||
entryPointName.includes('pages/api') ||
(entryPointName.includes('pages/api') && !entryPointName.includes('_middleware')) ||
Copy link

Choose a reason for hiding this comment

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

It's an unlikely situation, but in theory a user could name its non-middleware fn something like 'pages/api/_middleware/foo.ts', which will match this check and thus inject sentry where it shouldn't.

(isServer && entryPointName === 'pages/_error')
);
}
Expand Down
16 changes: 16 additions & 0 deletions packages/nextjs/test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ const serverWebpackConfig = {
Promise.resolve({
'pages/_error': 'private-next-pages/_error.js',
'pages/_app': ['./node_modules/smellOVision/index.js', 'private-next-pages/_app.js'],
'pages/api/_middleware': 'private-next-pages/api/_middleware.js',
'pages/api/simulator/dogStats/[name]': { import: 'private-next-pages/api/simulator/dogStats/[name].js' },
'pages/api/simulator/leaderboard': {
import: ['./node_modules/dogPoints/converter.js', 'private-next-pages/api/simulator/leaderboard.js'],
Expand Down Expand Up @@ -448,6 +449,21 @@ describe('webpack config', () => {
);
});

it('does not inject user config file into API middleware', async () => {
const finalWebpackConfig = await materializeFinalWebpackConfig({
userNextConfig,
incomingWebpackConfig: serverWebpackConfig,
incomingWebpackBuildContext: serverBuildContext,
});

expect(finalWebpackConfig.entry).toEqual(
expect.objectContaining({
// no injected file
'pages/api/_middleware': 'private-next-pages/api/_middleware.js',
}),
);
});

it('does not inject anything into non-_app, non-_error, non-API routes', async () => {
const finalWebpackConfig = await materializeFinalWebpackConfig({
userNextConfig,
Expand Down