Skip to content

Commit 8283618

Browse files
committed
fix(browser): Avoid showing browser extension error message in non-window browser environments
1 parent d17f1be commit 8283618

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
@@ -72,7 +72,12 @@ type Runtime = {
7272
};
7373

7474
function shouldShowBrowserExtensionError(): boolean {
75-
const windowWithMaybeExtension = WINDOW as typeof WINDOW & ExtensionProperties;
75+
const windowWithMaybeExtension =
76+
typeof WINDOW.window !== 'undefined' && (WINDOW as typeof WINDOW & ExtensionProperties);
77+
if (!windowWithMaybeExtension) {
78+
// No need to show the error if we're not in a browser window environment (e.g. service workers)
79+
return false;
80+
}
7681

7782
const extensionKey = windowWithMaybeExtension.chrome ? 'chrome' : 'browser';
7883
const extensionObject = windowWithMaybeExtension[extensionKey];

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

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

148149
it('logs a browser extension error if executed inside a Chrome extension', () => {
@@ -223,6 +224,18 @@ describe('init', () => {
223224
consoleErrorSpy.mockRestore();
224225
});
225226

227+
it("doesn't log a browser extension error if the `window` object isn't defined", () => {
228+
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
229+
230+
Object.defineProperty(WINDOW, 'window', { value: undefined });
231+
232+
init(options);
233+
234+
expect(consoleErrorSpy).not.toHaveBeenCalled();
235+
236+
consoleErrorSpy.mockRestore();
237+
});
238+
226239
it("doesn't return a client on initialization error", () => {
227240
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
228241

0 commit comments

Comments
 (0)