From 399c01b3a6ccec69894c135a01a86dd4731c4835 Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Wed, 8 Apr 2026 21:18:32 +0200 Subject: [PATCH] feat(web): preload user identity query at root route Fire the UserIdentity query in the root route's beforeLoad so it's already in the Apollo cache before any component calling useAuth() renders. Co-Authored-By: Claude Opus 4.6 (1M context) --- webui2/src/lib/auth.tsx | 14 +++++++------- webui2/src/routes/__root.tsx | 5 +++++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/webui2/src/lib/auth.tsx b/webui2/src/lib/auth.tsx index 239c3e71e626d9cdc61018b91156aa5b0c0727e4..3cdcb531dbe1faa2d0a27ff60bcb48d49c0c59a9 100644 --- a/webui2/src/lib/auth.tsx +++ b/webui2/src/lib/auth.tsx @@ -1,12 +1,12 @@ // auth.tsx — current user hook for the webui. // -// Fetches the user identity from git config via GraphQL. Apollo handles -// deduplication and caching, so no Provider/Context is needed. +// The UserIdentity query is preloaded in the root route loader and consumed +// via useSuspenseQuery, so useAuth() always returns a resolved user. import { gql } from "@apollo/client"; -import { useQuery } from "@apollo/client/react"; +import { useSuspenseQuery } from "@apollo/client/react"; -const USER_IDENTITY_QUERY = gql` +export const USER_IDENTITY_QUERY = gql` query UserIdentity { repository { userIdentity { @@ -32,9 +32,9 @@ export interface AuthUser { login: string | null; } -export function useAuth(): { user: AuthUser | null; loading: boolean } { - const { data, loading } = useQuery<{ repository: { userIdentity: AuthUser | null } }>( +export function useAuth(): { user: AuthUser } { + const { data } = useSuspenseQuery<{ repository: { userIdentity: AuthUser } }>( USER_IDENTITY_QUERY, ); - return { user: data?.repository?.userIdentity ?? null, loading }; + return { user: data.repository.userIdentity }; } diff --git a/webui2/src/routes/__root.tsx b/webui2/src/routes/__root.tsx index 5972a00013556b12ae30971fb659b54afdde4aae..e327b6797a9c75e38f8034505569ed65023d3b38 100644 --- a/webui2/src/routes/__root.tsx +++ b/webui2/src/routes/__root.tsx @@ -5,12 +5,17 @@ import { Shell } from "@/components/layout/shell"; import { Button } from "@/components/ui/button"; import { ButtonLink } from "@/components/ui/button-link"; import type { preloadQuery } from "@/lib/apollo"; +import { USER_IDENTITY_QUERY } from "@/lib/auth"; export interface RouterContext { preloadQuery: typeof preloadQuery; } export const Route = createRootRouteWithContext()({ + async loader({ context }) { + const ref = context.preloadQuery(USER_IDENTITY_QUERY); + await context.preloadQuery.toPromise(ref); + }, component: Shell, errorComponent: ErrorPage, });