From c201fc0d7cc91ba29e5a77f6e9783e1654c90774 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Fri, 15 Sep 2023 16:44:32 +0200 Subject: [PATCH] SEO: Provide redirect for new users (#3786) --- src/components/left/main/LeftMainHeader.tsx | 17 +++---- src/config.ts | 1 + src/index.tsx | 3 ++ src/lib/gramjs/client/downloadFile.ts | 3 +- src/util/permanentWebVersion.ts | 52 ++++++++++++++++++++- 5 files changed, 63 insertions(+), 13 deletions(-) diff --git a/src/components/left/main/LeftMainHeader.tsx b/src/components/left/main/LeftMainHeader.tsx index 9d0eb7e43..cb64d1dfb 100644 --- a/src/components/left/main/LeftMainHeader.tsx +++ b/src/components/left/main/LeftMainHeader.tsx @@ -20,6 +20,7 @@ import { IS_ELECTRON, IS_TEST, PRODUCTION_HOSTNAME, + WEB_VERSION_BASE, } from '../../../config'; import { INITIAL_PERFORMANCE_STATE_MAX, @@ -37,8 +38,7 @@ import buildClassName from '../../../util/buildClassName'; import captureEscKeyListener from '../../../util/captureEscKeyListener'; import { formatDateToString } from '../../../util/dateFormat'; import { getPromptInstall } from '../../../util/installPrompt'; -import { setPermanentWebVersion } from '../../../util/permanentWebVersion'; -import { clearWebsync } from '../../../util/websync'; +import { switchPermanentWebVersion } from '../../../util/permanentWebVersion'; import { IS_APP, IS_MAC_OS } from '../../../util/windowEnvironment'; import useAppLayout from '../../../hooks/useAppLayout'; @@ -99,7 +99,6 @@ type StateProps = const CLEAR_DATE_SEARCH_PARAM = { date: undefined }; const CLEAR_CHAT_SEARCH_PARAM = { id: undefined }; -const WEBK_VERSION_URL = 'https://web.telegram.org/k/'; const LeftMainHeader: FC = ({ shouldHideSearch, @@ -140,7 +139,6 @@ const LeftMainHeader: FC = ({ openChatByUsername, lockScreen, requestNextSettingsScreen, - skipLockOnUnload, openUrl, updatePerformanceSettings, } = getActions(); @@ -252,9 +250,7 @@ const LeftMainHeader: FC = ({ }); const handleSwitchToWebK = useLastCallback(() => { - setPermanentWebVersion('K'); - clearWebsync(); - skipLockOnUnload(); + switchPermanentWebVersion('K'); }); const handleOpenTipsChat = useLastCallback(() => { @@ -378,7 +374,7 @@ const LeftMainHeader: FC = ({ Switch to K Version @@ -394,9 +390,8 @@ const LeftMainHeader: FC = ({ )} ), [ - animationLevelValue, archivedUnreadChatsCount, canInstall, handleAnimationLevelChange, handleBugReportClick, lang, - handleChangelogClick, handleDarkModeToggle, handleOpenTipsChat, handleSelectSaved, handleSwitchToWebK, - onSelectArchived, onSelectContacts, onSelectSettings, theme, withOtherVersions, archiveSettings, + animationLevelValue, archiveSettings.isHidden, archivedUnreadChatsCount, canInstall, lang, onSelectArchived, + onSelectContacts, onSelectSettings, theme, withOtherVersions, ]); const searchContent = useMemo(() => { diff --git a/src/config.ts b/src/config.ts index 434fc8c8c..8e87ef629 100644 --- a/src/config.ts +++ b/src/config.ts @@ -6,6 +6,7 @@ export const RELEASE_DATETIME = process.env.RELEASE_DATETIME; export const PRODUCTION_HOSTNAME = 'web.telegram.org'; export const PRODUCTION_URL = 'https://web.telegram.org/a'; +export const WEB_VERSION_BASE = 'https://web.telegram.org/'; // Used to redirect to other versions export const IS_MOCKED_CLIENT = process.env.APP_MOCKED_CLIENT === '1'; export const IS_TEST = process.env.APP_ENV === 'test'; diff --git a/src/index.tsx b/src/index.tsx index 582ddac5c..15d9445ae 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -15,6 +15,7 @@ import { enableStrict, requestMutation } from './lib/fasterdom/fasterdom'; import { selectTabState } from './global/selectors'; import { establishMultitabRole, subscribeToMasterChange } from './util/establishMultitabRole'; import { requestGlobal, subscribeToMultitabBroadcastChannel } from './util/multitab'; +import { checkAndAssignPermanentWebVersion } from './util/permanentWebVersion'; import { onBeforeUnload } from './util/schedulers'; import updateWebmanifest from './util/updateWebmanifest'; import { IS_MULTITAB_SUPPORTED } from './util/windowEnvironment'; @@ -37,6 +38,8 @@ async function init() { if (!(window as any).isCompatTestPassed) return; + checkAndAssignPermanentWebVersion(); + if (IS_MULTITAB_SUPPORTED) { subscribeToMultitabBroadcastChannel(); diff --git a/src/lib/gramjs/client/downloadFile.ts b/src/lib/gramjs/client/downloadFile.ts index aaebbdc7c..2440b2bb4 100644 --- a/src/lib/gramjs/client/downloadFile.ts +++ b/src/lib/gramjs/client/downloadFile.ts @@ -1,5 +1,7 @@ import BigInt from 'big-integer'; +import type TelegramClient from './TelegramClient'; + import Deferred from '../../../util/Deferred'; import { Foreman } from '../../../util/foreman'; import errors from '../errors'; @@ -7,7 +9,6 @@ import Api from '../tl/api'; import { sleep } from '../Helpers'; import { getDownloadPartSize } from '../Utils'; -import type TelegramClient from './TelegramClient'; interface OnProgress { isCanceled?: boolean; diff --git a/src/util/permanentWebVersion.ts b/src/util/permanentWebVersion.ts index 321b251a0..6dafe1a9a 100644 --- a/src/util/permanentWebVersion.ts +++ b/src/util/permanentWebVersion.ts @@ -1,7 +1,57 @@ +import { getActions } from '../global'; + +import { PRODUCTION_HOSTNAME, WEB_VERSION_BASE } from '../config'; +import { clearWebsync } from './websync'; + +const SEARCH_ENGINE_REFERRERS = ['google', 'bing', 'duckduckgo', 'ya', 'yandex']; // Handled by the legacy version. Cannot be updated const PERMANENT_VERSION_KEY = 'kz_version'; const AVAILABLE_VERSIONS = ['Z', 'K'] as const; +const CLIENT_VERSION = 'Z'; +type AvailableVersions = typeof AVAILABLE_VERSIONS[number]; -export function setPermanentWebVersion(version: typeof AVAILABLE_VERSIONS[number]) { +function setPermanentWebVersion(version: AvailableVersions) { localStorage.setItem(PERMANENT_VERSION_KEY, JSON.stringify(version)); } + +export function getPermanentWebVersion(): AvailableVersions | undefined { + const version = localStorage.getItem(PERMANENT_VERSION_KEY); + if (version) { + return JSON.parse(version); + } + + return undefined; +} + +export function switchPermanentWebVersion(version: AvailableVersions) { + setPermanentWebVersion(version); + clearWebsync(); + getActions().skipLockOnUnload(); + window.location.assign(`${WEB_VERSION_BASE}${version}`); +} + +export function checkAndAssignPermanentWebVersion() { + if (window.location.hostname !== PRODUCTION_HOSTNAME) return; + + const referrer = document.referrer.toLowerCase(); + if (!SEARCH_ENGINE_REFERRERS.some((engine) => referrer.match(`(\\/\\/:|\\.)${engine}\\.`))) return; + + const currentVersion = getPermanentWebVersion(); + if (currentVersion) { + if (currentVersion !== CLIENT_VERSION) { + switchPermanentWebVersion(currentVersion); + } + return; + } + + const urlParams = new URLSearchParams(window.location.search); + const hasTest = (urlParams.get('test') ?? undefined) !== undefined; + const shouldRedirect = Math.random() < 0.5; + + if (hasTest || !shouldRedirect) { + setPermanentWebVersion('Z'); + return; + } + + switchPermanentWebVersion('K'); +}