Skip to content

Commit afef63e

Browse files
authored
fix(browser): Allow SDK initialization in NW.js apps (#12846)
Looks like our browser extension check that blocks SDK initalization via `Sentry.init` also blocked initializing the SDK in NW.js apps. This PR adds a check for the `window.nw` property to handle this case. fixes #12668
1 parent a79c566 commit afef63e

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

packages/browser/src/sdk.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ function applyDefaultOptions(optionsArg: BrowserOptions = {}): BrowserOptions {
6363
type ExtensionProperties = {
6464
chrome?: Runtime;
6565
browser?: Runtime;
66+
nw?: unknown;
6667
};
6768
type Runtime = {
6869
runtime?: {
@@ -85,7 +86,11 @@ function shouldShowBrowserExtensionError(): boolean {
8586
const isDedicatedExtensionPage =
8687
!!runtimeId && WINDOW === WINDOW.top && extensionProtocols.some(protocol => href.startsWith(`${protocol}//`));
8788

88-
return !!runtimeId && !isDedicatedExtensionPage;
89+
// Running the SDK in NW.js, which appears like a browser extension but isn't, is also fine
90+
// see: https://github.com/getsentry/sentry-javascript/issues/12668
91+
const isNWjs = typeof windowWithMaybeExtension.nw !== 'undefined';
92+
93+
return !!runtimeId && !isDedicatedExtensionPage && !isNWjs;
8994
}
9095

9196
/**

packages/browser/test/unit/sdk.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ describe('init', () => {
142142
afterEach(() => {
143143
Object.defineProperty(WINDOW, 'chrome', { value: undefined, writable: true });
144144
Object.defineProperty(WINDOW, 'browser', { value: undefined, writable: true });
145+
Object.defineProperty(WINDOW, 'nw', { value: undefined, writable: true });
145146
});
146147

147148
it('logs a browser extension error if executed inside a Chrome extension', () => {
@@ -210,6 +211,18 @@ describe('init', () => {
210211
consoleErrorSpy.mockRestore();
211212
});
212213

214+
it("doesn't log a browser extension error if executed inside an NW.js environment", () => {
215+
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
216+
217+
Object.defineProperty(WINDOW, 'nw', { value: {} });
218+
219+
init(options);
220+
221+
expect(consoleErrorSpy).not.toHaveBeenCalled();
222+
223+
consoleErrorSpy.mockRestore();
224+
});
225+
213226
it("doesn't return a client on initialization error", () => {
214227
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
215228

0 commit comments

Comments
 (0)