SEO: Provide redirect for new users (#3786)

This commit is contained in:
Alexander Zinchuk 2023-09-15 16:44:32 +02:00
parent 31403702fe
commit c201fc0d7c
5 changed files with 63 additions and 13 deletions

View File

@ -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<OwnProps & StateProps> = ({
shouldHideSearch,
@ -140,7 +139,6 @@ const LeftMainHeader: FC<OwnProps & StateProps> = ({
openChatByUsername,
lockScreen,
requestNextSettingsScreen,
skipLockOnUnload,
openUrl,
updatePerformanceSettings,
} = getActions();
@ -252,9 +250,7 @@ const LeftMainHeader: FC<OwnProps & StateProps> = ({
});
const handleSwitchToWebK = useLastCallback(() => {
setPermanentWebVersion('K');
clearWebsync();
skipLockOnUnload();
switchPermanentWebVersion('K');
});
const handleOpenTipsChat = useLastCallback(() => {
@ -378,7 +374,7 @@ const LeftMainHeader: FC<OwnProps & StateProps> = ({
<MenuItem
icon="K"
isCharIcon
href={WEBK_VERSION_URL}
href={`${WEB_VERSION_BASE}k`}
onClick={handleSwitchToWebK}
>
Switch to K Version
@ -394,9 +390,8 @@ const LeftMainHeader: FC<OwnProps & StateProps> = ({
)}
</>
), [
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(() => {

View File

@ -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';

View File

@ -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();

View File

@ -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;

View File

@ -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');
}