Skip to content

Commit ddf1935

Browse files
authored
feat(nextjs): Warn when client was initialized more than once (#15971)
With the addition on `instrumentation-client.ts` the likelihood of `Sentry.init()` rose starkly so I figured it might be worth printing a warning. Resolves getsentry/sentry-wizard#930
1 parent 2b99569 commit ddf1935

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

packages/nextjs/src/client/index.ts

+13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Client, EventProcessor, Integration } from '@sentry/core';
2+
import { consoleSandbox } from '@sentry/core';
23
import { getGlobalScope } from '@sentry/core';
34
import { GLOBAL_OBJ, addEventProcessor, applySdkMetadata } from '@sentry/core';
45
import type { BrowserOptions } from '@sentry/react';
@@ -16,6 +17,8 @@ export * from '../common';
1617
export { captureUnderscoreErrorException } from '../common/pages-router-instrumentation/_error';
1718
export { browserTracingIntegration } from './browserTracingIntegration';
1819

20+
let clientIsInitialized = false;
21+
1922
const globalWithInjectedValues = GLOBAL_OBJ as typeof GLOBAL_OBJ & {
2023
_sentryRewriteFramesAssetPrefixPath: string;
2124
_sentryAssetPrefix?: string;
@@ -29,6 +32,16 @@ declare const __SENTRY_TRACING__: boolean;
2932

3033
/** Inits the Sentry NextJS SDK on the browser with the React SDK. */
3134
export function init(options: BrowserOptions): Client | undefined {
35+
if (clientIsInitialized) {
36+
consoleSandbox(() => {
37+
// eslint-disable-next-line no-console
38+
console.warn(
39+
'[@sentry/nextjs] You are calling `Sentry.init()` more than once on the client. This can happen if you have both a `sentry.client.config.ts` and a `instrumentation-client.ts` file with `Sentry.init()` calls. It is recommended to call `Sentry.init()` once in `instrumentation-client.ts`.',
40+
);
41+
});
42+
}
43+
clientIsInitialized = true;
44+
3245
const opts = {
3346
environment: getVercelEnv(true) || process.env.NODE_ENV,
3447
defaultIntegrations: getDefaultIntegrations(options),

0 commit comments

Comments
 (0)