Query cache and loading
For Admin panel only projects, the login is /, the home page is /dashboard, and other panel pages omit the /admin prefix. Components use @/components and @/components/ui. API endpoints such as /api/admin/users/export keep their paths. Examples below use the combined public website + admin panel layout; generated standalone guides use the app’s actual paths.
Refresh data after changes
After a successful mutation, invalidate the affected query. For example, call this in a Client Component after creating or editing a user:
// src/hooks/use-refresh-users.ts
"use client";
import { useQueryClient } from "@tanstack/react-query";
import { useTRPC } from "@/trpc/client";
export function useRefreshUsers() {
const queryClient = useQueryClient();
const trpc = useTRPC();
return () =>
queryClient.invalidateQueries({
queryKey: trpc.admin.users.pathKey(),
});
}
The built-in user forms already do this. Path invalidation also matches administrator-scoped query keys.
For your own tRPC mutations, use TanStack’s useMutation with the procedure’s mutationOptions(), then invalidate or update the relevant cache in its success handler.
Freshness and revalidation
| Query | Freshness |
|---|---|
| Shared default | 30 seconds |
| Admin user list/details | 1 minute |
Fresh data is reused. Stale data can revalidate on mount, window focus, or reconnect while the previous result stays visible.
staleTime does not start a polling timer. Add polling only when the product needs periodic updates, and choose retries/freshness for the feature.
Use automatic revalidation and targeted invalidation for routine updates. Show a contextual Try again action when an error needs recovery.
Clear data after identity changes
Cancel and clear browser queries after successful login, logout, or account switching, before full navigation:
// src/lib/sign-out.ts
"use client";
import { authClient } from "@/lib/auth-client";
import { resetQueryCache } from "@/trpc/query-client";
export async function signOut() {
const { error } = await authClient.signOut();
if (error) {
return { error: "Unable to sign out. Please try again." };
}
await resetQueryCache();
window.location.assign("/");
}
Handle auth errors before clearing data or navigating. Apply the same reset after successful sign-in/sign-up and identity or role switches.
Private calls always revalidate authorization on the server. A browser cache is never proof of permission.
Build loading and error states
Render static page structure immediately: headings, descriptions, fields, filters, table headers, and known pagination controls.
| State | What to show |
|---|---|
| First read, no data | Matching skeletons for unresolved values or rows |
| Empty result | The empty state; loading is complete |
| Background refresh | The current data |
| Recoverable error | Contextual error and retry, preserving usable content |
For filter/page changes within the same identity, preserve prior rows with placeholderData or keepPreviousData. Do not carry private placeholders across identities.
Background fetching should not add spinners, loading text, dimming, overlays, or controls disabled solely by isFetching.
The generated AGENTS.md includes these conventions for new screens. Client Components can prerender static HTML too.
References: TanStack Query defaults, window-focus refetching.