Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ui): [core] allow browserDetection to be run in ssr context #2248

Merged
merged 1 commit into from
Apr 10, 2024
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
5 changes: 5 additions & 0 deletions .changeset/thick-worms-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lion/ui': patch
---

[core] allow browserDetection to be run in ssr context
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,7 @@ module.exports = {
[path.resolve('./scripts/eslint-resolver.cjs')]: {},
},
},
env: {
es2020: true,
},
};
1 change: 0 additions & 1 deletion packages/singleton-manager/src/SingletonManagerClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const sym = Symbol.for('lion::SingletonManagerClassStorage');
* In the future, we can just use globalThis directly
* (for now, we're backwards compatible with browsers that still only use window, since we don't know all contexts singleton-manager is used in).
*/
// eslint-disable-next-line no-undef
const globalThisOrWindow = globalThis || window;
export class SingletonManagerClass {
constructor() {
Expand Down
44 changes: 27 additions & 17 deletions packages/ui/components/core/src/browserDetection.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import { isServer } from 'lit';

/**
* From https://stackoverflow.com/questions/4565112/javascript-how-to-find-out-if-the-user-browser-is-chrome
* @param {string} [flavor]
* @param {string} [flavor='google-chrome']
*/
function checkChrome(flavor = 'google-chrome') {
const isChromium = /** @type {window & { chrome?: boolean}} */ (window).chrome;
if (isServer) {
return flavor === 'google-chrome';
}

const isChromium = /** @type {window & { chrome?: boolean}} */ (globalThis).chrome;

if (flavor === 'chromium') {
return isChromium;
}
const winNav = window.navigator;
const vendorName = winNav.vendor;
const isOpera = typeof (/** @type {window & { opr?: boolean}} */ (window).opr) !== 'undefined';
const isIEedge = winNav.userAgent.indexOf('Edge') > -1;
const isIOSChrome = winNav.userAgent.match('CriOS');
const winNav = globalThis.navigator;
const vendorName = winNav?.vendor;
const isOpera =
typeof (/** @type {window & { opr?: boolean}} */ (globalThis).opr) !== 'undefined';
// @ts-ignore
const isIEedge = globalThis.userAgent?.indexOf('Edge') > -1;
// @ts-ignore
const isIOSChrome = globalThis.userAgent?.match('CriOS');

if (flavor === 'ios') {
return isIOSChrome;
Expand All @@ -31,18 +41,18 @@ function checkChrome(flavor = 'google-chrome') {
}

export const browserDetection = {
isIE11: /Trident/.test(window.navigator.userAgent),
isIE11: /Trident/.test(globalThis.navigator?.userAgent),
isChrome: checkChrome(),
isIOSChrome: checkChrome('ios'),
isChromium: checkChrome('chromium'),
isFirefox: navigator.userAgent.toLowerCase().indexOf('firefox') > -1,
isMac: navigator.appVersion.indexOf('Mac') !== -1,
isIOS: /iPhone|iPad|iPod/i.test(navigator.userAgent),
isFirefox: globalThis.navigator?.userAgent.toLowerCase().indexOf('firefox') > -1,
isMac: globalThis.navigator?.appVersion?.indexOf('Mac') !== -1,
isIOS: /iPhone|iPad|iPod/i.test(globalThis.navigator?.userAgent),
isMacSafari:
navigator.vendor &&
navigator.vendor.indexOf('Apple') > -1 &&
navigator.userAgent &&
navigator.userAgent.indexOf('CriOS') === -1 &&
navigator.userAgent.indexOf('FxiOS') === -1 &&
navigator.appVersion.indexOf('Mac') !== -1,
globalThis.navigator?.vendor &&
globalThis.navigator?.vendor.indexOf('Apple') > -1 &&
globalThis.navigator?.userAgent &&
globalThis.navigator?.userAgent.indexOf('CriOS') === -1 &&
globalThis.navigator?.userAgent.indexOf('FxiOS') === -1 &&
globalThis.navigator?.appVersion.indexOf('Mac') !== -1,
};
Loading