Skip to content

Commit ded5302

Browse files
committed
feat(browser): Add skipBrowserExtensionCheck escape hatch option
1 parent b6dbde8 commit ded5302

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
5+
// We mock this here to simulate a Chrome browser extension
6+
window.chrome = { runtime: { id: 'mock-extension-id' } };
7+
8+
Sentry.init({
9+
dsn: 'https://[email protected]/1337',
10+
skipBrowserExtensionCheck: true,
11+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { expect } from '@playwright/test';
2+
import { sentryTest } from '../../../utils/fixtures';
3+
4+
sentryTest('should not initialize when inside a Chrome browser extension', async ({ getLocalTestUrl, page }) => {
5+
const url = await getLocalTestUrl({ testDir: __dirname });
6+
await page.goto(url);
7+
8+
const isInitialized = await page.evaluate(() => {
9+
return !!(window as any).Sentry.isInitialized();
10+
});
11+
12+
expect(isInitialized).toBe(true);
13+
});

packages/browser/src/client.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,25 @@ import { createUserFeedbackEnvelope } from './userfeedback';
2626
*/
2727
export type BrowserOptions = Options<BrowserTransportOptions> &
2828
BrowserClientReplayOptions &
29-
BrowserClientProfilingOptions;
29+
BrowserClientProfilingOptions & {
30+
/**
31+
* By default, the SDK will check if it is initialized in a browser extension
32+
* if you call `Sentry.init`. In case it is, it will stop initialization
33+
* because browser extensions require a different Sentry initialization process:
34+
* https://docs.sentry.io/platforms/javascript/best-practices/shared-environments/
35+
*
36+
* If this check wrongfully flags your setup as a browser extension, you can set this
37+
* option to `true` to skip the check.
38+
*
39+
* Important: Only set this option if you know what you are doing!
40+
*
41+
* Setting up the SDK in a browser extension with global error monitoring is not recommended
42+
* and will likely flood you with errors from other web sites or extensions.
43+
*
44+
* @default false
45+
*/
46+
skipBrowserExtensionCheck?: boolean;
47+
};
3048

3149
/**
3250
* Configuration options for the Sentry Browser SDK Client class

packages/browser/src/sdk.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ declare const __SENTRY_RELEASE__: string | undefined;
160160
export function init(browserOptions: BrowserOptions = {}): Client | undefined {
161161
const options = applyDefaultOptions(browserOptions);
162162

163-
if (shouldShowBrowserExtensionError()) {
163+
if (!options.skipBrowserExtensionCheck && shouldShowBrowserExtensionError()) {
164164
consoleSandbox(() => {
165165
// eslint-disable-next-line no-console
166166
console.error(

0 commit comments

Comments
 (0)