|
7 | 7 | type ErrorComponentProps, |
8 | 8 | useLocation, |
9 | 9 | useNavigate, |
| 10 | + useRouter, |
10 | 11 | } from "@tanstack/react-router"; |
11 | 12 | import { useEffect, useEffectEvent, useRef, useState } from "react"; |
12 | 13 |
|
@@ -188,6 +189,36 @@ function RootRouteErrorView({ error, reset }: ErrorComponentProps) { |
188 | 189 | const message = errorMessage(error); |
189 | 190 | const details = errorDetails(error); |
190 | 191 |
|
| 192 | + // The root error boundary is shown when beforeLoad's startup probe |
| 193 | + // (resolveInitialServerAuthGateState -> fetchSessionState) throws. That failure |
| 194 | + // is frequently transient (backend still starting, a flaky session call), but |
| 195 | + // the boundary persists until a manual reload. Auto-retry when the window |
| 196 | + // regains focus/visibility or the network returns, so a recovered backend |
| 197 | + // un-sticks the app instead of stranding the user here. reset() only resets the |
| 198 | + // boundary; router.invalidate() re-runs beforeLoad. See #3513. |
| 199 | + const router = useRouter(); |
| 200 | + const retryingRef = useRef(false); |
| 201 | + useEffect(() => { |
| 202 | + const retry = () => { |
| 203 | + if (retryingRef.current) return; |
| 204 | + retryingRef.current = true; |
| 205 | + void router.invalidate().finally(() => { |
| 206 | + retryingRef.current = false; |
| 207 | + }); |
| 208 | + }; |
| 209 | + const retryIfVisible = () => { |
| 210 | + if (document.visibilityState === "visible") retry(); |
| 211 | + }; |
| 212 | + document.addEventListener("visibilitychange", retryIfVisible); |
| 213 | + window.addEventListener("focus", retry); |
| 214 | + window.addEventListener("online", retry); |
| 215 | + return () => { |
| 216 | + document.removeEventListener("visibilitychange", retryIfVisible); |
| 217 | + window.removeEventListener("focus", retry); |
| 218 | + window.removeEventListener("online", retry); |
| 219 | + }; |
| 220 | + }, [router]); |
| 221 | + |
191 | 222 | return ( |
192 | 223 | <div className="relative flex min-h-screen items-center justify-center overflow-hidden bg-background px-4 py-10 text-foreground sm:px-6"> |
193 | 224 | <div className="pointer-events-none absolute inset-0 opacity-80"> |
|
0 commit comments