Skip to content

Commit 01bb58d

Browse files
authored
fix(nextjs): Make withSentryConfig return type match given config type (#3760)
When fixing https://github.com/getsentry/sentry-docs/issues/3723, the return type of `withSentryConfig` was changed from an object to a function which returns an object, since otherwise there's no way to get the `phase` and `defaultConfig` arguments needed when the user passes their existing nextjs config as a function. If users follow our instructions in https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/, and export `withSentryConfig(...)` with no further modifications, this is an invisible change, since they're never dealing with its return value. But for any who do further process said value, it turns out to have been a potentially breaking change. This reverts to the old behavior (of returning an object) in cases where the existing config is an object, and only returns a function when necessary, which is to say, when the existing config is itself a function. Fixes #3722.
1 parent 715a886 commit 01bb58d

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

packages/nextjs/src/config/index.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,22 @@ import { constructWebpackConfigFunction } from './webpack';
1111
export function withSentryConfig(
1212
userNextConfig: ExportedNextConfig = {},
1313
userSentryWebpackPluginOptions: Partial<SentryWebpackPluginOptions> = {},
14-
): NextConfigFunction {
15-
const newWebpackExport = constructWebpackConfigFunction(userNextConfig, userSentryWebpackPluginOptions);
16-
17-
const finalNextConfig = (
18-
phase: string,
19-
defaults: { defaultConfig: { [key: string]: unknown } },
20-
): NextConfigObject => {
21-
const materializedUserNextConfig =
22-
typeof userNextConfig === 'function' ? userNextConfig(phase, defaults) : userNextConfig;
23-
24-
return {
25-
...materializedUserNextConfig,
26-
// TODO When we add a way to disable the webpack plugin, doing so should turn this off, too
27-
productionBrowserSourceMaps: true,
28-
webpack: newWebpackExport,
29-
};
14+
): NextConfigFunction | NextConfigObject {
15+
const partialConfig = {
16+
// TODO When we add a way to disable the webpack plugin, doing so should turn this off, too
17+
productionBrowserSourceMaps: true,
18+
webpack: constructWebpackConfigFunction(userNextConfig, userSentryWebpackPluginOptions),
3019
};
3120

32-
return finalNextConfig;
21+
// If the user has passed us a function, we need to return a function, so that we have access to `phase` and
22+
// `defaults` in order to pass them along to the user's function
23+
if (typeof userNextConfig === 'function') {
24+
return (phase: string, defaults: { defaultConfig: { [key: string]: unknown } }): NextConfigObject => ({
25+
...userNextConfig(phase, defaults),
26+
...partialConfig,
27+
});
28+
}
29+
30+
// Otherwise, we can just merge their config with ours and return an object.
31+
return { ...userNextConfig, ...partialConfig };
3332
}

packages/nextjs/test/config.test.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,31 @@ const clientWebpackConfig = {
5050
const buildContext = { isServer: true, dev: false, buildId: 'doGsaREgReaT' };
5151

5252
/**
53-
* Derive the final values of all next config options, by first applying `withSentryConfig` and then running the
54-
* resulting function.
53+
* Derive the final values of all next config options, by first applying `withSentryConfig` and then, if it returns a
54+
* function, running that function.
5555
*
5656
* @param userNextConfig Next config options provided by the user
5757
* @param userSentryWebpackPluginConfig SentryWebpackPlugin options provided by the user
5858
*
59-
* @returns The config values next will receive when it calls the function returned by `withSentryConfig`
59+
* @returns The config values next will receive directly from `withSentryConfig` or when it calls the function returned
60+
* by `withSentryConfig`
6061
*/
6162
function materializeFinalNextConfig(
6263
userNextConfig: ExportedNextConfig,
6364
userSentryWebpackPluginConfig: SentryWebpackPluginOptions,
6465
): NextConfigObject {
65-
const configFunction = withSentryConfig(userNextConfig, userSentryWebpackPluginConfig);
66-
const finalConfigValues = configFunction('phase-production-build', {
67-
defaultConfig: {},
68-
});
66+
const sentrifiedConfig = withSentryConfig(userNextConfig, userSentryWebpackPluginConfig);
67+
let finalConfigValues = sentrifiedConfig;
68+
69+
if (typeof sentrifiedConfig === 'function') {
70+
// for some reason TS won't recognize that `finalConfigValues` is now a NextConfigObject, which is why the cast
71+
// below is necessary
72+
finalConfigValues = sentrifiedConfig('phase-production-build', {
73+
defaultConfig: {},
74+
});
75+
}
6976

70-
return finalConfigValues;
77+
return finalConfigValues as NextConfigObject;
7178
}
7279

7380
/**

0 commit comments

Comments
 (0)