Skip to content

Commit e54d840

Browse files
committed
simplify logic
1 parent 4700ae2 commit e54d840

File tree

3 files changed

+22
-39
lines changed

3 files changed

+22
-39
lines changed

packages/nextjs/src/index.client.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,13 @@ export function init(options: NextjsOptions): void {
5757
});
5858
}
5959

60-
function createClientIntegrations(userIntegrations?: UserIntegrations): UserIntegrations {
60+
function createClientIntegrations(userIntegrations: UserIntegrations = []): UserIntegrations {
6161
const defaultBrowserTracingIntegration = new BrowserTracing({
6262
tracingOrigins: [...defaultRequestInstrumentationOptions.tracingOrigins, /^(api\/)/],
6363
routingInstrumentation: nextRouterInstrumentation,
6464
});
6565

66-
if (userIntegrations) {
67-
return addOrUpdateIntegration(defaultBrowserTracingIntegration, userIntegrations, {
68-
BrowserTracing: { keyPath: 'options.routingInstrumentation', value: nextRouterInstrumentation },
69-
});
70-
} else {
71-
return [defaultBrowserTracingIntegration];
72-
}
66+
return addOrUpdateIntegration(defaultBrowserTracingIntegration, userIntegrations, {
67+
'options.routingInstrumentation': nextRouterInstrumentation,
68+
});
7369
}

packages/nextjs/src/index.server.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ function sdkAlreadyInitialized(): boolean {
9393
}
9494

9595
function addServerIntegrations(options: NextjsOptions): void {
96+
let integrations = options.integrations || [];
97+
9698
// This value is injected at build time, based on the output directory specified in the build config. Though a default
9799
// is set there, we set it here as well, just in case something has gone wrong with the injection.
98100
const distDirName = (global as GlobalWithDistDir).__rewriteFramesDistDir__ || '.next';
@@ -107,19 +109,16 @@ function addServerIntegrations(options: NextjsOptions): void {
107109
return frame;
108110
},
109111
});
110-
111-
if (options.integrations) {
112-
options.integrations = addOrUpdateIntegration(defaultRewriteFramesIntegration, options.integrations);
113-
} else {
114-
options.integrations = [defaultRewriteFramesIntegration];
115-
}
112+
integrations = addOrUpdateIntegration(defaultRewriteFramesIntegration, integrations);
116113

117114
if (hasTracingEnabled(options)) {
118115
const defaultHttpTracingIntegration = new Integrations.Http({ tracing: true });
119-
options.integrations = addOrUpdateIntegration(defaultHttpTracingIntegration, options.integrations, {
120-
Http: { keyPath: '_tracing', value: true },
116+
integrations = addOrUpdateIntegration(defaultHttpTracingIntegration, integrations, {
117+
_tracing: true,
121118
});
122119
}
120+
121+
options.integrations = integrations;
123122
}
124123

125124
export type { SentryWebpackPluginOptions } from './config/types';

packages/nextjs/src/utils/userIntegrations.ts

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@ export type UserIntegrationsFunction = (integrations: Integration[]) => Integrat
44
export type UserIntegrations = Integration[] | UserIntegrationsFunction;
55

66
type ForcedIntegrationOptions = {
7-
[integrationName: string]:
8-
| {
9-
keyPath: string;
10-
value: unknown;
11-
}
12-
| undefined;
7+
[keyPath: string]: unknown;
138
};
149

1510
/**
@@ -30,7 +25,8 @@ function setNestedKey(obj: Record<string, any>, keyPath: string, value: unknown)
3025
obj[keyPath] = value;
3126
} else {
3227
// `match[1]` is the initial segment of the path, and `match[2]` is the remainder of the path
33-
setNestedKey(obj[match[1]], match[2], value);
28+
const innerObj = obj[match[1]];
29+
setNestedKey(innerObj, match[2], value);
3430
}
3531
}
3632

@@ -52,34 +48,26 @@ export function addOrUpdateIntegration(
5248
userIntegrations: UserIntegrations,
5349
forcedOptions: ForcedIntegrationOptions = {},
5450
): UserIntegrations {
55-
if (Array.isArray(userIntegrations)) {
56-
return addOrUpdateIntegrationInArray(defaultIntegrationInstance, userIntegrations, forcedOptions);
57-
} else {
58-
return addOrUpdateIntegrationInFunction(defaultIntegrationInstance, userIntegrations, forcedOptions);
59-
}
51+
return Array.isArray(userIntegrations)
52+
? addOrUpdateIntegrationInArray(defaultIntegrationInstance, userIntegrations, forcedOptions)
53+
: addOrUpdateIntegrationInFunction(defaultIntegrationInstance, userIntegrations, forcedOptions);
6054
}
6155

6256
function addOrUpdateIntegrationInArray(
6357
defaultIntegrationInstance: Integration,
6458
userIntegrations: Integration[],
6559
forcedOptions: ForcedIntegrationOptions,
6660
): Integration[] {
67-
let includesName = false;
68-
// eslint-disable-next-line @typescript-eslint/prefer-for-of
69-
for (let x = 0; x < userIntegrations.length; x++) {
70-
if (userIntegrations[x].name === defaultIntegrationInstance.name) {
71-
includesName = true;
72-
}
61+
const userInstance = userIntegrations.find(integration => integration.name === defaultIntegrationInstance.name);
7362

74-
const optionToSet = forcedOptions[userIntegrations[x].name];
75-
if (optionToSet) {
76-
setNestedKey(userIntegrations[x], optionToSet.keyPath, optionToSet.value);
63+
if (userInstance) {
64+
for (const [keyPath, value] of Object.entries(forcedOptions)) {
65+
setNestedKey(userInstance, keyPath, value);
7766
}
78-
}
7967

80-
if (includesName) {
8168
return userIntegrations;
8269
}
70+
8371
return [...userIntegrations, defaultIntegrationInstance];
8472
}
8573

0 commit comments

Comments
 (0)