Skip to content

ref(nextjs): Clean up client-side integrations code #6382

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
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
48 changes: 23 additions & 25 deletions packages/nextjs/src/index.client.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { configureScope, init as reactInit, Integrations } from '@sentry/react';
import { BrowserTracing, defaultRequestInstrumentationOptions } from '@sentry/tracing';
import { BrowserTracing, defaultRequestInstrumentationOptions, hasTracingEnabled } from '@sentry/tracing';
import { EventProcessor } from '@sentry/types';

import { nextRouterInstrumentation } from './performance/client';
import { buildMetadata } from './utils/metadata';
import { NextjsOptions } from './utils/nextjsOptions';
import { addOrUpdateIntegration, UserIntegrations } from './utils/userIntegrations';
import { addOrUpdateIntegration } from './utils/userIntegrations';

export * from '@sentry/react';
export { nextRouterInstrumentation } from './performance/client';
Expand All @@ -32,21 +32,9 @@ declare const __SENTRY_TRACING__: boolean;
export function init(options: NextjsOptions): void {
buildMetadata(options, ['nextjs', 'react']);
options.environment = options.environment || process.env.NODE_ENV;
addClientIntegrations(options);

let integrations = options.integrations;

// Guard below evaluates to true unless __SENTRY_TRACING__ is text-replaced with "false"
if (typeof __SENTRY_TRACING__ === 'undefined' || __SENTRY_TRACING__) {
// Only add BrowserTracing if a tracesSampleRate or tracesSampler is set
if (options.tracesSampleRate !== undefined || options.tracesSampler !== undefined) {
integrations = createClientIntegrations(options.integrations);
}
}

reactInit({
...options,
integrations,
});
reactInit(options);

configureScope(scope => {
scope.setTag('runtime', 'browser');
Expand All @@ -57,14 +45,24 @@ export function init(options: NextjsOptions): void {
});
}

function createClientIntegrations(userIntegrations: UserIntegrations = []): UserIntegrations {
const defaultBrowserTracingIntegration = new BrowserTracing({
// eslint-disable-next-line deprecation/deprecation
tracingOrigins: [...defaultRequestInstrumentationOptions.tracingOrigins, /^(api\/)/],
routingInstrumentation: nextRouterInstrumentation,
});
function addClientIntegrations(options: NextjsOptions): void {
let integrations = options.integrations || [];

return addOrUpdateIntegration(defaultBrowserTracingIntegration, userIntegrations, {
'options.routingInstrumentation': nextRouterInstrumentation,
});
// This evaluates to true unless __SENTRY_TRACING__ is text-replaced with "false", in which case everything inside
// will get treeshaken away
if (typeof __SENTRY_TRACING__ === 'undefined' || __SENTRY_TRACING__) {
if (hasTracingEnabled(options)) {
const defaultBrowserTracingIntegration = new BrowserTracing({
// eslint-disable-next-line deprecation/deprecation
tracingOrigins: [...defaultRequestInstrumentationOptions.tracingOrigins, /^(api\/)/],
routingInstrumentation: nextRouterInstrumentation,
});

integrations = addOrUpdateIntegration(defaultBrowserTracingIntegration, integrations, {
'options.routingInstrumentation': nextRouterInstrumentation,
});
}
}

options.integrations = integrations;
}
2 changes: 1 addition & 1 deletion packages/nextjs/test/index.client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('Client init()', () => {
},
},
environment: 'test',
integrations: undefined,
integrations: [],
}),
);
});
Expand Down