diff --git a/packages/browser/src/sdk.ts b/packages/browser/src/sdk.ts index 522f87e33d8b..4dd323c6aa02 100644 --- a/packages/browser/src/sdk.ts +++ b/packages/browser/src/sdk.ts @@ -63,6 +63,7 @@ function applyDefaultOptions(optionsArg: BrowserOptions = {}): BrowserOptions { type ExtensionProperties = { chrome?: Runtime; browser?: Runtime; + nw?: unknown; }; type Runtime = { runtime?: { @@ -85,7 +86,11 @@ function shouldShowBrowserExtensionError(): boolean { const isDedicatedExtensionPage = !!runtimeId && WINDOW === WINDOW.top && extensionProtocols.some(protocol => href.startsWith(`${protocol}//`)); - return !!runtimeId && !isDedicatedExtensionPage; + // Running the SDK in NW.js, which appears like a browser extension but isn't, is also fine + // see: https://github.com/getsentry/sentry-javascript/issues/12668 + const isNWjs = typeof windowWithMaybeExtension.nw !== 'undefined'; + + return !!runtimeId && !isDedicatedExtensionPage && !isNWjs; } /** diff --git a/packages/browser/test/unit/sdk.test.ts b/packages/browser/test/unit/sdk.test.ts index dad7e37af0ce..667ad850fb36 100644 --- a/packages/browser/test/unit/sdk.test.ts +++ b/packages/browser/test/unit/sdk.test.ts @@ -142,6 +142,7 @@ describe('init', () => { afterEach(() => { Object.defineProperty(WINDOW, 'chrome', { value: undefined, writable: true }); Object.defineProperty(WINDOW, 'browser', { value: undefined, writable: true }); + Object.defineProperty(WINDOW, 'nw', { value: undefined, writable: true }); }); it('logs a browser extension error if executed inside a Chrome extension', () => { @@ -210,6 +211,18 @@ describe('init', () => { consoleErrorSpy.mockRestore(); }); + it("doesn't log a browser extension error if executed inside an NW.js environment", () => { + const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); + + Object.defineProperty(WINDOW, 'nw', { value: {} }); + + init(options); + + expect(consoleErrorSpy).not.toHaveBeenCalled(); + + consoleErrorSpy.mockRestore(); + }); + it("doesn't return a client on initialization error", () => { const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});