react-web-advanced
React Web Advanced: TanStack Router, Start & Virtual
Web-specific patterns for React apps built on the TanStack Router + Start + Virtual stack.
This skill extends react-advanced (core cross-platform patterns). Read that skill first for
React Query, XState, Zustand, Zod, TanStack Form, and TanStack Table conventions.
Web Architecture
The web stack adds three layers on top of the shared core:
| Layer | Library | Responsibility |
|---|---|---|
| Routing + URL state | TanStack Router | Type-safe navigation, search params, route loaders |
| Full-stack boundary | TanStack Start | Server functions (createServerFn), SSR, streaming |
| Large lists | TanStack Virtual | Virtualized rendering for 1000+ items |
The golden rule: queryOptions as single source of truth
Define query options once, import everywhere — loaders, components, invalidation:
// queries/posts.ts
export const postsQueryOptions = queryOptions({
queryKey: ["posts"],
queryFn: fetchPosts,
staleTime: 30_000,
});
Router + React Query wiring
The router receives QueryClient as context — the single integration point:
const router = createRouter({
routeTree,
context: { queryClient },
defaultPreload: "intent",
defaultPreloadStaleTime: 0, // Let React Query manage staleness
});
declare module "@tanstack/react-router" {
interface Register {
router: typeof router;
}
}
defaultPreloadStaleTime: 0 is intentional — without it, the router caches loader results
independently, causing React Query's staleTime to be ignored during preloads.
Route Loader + React Query Pattern
ensureQueryData for blocking, prefetchQuery for non-blocking
export const Route = createFileRoute('/posts/$postId')({
loader: async ({ context: { queryClient }, params }) => {
// Fire-and-forget secondary data
queryClient.prefetchQuery(commentsQueryOptions(params.postId))
// Block route render until critical data is ready
await queryClient.ensureQueryData(postQueryOptions(params.postId))
},
component: PostDetail,
})
function PostDetail() {
const { postId } = Route.useParams()
// Data guaranteed in cache — instant, no loading state
const { data: post } = useSuspenseQuery(postQueryOptions(postId))
return <h1>{post.title}</h1>
}
Avoid waterfall requests
Prefetch all independent data in route loaders using Promise.all:
loader: async ({ context: { queryClient }, params }) => {
await Promise.all([
queryClient.ensureQueryData(userQueryOptions(params.id)),
queryClient.ensureQueryData(permissionsQueryOptions(params.id)),
]);
// Fire-and-forget for non-critical
queryClient.prefetchQuery(activityQueryOptions(params.id));
};
- Never fetch data in
useEffectthat could go in a route loader - Parent and child route loaders run concurrently by default
Performance Patterns
React Compiler (React 19+)
With the compiler enabled:
- Do not manually wrap components in
React.memo - Do not manually use
useMemo/useCallbackfor performance - Do write idiomatic React — the compiler handles memoization
- Do ensure code follows Rules of React (no mutation during render)
Manual useMemo/useCallback remain useful only for controlling effect dependencies.
Suspense boundaries placement
- Route-level boundaries: use
pendingComponent/errorComponenton route definitions - Within routes: wrap non-blocking data in
<Suspense>individually - Group co-dependent queries under one
<Suspense>so they resolve together - Independent queries get separate
<Suspense>boundaries
Code splitting
- Split routes using
.lazy()or.lazy.tsxfiles — critical config (loader, params) stays in the main file, component/UI splits into the lazy file - Use
React.lazyfor heavy on-demand components (rich editors, charts) - Machine definitions auto-split since they are separate
.tsfiles
File Organization
src/
routes/ # TanStack Router file-based routes
__root.tsx # Root layout, router context type
(auth)/ # Route group — no URL impact
(app)/
users/
$userId.tsx
$userId.lazy.tsx # Component-only code split
-components/ # "-" prefix excludes from route tree
queries/ # queryOptions definitions — one file per entity
mutations/ # useMutation wrappers
machines/ # XState machine definitions (pure TS, no React)
stores/ # Zustand stores
serverFns/ # TanStack Start server functions
components/
ui/ # Design system primitives
shared/ # Cross-feature shared components
lib/
query-client.ts # QueryClient singleton
router.ts # Router singleton
test/
setup.ts # Vitest setup
test-utils.tsx # renderWithProviders
mocks/handlers.ts # MSW handlers
Key conventions:
- Route-specific components use
-prefix directories to avoid route tree inclusion - Pathless route groups
(name)/for organization without URL impact .lazy.tsxfiles export component/pendingComponent/errorComponent only- Co-locate test files next to source (
.test.ts/.test.tsx)
Common Pitfalls
-
Wrong property order in
createFileRoute— must bevalidateSearch -> loaderDeps -> beforeLoad -> loaderfor TypeScript inference. Install@tanstack/eslint-plugin-routerwithcreate-route-property-orderrule. -
Returning entire search in
loaderDeps— invalidates cache on any param change. Extract only the deps the loader uses. -
preload="intent"without React Query cache — preloaded data is discarded if user doesn't navigate. Combine with React Query'sensureQueryDatafor cache persistence. -
Not registering the router — without
declare module Register, everything isany. -
Forgetting
<Suspense>around<Await>— runtime error without a Suspense ancestor. -
defaultPreloadStaleTimenot set to 0 — Router's default staleTime overrides React Query's staleTime during preloads, causing stale data to be served. -
useLoaderDatainnotFoundComponent— not valid. UseRoute.useParams()or pass data viathrow notFound({ data: ... }). -
Building all pages before verifying auth flow — implement and verify the auth flow first (login → session cookie → protected route guard → redirect) end-to-end before building feature pages. Auth integration bugs hide behind each other (7+ layers deep). See
references/ssr-auth.md. -
defaultPreload: "intent"triggering auth checks on hover — with intent preloading,beforeLoadfires on every link hover. IfbeforeLoaddoes auth validation, every hover triggers a session check. Disable intent preloading during auth debugging, or make auth checks cache-aware (queryClient.getQueryData(sessionKey)before fetching). -
Blanket
invalidateQueries()cascading through auth hooks —queryClient.invalidateQueries()with no key filter invalidates everything, including session queries. Auth reactive hooks refetch, components re-render,beforeLoadre-runs. Always scope invalidation to the specific entity query key. Seereferences/integration.mdfor query key namespacing patterns. -
Auth nanostores re-rendering on every API mutation — libraries like Better Auth use nanostores internally. Every successful mutation toggles signals, causing all
useSession()/useActiveOrganization()subscribers to re-render synchronously. Combined with TanStack Router, this creates infinite re-render loops. Replace auth hooks with React Query wrappers. Seereferences/better-auth-start.md. -
beforeLoadre-runs on every client-side navigation — if the session check insidebeforeLoadcalls a server function without React Query caching, every link click triggers a server round-trip. UseensureQueryDatawithstaleTimeto serve from cache. Seereferences/ssr-auth.md.
Reference Files
Read the relevant reference file when working with a specific library:
| File | When to read |
|---|---|
references/router.md |
Routing, search params, loaders, code splitting, navigation |
references/start.md |
Server functions, SSR, middleware, deployment |
references/virtual.md |
Virtualization, dynamic heights, infinite scroll, grids |
references/integration.md |
Router+Query wiring, auth guards, query key namespacing, org data |
references/ssr-auth.md |
SSR cookie auth, Vite proxy, CORS, route guards, FOUC, CF Workers |
references/better-auth-start.md |
Better Auth + TanStack Start: signals, RQ wrapper, org state |
references/testing.md |
Testing Router routes, renderWithProviders |
More from trancong12102/agentskills
deps-dev
Look up the latest stable version of any open-source package across npm, PyPI, Go, Cargo, Maven, and NuGet. Use when the user asks 'what's the latest version of X', 'what version should I use', 'is X deprecated', 'how outdated is my package.json/requirements.txt/Cargo.toml', or needs version numbers for adding or updating dependencies. Also covers pinning versions, checking if packages are maintained, or comparing installed vs latest versions. Do NOT use for private/internal packages or for looking up documentation (use context7).
151council-review
Multi-perspective code review that synthesizes findings from multiple reviewers into a unified report. Use when the user asks to review code changes, audit a diff, check code quality, review a PR, review commits, or review uncommitted changes. Also covers 'code review', 'review my changes', 'check this before I merge', or wanting multiple perspectives on code. Do not use for documentation/markdown review or trivial single-line changes.
94oracle
Deep analysis and expert reasoning. Use when the user asks for 'oracle', 'second opinion', architecture analysis, elusive bug debugging, impact assessment, security reasoning, refactoring strategy, or trade-off evaluation — problems that benefit from deep, independent reasoning. Do not use for simple factual questions, code generation, code review (use council-review), or tasks needing file modifications.
92context7
Fetch up-to-date documentation for any open-source library or framework. Use when the user asks to look up docs, check an API, find code examples, or verify how a feature works — especially with a specific library name, version migration, or phrases like 'what's the current way to...' or 'the API might have changed'. Also covers setup and configuration docs. Do NOT use for general programming concepts, internal project code, or version lookups (use deps-dev).
86conventional-commit
Generates git commit messages following Conventional Commits 1.0.0 specification with semantic types (feat, fix, etc.), optional scope, and breaking change annotations. Use when committing code changes or creating commit messages.
58react-advanced
Advanced React patterns and conventions for data fetching, tables, forms, state machines, client state management, schema validation, and testing. Use when tackling complex React problems — not simple component questions, but multi-concern tasks like server-driven tables with filtering, multi-step wizards, eliminating useEffect, Suspense architecture, choosing between state management approaches, or designing data flow across server/client/URL/form state. Do not use for web-specific routing/SSR or React Native-specific navigation/performance.
45