Skip to content

ref(browser): Skip browser extension warning in non-debug builds #15310

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
merged 3 commits into from
Feb 6, 2025
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from '@playwright/test';
import { sentryTest } from '../../../utils/fixtures';
import { hasDebugLogs } from '../../../utils/helpers';

sentryTest(
'should not initialize when inside a Firefox/Safari browser extension',
Expand All @@ -18,9 +19,14 @@ sentryTest(
});

expect(isInitialized).toEqual(false);
expect(errorLogs.length).toEqual(1);
expect(errorLogs[0]).toEqual(
'[Sentry] You cannot run Sentry this way in a browser extension, check: https://docs.sentry.io/platforms/javascript/best-practices/browser-extensions/',
);

if (hasDebugLogs()) {
expect(errorLogs.length).toEqual(1);
expect(errorLogs[0]).toEqual(
'[Sentry] You cannot run Sentry this way in a browser extension, check: https://docs.sentry.io/platforms/javascript/best-practices/browser-extensions/',
);
} else {
expect(errorLogs.length).toEqual(0);
}
},
);
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from '@playwright/test';
import { sentryTest } from '../../../utils/fixtures';
import { hasDebugLogs } from '../../../utils/helpers';

sentryTest('should not initialize when inside a Chrome browser extension', async ({ getLocalTestUrl, page }) => {
const errorLogs: string[] = [];
Expand All @@ -16,8 +17,13 @@ sentryTest('should not initialize when inside a Chrome browser extension', async
});

expect(isInitialized).toEqual(false);
expect(errorLogs.length).toEqual(1);
expect(errorLogs[0]).toEqual(
'[Sentry] You cannot run Sentry this way in a browser extension, check: https://docs.sentry.io/platforms/javascript/best-practices/browser-extensions/',
);

if (hasDebugLogs()) {
expect(errorLogs.length).toEqual(1);
expect(errorLogs[0]).toEqual(
'[Sentry] You cannot run Sentry this way in a browser extension, check: https://docs.sentry.io/platforms/javascript/best-practices/browser-extensions/',
);
} else {
expect(errorLogs.length).toEqual(0);
}
});
8 changes: 8 additions & 0 deletions dev-packages/browser-integration-tests/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,14 @@ export function shouldSkipFeatureFlagsTest(): boolean {
return bundle != null && !bundle.includes('esm') && !bundle.includes('cjs');
}

/**
* Returns true if the current bundle has debug logs.
*/
export function hasDebugLogs(): boolean {
const bundle = process.env.PW_BUNDLE;
return !bundle?.includes('min');
}

/**
* Waits until a number of requests matching urlRgx at the given URL arrive.
* If the timeout option is configured, this function will abort waiting, even if it hasn't received the configured
Expand Down
24 changes: 12 additions & 12 deletions packages/browser/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,21 +173,21 @@ export function init(browserOptions: BrowserOptions = {}): Client | undefined {
const options = applyDefaultOptions(browserOptions);

if (!options.skipBrowserExtensionCheck && shouldShowBrowserExtensionError()) {
consoleSandbox(() => {
// eslint-disable-next-line no-console
console.error(
'[Sentry] You cannot run Sentry this way in a browser extension, check: https://docs.sentry.io/platforms/javascript/best-practices/browser-extensions/',
);
});
if (DEBUG_BUILD) {
consoleSandbox(() => {
// eslint-disable-next-line no-console
console.error(
'[Sentry] You cannot run Sentry this way in a browser extension, check: https://docs.sentry.io/platforms/javascript/best-practices/browser-extensions/',
);
});
}
return;
}

if (DEBUG_BUILD) {
if (!supportsFetch()) {
logger.warn(
'No Fetch API detected. The Sentry SDK requires a Fetch API compatible environment to send events. Please add a Fetch API polyfill.',
);
}
if (DEBUG_BUILD && !supportsFetch()) {
logger.warn(
'No Fetch API detected. The Sentry SDK requires a Fetch API compatible environment to send events. Please add a Fetch API polyfill.',
);
}
const clientOptions: BrowserClientOptions = {
...options,
Expand Down
Loading