Skip to content
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
13 changes: 9 additions & 4 deletions packages/nextjs/src/config/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ async function addSentryToEntryProperty(

// inject into all entry points which might contain user's code
for (const entryPointName in newEntryProperty) {
if (shouldAddSentryToEntryPoint(entryPointName)) {
if (shouldAddSentryToEntryPoint(entryPointName, isServer)) {
addFilesToExistingEntryPoint(newEntryProperty, entryPointName, filesToInject);
}
}
Expand Down Expand Up @@ -251,10 +251,15 @@ function checkWebpackPluginOverrides(
* Determine if this is an entry point into which both `Sentry.init()` code and the release value should be injected
*
* @param entryPointName The name of the entry point in question
* @param isServer Whether or not this function is being called in the context of a server build
* @returns `true` if sentry code should be injected, and `false` otherwise
*/
function shouldAddSentryToEntryPoint(entryPointName: string): boolean {
return entryPointName === 'pages/_app' || entryPointName.includes('pages/api');
function shouldAddSentryToEntryPoint(entryPointName: string, isServer: boolean): boolean {
return (
entryPointName === 'pages/_app' ||
entryPointName.includes('pages/api') ||
(isServer && entryPointName === 'pages/_error')
);
}

/**
Expand Down Expand Up @@ -295,7 +300,7 @@ export function getWebpackPluginOptions(
configFile: hasSentryProperties ? 'sentry.properties' : undefined,
stripPrefix: ['webpack://_N_E/'],
urlPrefix,
entries: shouldAddSentryToEntryPoint,
entries: (entryPointName: string) => shouldAddSentryToEntryPoint(entryPointName, isServer),
release: getSentryRelease(buildId),
dryRun: isDev,
});
Expand Down
33 changes: 29 additions & 4 deletions packages/nextjs/test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const defaultsObject = { defaultConfig: {} as NextConfigObject };
const serverWebpackConfig = {
entry: () =>
Promise.resolve({
'pages/api/dogs/[name]': 'private-next-pages/api/dogs/[name].js',
'pages/_error': 'private-next-pages/_error.js',
'pages/_app': ['./node_modules/smellOVision/index.js', 'private-next-pages/_app.js'],
'pages/api/simulator/dogStats/[name]': { import: 'private-next-pages/api/simulator/dogStats/[name].js' },
'pages/api/simulator/leaderboard': {
Expand All @@ -111,6 +111,7 @@ const clientWebpackConfig = {
Promise.resolve({
main: './src/index.ts',
'pages/_app': 'next-client-pages-loader?page=%2F_app',
'pages/_error': 'next-client-pages-loader?page=%2F_error',
}),
output: { filename: 'static/chunks/[name].js', path: '/Users/Maisey/projects/squirrelChasingSimulator/.next' },
target: 'web',
Expand Down Expand Up @@ -316,8 +317,8 @@ describe('webpack config', () => {
expect(finalWebpackConfig.entry).toEqual(
expect.objectContaining({
// original entrypoint value is a string
// (was 'private-next-pages/api/dogs/[name].js')
'pages/api/dogs/[name]': [rewriteFramesHelper, serverConfigFilePath, 'private-next-pages/api/dogs/[name].js'],
// (was 'private-next-pages/_error.js')
'pages/_error': [rewriteFramesHelper, serverConfigFilePath, 'private-next-pages/_error.js'],

// original entrypoint value is a string array
// (was ['./node_modules/smellOVision/index.js', 'private-next-pages/_app.js'])
Expand Down Expand Up @@ -379,6 +380,30 @@ describe('webpack config', () => {
);
});

it('injects user config file into `_error` in server bundle but not client bundle', async () => {
const finalServerWebpackConfig = await materializeFinalWebpackConfig({
userNextConfig,
incomingWebpackConfig: serverWebpackConfig,
incomingWebpackBuildContext: serverBuildContext,
});
const finalClientWebpackConfig = await materializeFinalWebpackConfig({
userNextConfig,
incomingWebpackConfig: clientWebpackConfig,
incomingWebpackBuildContext: clientBuildContext,
});

expect(finalServerWebpackConfig.entry).toEqual(
expect.objectContaining({
'pages/_error': expect.arrayContaining([serverConfigFilePath]),
}),
);
expect(finalClientWebpackConfig.entry).toEqual(
expect.objectContaining({
'pages/_error': expect.not.arrayContaining([clientConfigFilePath]),
}),
);
});

it('injects user config file into API routes', async () => {
const finalWebpackConfig = await materializeFinalWebpackConfig({
userNextConfig,
Expand All @@ -403,7 +428,7 @@ describe('webpack config', () => {
);
});

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