diff --git a/client/src/utils/handleBeta.js b/client/src/utils/handleBeta.js index 8a1fba468..34bdab626 100644 --- a/client/src/utils/handleBeta.js +++ b/client/src/utils/handleBeta.js @@ -1,37 +1,42 @@ -export const BETA_KEY = 'betaSwitchImex'; +export const BETA_KEY = "betaSwitchImex"; export const checkBeta = () => { - const cookie = document.cookie.split('; ').find(row => row.startsWith(BETA_KEY)); - return cookie ? cookie.split('=')[1] === 'true' : false; -} - + const cookie = document.cookie.split("; ").find((row) => row.startsWith(BETA_KEY)); + return cookie ? cookie.split("=")[1] === "true" : false; +}; export const setBeta = (value) => { - const domain = window.location.hostname.split('.').slice(-2).join('.'); - document.cookie = `${BETA_KEY}=${value}; path=/; domain=.${domain}`; -} + const domain = window.location.hostname.split(".").slice(-2).join("."); + document.cookie = `${BETA_KEY}=${value}; path=/; domain=.${domain}`; +}; export const handleBeta = () => { - // If the current host name does not start with beta or test, then we don't need to do anything. - if (window.location.hostname.startsWith('localhost')) { - console.log('Not on beta or test, so no need to handle beta.'); - return; - } + if (window.location.hostname.startsWith("localhost")) { + console.log("Not on beta or test, so no need to handle beta."); + return; + } - const isBeta = checkBeta(); + const isBeta = checkBeta(); + const currentHostName = window.location.hostname; - const currentHostName = window.location.hostname; + // Determine if the host name starts with "beta" or "www.beta" + const isBetaHost = currentHostName.startsWith("beta"); + const isBetaHostWithWWW = currentHostName.startsWith("www.beta"); - // Beta is enabled, but the current host name does start with beta. - if (isBeta && !currentHostName.startsWith('beta')) { - const href= `${window.location.protocol}//beta.${currentHostName}${window.location.pathname}${window.location.search}${window.location.hash}`; - window.location.replace(href); - } + // Handle beta redirection + if (isBeta && !isBetaHost && !isBetaHostWithWWW) { + // From non-beta to beta + const newHostName = currentHostName.startsWith("www.") + ? `www.beta.${currentHostName.replace(/^www\./, "")}` + : `beta.${currentHostName}`; + const href = `${window.location.protocol}//${newHostName}${window.location.pathname}${window.location.search}${window.location.hash}`; + window.location.replace(href); + } else if (!isBeta && (isBetaHost || isBetaHostWithWWW)) { + // From beta to non-beta + const newHostName = currentHostName.replace(/^www\./, "").replace(/^beta\./, "www."); + const href = `${window.location.protocol}//${newHostName}${window.location.pathname}${window.location.search}${window.location.hash}`; + window.location.replace(href); + } +}; - // Beta is not enabled, but the current host name does start with beta. - else if (!isBeta && currentHostName.startsWith('beta')) { - const href = `${window.location.protocol}//${currentHostName.replace('beta.', '')}${window.location.pathname}${window.location.search}${window.location.hash}`; - window.location.replace(href); - } -} export default handleBeta;