Skip to content

Commit

Permalink
ES-1550: improve browser compatibility check (#346)
Browse files Browse the repository at this point in the history
* fix: fix browser compatibility issue

Signed-off-by: bunsy-0900 <[email protected]>

* fix: fixes check min browser version

Signed-off-by: bunsy-0900 <[email protected]>

* fix: change variable name

Signed-off-by: bunsy-0900 <[email protected]>

---------

Signed-off-by: bunsy-0900 <[email protected]>
  • Loading branch information
bunsy-0900 authored Sep 11, 2024
1 parent 6b7824e commit a9364b0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
1 change: 1 addition & 0 deletions signup-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@types/react": "^18.2.32",
"@types/react-dom": "^18.2.14",
"axios": "^1.6.0",
"bowser": "^2.11.0",
"buffer": "^6.0.3",
"class-variance-authority": "^0.7.0",
"clsx": "^2.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
import Bowser, { BROWSER_MAP } from "bowser";
import { findKey } from "lodash";

import { BROWSER_MIN_COMPATIBILITY } from "~constants/browsers";
import { compareVersions } from "~utils/browser";

/**
* Checks if the browser is compatible based on its version.
*
* This function gets the user agent string and defines a regular expression for each browser to extract its version.
* It then loops over the regular expressions, and if it finds a match in the user agent string, it compares the extracted version with the minimum compatible version for that browser.
* If the browser version is greater than or equal to the minimum compatible version, it returns true.
* If no match is found for any browser, it returns false.
* @param {Object} minBrowserCompatibility - An object containing the minimum compatible version for each browser. The default value is BROWSER_MIN_COMPATIBILITY.
* @returns {boolean} - True if the browser is compatible, false otherwise.
*/
export const checkBrowserCompatible = (
minBrowserCompatibility: { [key: string]: string } = BROWSER_MIN_COMPATIBILITY
) => {
const userAgent = navigator.userAgent;
const browserVersionRegex = {
edge: /Edg\/(\d+\.\d+\.\d+\.\d+)/,
safari: /Version\/(\d+\.\d+)/,
firefox: /Firefox\/(\d+\.\d+)/,
chrome: /Chrome\/(\d+\.\d+\.\d+\.\d+)/,
};

for (const [browser, regex] of Object.entries(browserVersionRegex)) {
const browserVersionMatch = userAgent.match(regex);
if (browserVersionMatch) {
return compareVersions(
browserVersionMatch[1],
minBrowserCompatibility[browser]
);
}
const browserInfo = Bowser.parse(window.navigator.userAgent);

const browserName = findKey(
BROWSER_MAP,
(b) => b === browserInfo.browser.name
);

if (!browserName) return false;

const minBrowserVersion = minBrowserCompatibility[browserName];
const currentBrowserVersion = browserInfo.browser.version;

if (!minBrowserVersion || !currentBrowserVersion) return false;

const isCompatible = compareVersions(currentBrowserVersion, minBrowserVersion);

if (isCompatible) {
return true;
}

return false;
};

0 comments on commit a9364b0

Please sign in to comment.