Skip to content
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
7 changes: 6 additions & 1 deletion packages/browser/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ type Runtime = {
};

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

const extensionKey = windowWithMaybeExtension.chrome ? 'chrome' : 'browser';
const extensionObject = windowWithMaybeExtension[extensionKey];
Expand Down
13 changes: 13 additions & 0 deletions packages/browser/test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ describe('init', () => {
Object.defineProperty(WINDOW, 'chrome', { value: undefined, writable: true });
Object.defineProperty(WINDOW, 'browser', { value: undefined, writable: true });
Object.defineProperty(WINDOW, 'nw', { value: undefined, writable: true });
Object.defineProperty(WINDOW, 'window', { value: WINDOW, writable: true });
});

it('logs a browser extension error if executed inside a Chrome extension', () => {
Expand Down Expand Up @@ -229,6 +230,18 @@ describe('init', () => {
consoleErrorSpy.mockRestore();
});

it("doesn't log a browser extension error if the `window` object isn't defined", () => {
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});

Object.defineProperty(WINDOW, 'window', { value: undefined });

init(options);

expect(consoleErrorSpy).not.toHaveBeenCalled();

consoleErrorSpy.mockRestore();
});

it("doesn't return a client on initialization error", () => {
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});

Expand Down