From a06fabd4f319a59a407121fb370faf2d7f9b3d29 Mon Sep 17 00:00:00 2001 From: zubiden <19638254+zubiden@users.noreply.github.com> Date: Tue, 31 Mar 2026 11:29:30 +0200 Subject: [PATCH] Browser History: Fix early reset closing chats on reload (#6808) --- src/hooks/useHistoryBack.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/hooks/useHistoryBack.ts b/src/hooks/useHistoryBack.ts index f6b3e670d..eb57dbbbd 100644 --- a/src/hooks/useHistoryBack.ts +++ b/src/hooks/useHistoryBack.ts @@ -52,6 +52,7 @@ let isAlteringHistory = false; let deferredHistoryOperations: HistoryOperation[] = []; let deferredPopstateOperations: HistoryOperationState[] = []; let isSafariGestureAnimation = false; +let isHistoryInitialized = false; // Do not remove: used for history unit tests if (IS_TEST) { @@ -127,7 +128,14 @@ function resetHistory() { window.history.replaceState({ index: 0, historyUniqueSessionId }, '', PATH_BASE); } -resetHistory(); +function ensureHistoryInitialized() { + if (isHistoryInitialized) { + return; + } + + isHistoryInitialized = true; + resetHistory(); +} function cleanupClosed(alreadyClosedCount = 1) { let countClosed = alreadyClosedCount; @@ -164,6 +172,10 @@ function cleanupTrashedState() { } window.addEventListener('popstate', ({ state }: PopStateEvent) => { + if (!isHistoryInitialized) { + return; + } + if (isAlteringHistory) { isAlteringHistory = false; if (deferredPopstateOperations.length) { @@ -241,6 +253,8 @@ export default function useHistoryBack({ shouldResetUrlHash?: boolean; onBack: VoidFunction; }) { + ensureHistoryInitialized(); + const lastOnBack = useLastCallback(onBack); // Active index of the record