From 6338d1dfe726a2c8f6848a5c2cb009c0b634d611 Mon Sep 17 00:00:00 2001 From: Usman Baig Date: Sat, 7 Mar 2026 19:55:16 +0100 Subject: [PATCH] fix: prevent infinite reload loop on stale build recovery Use sessionStorage guard so the hard reload only fires once. If the reload doesn't fix it (CDN still serving stale JS), fall through gracefully instead of looping forever. --- app/auth/callback/page.tsx | 11 +++++++++-- lib/auth/context.tsx | 22 +++++++++++++++++++--- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/app/auth/callback/page.tsx b/app/auth/callback/page.tsx index 933fdce..3b630ae 100644 --- a/app/auth/callback/page.tsx +++ b/app/auth/callback/page.tsx @@ -26,8 +26,15 @@ function AuthCallbackContent() { try { result = await exchangeAuthCode(code, codeVerifier, redirectUri) } catch { - // * Stale build — cached JS has old Server Action hashes. Hard reload to fix. - window.location.reload() + // * Stale build — cached JS has old Server Action hashes. Hard reload once to fix. + const key = 'pulse_reload_for_stale_build' + if (!sessionStorage.getItem(key)) { + sessionStorage.setItem(key, '1') + window.location.reload() + return + } + sessionStorage.removeItem(key) + setError('Something went wrong. Please try logging in again.') return } if (result.success && result.user) { diff --git a/lib/auth/context.tsx b/lib/auth/context.tsx index d9adb92..b6a9944 100644 --- a/lib/auth/context.tsx +++ b/lib/auth/context.tsx @@ -135,10 +135,19 @@ export function AuthProvider({ children }: { children: React.ReactNode }) { let session: Awaited> = null try { session = await getSessionAction() + sessionStorage.removeItem('pulse_reload_for_stale_build') } catch { // * Stale build — browser has cached JS with old Server Action hashes. - // * Force a hard reload to fetch fresh bundles from the server. - window.location.reload() + // * Force a hard reload once to fetch fresh bundles. Guard prevents infinite loop. + const key = 'pulse_reload_for_stale_build' + if (!sessionStorage.getItem(key)) { + sessionStorage.setItem(key, '1') + window.location.reload() + return + } + sessionStorage.removeItem(key) + // * Reload didn't fix it — treat as no session + setLoading(false) return } @@ -153,7 +162,14 @@ export function AuthProvider({ children }: { children: React.ReactNode }) { try { session = await getSessionAction() } catch { - window.location.reload() + const key = 'pulse_reload_for_stale_build' + if (!sessionStorage.getItem(key)) { + sessionStorage.setItem(key, '1') + window.location.reload() + return + } + sessionStorage.removeItem(key) + setLoading(false) return } }