diff --git a/.changeset/wicked-avocados-chew.md b/.changeset/wicked-avocados-chew.md new file mode 100644 index 0000000000..f1bd2e93d8 --- /dev/null +++ b/.changeset/wicked-avocados-chew.md @@ -0,0 +1,5 @@ +--- +"@rainbow-me/rainbowkit": patch +--- + +Resolved an issue in development where browser detection would throw an error if `navigator.userAgent` was unavailable in the browser. diff --git a/packages/rainbowkit/src/utils/browsers.ts b/packages/rainbowkit/src/utils/browsers.ts index f553310925..8a61684640 100644 --- a/packages/rainbowkit/src/utils/browsers.ts +++ b/packages/rainbowkit/src/utils/browsers.ts @@ -1,6 +1,7 @@ export function isSafari(): boolean { return ( typeof navigator !== 'undefined' && + typeof navigator.userAgent !== 'undefined' && /Version\/([0-9._]+).*Safari/.test(navigator.userAgent) // Source: https://github.com/DamonOehlman/detect-browser/blob/master/src/index.ts ); } @@ -25,15 +26,17 @@ export enum BrowserType { } export function getBrowser(): BrowserType { - if (typeof navigator === 'undefined') return BrowserType.Browser; - const ua = navigator.userAgent.toLowerCase(); + // bail out if `navigator` or `navigator.userAgent` is not available + if (typeof navigator === 'undefined' || Object.keys(navigator).length === 0) + return BrowserType.Browser; + const ua = navigator.userAgent?.toLowerCase(); // @ts-ignore - brave is not in the navigator type if (navigator.brave?.isBrave) return BrowserType.Brave; - if (ua.indexOf('edg/') > -1) return BrowserType.Edge; - if (ua.indexOf('op') > -1) return BrowserType.Opera; + if (ua?.indexOf('edg/') > -1) return BrowserType.Edge; + if (ua?.indexOf('op') > -1) return BrowserType.Opera; if (isArc()) return BrowserType.Arc; - if (ua.indexOf('chrome') > -1) return BrowserType.Chrome; - if (ua.indexOf('firefox') > -1) return BrowserType.Firefox; + if (ua?.indexOf('chrome') > -1) return BrowserType.Chrome; + if (ua?.indexOf('firefox') > -1) return BrowserType.Firefox; if (isSafari()) return BrowserType.Safari; return BrowserType.Browser; }