From e4045ccc8aa33eed4aec925e801e695d7112859b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 23 Jul 2026 21:02:44 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20keep=20the=20app=20alive?= =?UTF-8?q?=20when=20/users/me=20fails?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A non-401 failure on the bootstrap /users/me (e.g. a 429 once the API rate limit trips) threw out of an unguarded resource and blanked the whole tab with no way back but a fresh tab. Now the resource swallows every non-401 error into an error signal and resolves immediately, so it never re-throws during render nor suspends the shell. RouteGuard shows a translated alert with a Try-again button, a root ErrorBoundary backstops anything else, and a non-blocking background timer auto-retries after the server's Retry-After so the tab self-heals once the window rolls over. --- src/api/client/client.ts | 2 +- src/app.tsx | 31 ++++++++++++++++++++--- src/components/layout/route-guard.tsx | 13 ++++++++++ src/stores/auth-context.tsx | 36 ++++++++++++++++++++++++++- 4 files changed, 77 insertions(+), 5 deletions(-) diff --git a/src/api/client/client.ts b/src/api/client/client.ts index 77959c9..19a2432 100644 --- a/src/api/client/client.ts +++ b/src/api/client/client.ts @@ -172,7 +172,7 @@ const FIELD_LABELS: Record> = { username: { en: "Username", tr: "Kullanıcı adı" }, }; -function currentLocale(): Locale { +export function currentLocale(): Locale { if (typeof window === "undefined") return "en"; try { const saved = window.localStorage.getItem("hezarfen.locale"); diff --git a/src/app.tsx b/src/app.tsx index 7dcecef..c193b01 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -1,18 +1,43 @@ import { RouterProvider } from "@tanstack/solid-router"; -import { Suspense, lazy } from "solid-js"; +import { ErrorBoundary, Suspense, lazy } from "solid-js"; +import { currentLocale, formatApiError } from "@/api/client"; import { router } from "@/routes/router"; const RouterDevtools = import.meta.env.DEV ? lazy(() => import("@/router-devtools")) : undefined; +// Last-resort backstop for anything that throws during render. It lives above +// the i18n provider, so it mirrors the client's own bilingual-fallback idiom +// instead of using t(...); a real message + reload beats a blank document. +function RootErrorFallback(props: { error: unknown; reset: () => void }) { + const tr = currentLocale() === "tr"; + return ( +
+
+

{formatApiError(props.error)}

+ +
+
+ ); +} + export function App() { return ( - <> + }> {RouterDevtools ? ( ) : null} - + ); } diff --git a/src/components/layout/route-guard.tsx b/src/components/layout/route-guard.tsx index 1d60118..89c49f8 100644 --- a/src/components/layout/route-guard.tsx +++ b/src/components/layout/route-guard.tsx @@ -1,7 +1,9 @@ import { Navigate } from "@tanstack/solid-router"; import { type ParentProps, Show } from "solid-js"; import type { Role } from "@/api/client"; +import { formatApiError } from "@/api/client"; import { Alert } from "@/components/ui/alert"; +import { Button } from "@/components/ui/button"; import { PageSpinner } from "@/components/ui/page-spinner"; import { hasExactRole, roleInRange } from "@/lib/roles"; import { useAuth } from "@/stores/auth-context"; @@ -27,6 +29,16 @@ export function RouteGuard( return ( }> + +
+

{formatApiError(auth.error())}

+ +
+ + }> }> {(u) => ( )} +
); } diff --git a/src/stores/auth-context.tsx b/src/stores/auth-context.tsx index b123203..8d62a32 100644 --- a/src/stores/auth-context.tsx +++ b/src/stores/auth-context.tsx @@ -5,6 +5,7 @@ import { createEffect, createResource, createSignal, + onCleanup, useContext, } from "solid-js"; import { getMe } from "@/api/users"; @@ -16,21 +17,42 @@ import { usePreferences } from "@/stores/preferences-context"; type AuthContextValue = { user: Accessor; loading: Accessor; + /** Non-401 bootstrap failure. 401 is "logged out", not an error. */ + error: Accessor; refresh: () => Promise; logout: () => Promise; }; const AuthContext = createContext(); +/** Retry-After (seconds) for a transient 429/503, else null. */ +function retryAfterFor(err: unknown): number | null { + if ( + err instanceof ApiError && + (err.status === 429 || err.status === 503) && + err.retryAfter != null + ) { + return Math.min(Math.max(err.retryAfter, 0), 60); + } + return null; +} + export function AuthProvider(props: ParentProps) { const prefs = usePreferences(); const [override, setOverride] = createSignal(undefined); + const [error, setError] = createSignal(null); + // The fetcher swallows every failure into `error` state and resolves + // IMMEDIATELY: it must never stay `loading` (that suspends the whole shell + // and blanks the page) nor re-throw during render. Transient 429/503 waits + // happen off the resource, in the background timer below. const [me, { refetch }] = createResource(async () => { + setError(null); try { return await getMe(); } catch (err) { if (err instanceof ApiError && err.status === 401) return null; - throw err; + setError(err); + return null; } }); @@ -51,6 +73,17 @@ export function AuthProvider(props: ParentProps) { return result as User | null | undefined; }; + // Auto-heal a rate-limited bootstrap without blocking render: once `error` + // holds a 429/503 with a Retry-After, schedule one background refresh. The + // user is already looking at the error Alert while this waits; on success + // the app paints. Re-running the effect (new attempt) clears the old timer. + createEffect(() => { + const wait = retryAfterFor(error()); + if (wait == null) return; + const timer = setTimeout(() => void refresh(), wait * 1000); + onCleanup(() => clearTimeout(timer)); + }); + const logout = async () => { try { await postLogout(); @@ -62,6 +95,7 @@ export function AuthProvider(props: ParentProps) { const value: AuthContextValue = { user, loading: () => me.loading && override() === undefined, + error: () => (override() === undefined ? error() : null), refresh, logout, };