router-core-data-loading

Configure TanStack Router loaders, SWR caching, pending states, and deferred data loading.

Updated Dec 21, 2025
One-click install
npx skills add https://github.com/Angael/veles --skill router-core-data-loading-angael
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: router-core-data-loading
Source: https://github.com/Angael/veles/tree/main/.agents/skills/router-core-data-loading
Command: npx skills add https://github.com/Angael/veles --skill router-core-data-loading-angael

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires @tanstack/react-router.

What problem does it solve? Fetching and caching route data in TanStack Router involves subtle behaviors—loaders run on the client by default, staleTime defaults to 0, and search params do not automatically reach loaders—which leads to stale data, unnecessary refetches, and runtime crashes when developers apply server-side or React-hook assumptions. ## Core Features & Use Cases - Loader and Cache Configuration: Set up route loaders with loaderDeps cache keys, staleTime/gcTime SWR caching, and pendingComponent states with pendingMs/pendingMinMs timing. - Context and Dependency Injection: Use createRootRouteWithContext to inject auth, API clients, and hook values into loaders and beforeLoad without calling hooks inside route functions. - Deferred Loading and Invalidation: Return unawaited promises from loaders, render them with the Await component, and re-run loaders after mutations with router.invalidate. - Use Case: When building a paginated posts page, declare loaderDeps from validated search params so the loader refetches only when offset or limit changes, while unrelated params like sort direction leave the cache untouched. ## Quick Start Ask the AI to add a loader with loaderDeps, pendingComponent, and error handling to a TanStack Router route that fetches paginated data from an API.

Frequently Asked Questions about router-core-data-loading

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fetch data in a TanStack Router route loader?

Define a loader function on the route that returns fetched data, then consume it in the component with Route.useLoaderData or getRouteApi for code-split files. Loaders run on the client by default, so use fetch or API calls rather than server-only code.

How do I make a TanStack Router loader refetch when search params change?

Use loaderDeps to declare which validated search params form the cache key, such as offset and limit. The loader receives them via deps and reloads when they change, regardless of staleTime. Return only the relevant params, not the whole search object.

Does TanStack Router cache loader data automatically?

Yes, TanStack Router has built-in stale-while-revalidate caching keyed on the pathname plus loaderDeps. Defaults are staleTime of 0, preloadStaleTime of 30 seconds, and gcTime of 30 minutes, all configurable per route or globally.

Why does my TanStack Router loader crash when using Node.js APIs?

Loaders are client-first and execute in the browser by default, so fs, database queries, and other server-only code fail. Use fetch against an API, or move server logic into TanStack Start server functions when using SSR.

Can I use React hooks inside a TanStack Router loader or beforeLoad?

No, loaders and beforeLoad are not React components, so calling hooks inside them crashes. Inject hook values through router context by passing them to RouterProvider's context prop and reading them from the loader's context argument.

How do I show deferred or slow data in TanStack Router?

Return an unawaited promise from the loader alongside awaited critical data, then render it with the Await component and a fallback. The page renders immediately with fast data while the deferred promise resolves in the background.