Skip to content

Commit a15ce30

Browse files
authored
feat(browser): Add skipBrowserExtensionCheck escape hatch option (#14147)
Add a general escape hatch for the browser extension detection check in favour of further bloating the bundle size by introducing more special cases in the extension check. This way, we still initially block `Sentry.init` and make users think twice how to proceed. However, we don't have to adapt the check every time a false positive gets reported.
1 parent 7781e04 commit a15ce30

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed
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+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { expect } from '@playwright/test';
2+
import { sentryTest } from '../../../utils/fixtures';
3+
4+
sentryTest(
5+
'initializes inside a Chrome browser extension if `skipBrowserExtensionCheck` is set',
6+
async ({ getLocalTestUrl, page }) => {
7+
const url = await getLocalTestUrl({ testDir: __dirname });
8+
await page.goto(url);
9+
10+
const isInitialized = await page.evaluate(() => {
11+
return !!(window as any).Sentry.isInitialized();
12+
});
13+
14+
expect(isInitialized).toBe(true);
15+
},
16+
);

packages/browser/src/client.ts

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

3150
/**
3251
* Configuration options for the Sentry Browser SDK Client class

packages/browser/src/sdk.ts

+1-1
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)