compositions-router-query

Integrates TanStack Query caching with TanStack Router loaders and SSR hydration.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires @tanstack/react-query, @tanstack/react-router, @tanstack/react-router-ssr-query.

What problem does it solve? Coordinating TanStack Query's cache with TanStack Router's loader system is error-prone: Router's built-in preload cache can serve stale data, module-level QueryClient singletons leak data between SSR requests, and misused prefetch calls block navigation. This Skill provides the correct integration patterns. ## Core Features & Use Cases - Router Context Setup: Inject QueryClient into router context via createRootRouteWithContext, with SSR-safe per-request QueryClient creation inside the createRouter factory. - Loader + Component Pattern: Use ensureQueryData in loaders with useSuspenseQuery in components so data is cached before render without loading flashes, plus prefetchQuery for non-blocking streaming of non-critical data. - SSR Dehydration/Hydration: Configure setupRouterSsrQueryIntegration for automatic dehydration, hydration, streaming, and redirect handling, or wire dehydrate/hydrate manually. - Use Case: Building a TanStack Start dashboard where critical user data blocks navigation via ensureQueryData while analytics stream in via prefetchQuery, with QueryClient isolated per server request. ## Quick Start Set up TanStack Query with my TanStack Router app so loaders prefetch data and SSR hydration works correctly.

Frequently Asked Questions about compositions-router-query

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

FAQPage Schema
How do I use TanStack Query with TanStack Router loaders?

Pass the QueryClient through router context, call context.queryClient.ensureQueryData(queryOptions) in the route loader, and read the data with useSuspenseQuery in the component. The loader populates the cache before render while the component subscribes for background updates.

Why is my TanStack Query data stale when using TanStack Router preloading?

Router's built-in preload cache has a 30-second default staleTime that short-circuits Query's freshness logic. Set defaultPreloadStaleTime: 0 in createRouter so Query controls data freshness during preloading.

How do I set up TanStack Query SSR with TanStack Router?

Create the QueryClient inside the createRouter factory so each server request gets an isolated instance, then call setupRouterSsrQueryIntegration from @tanstack/react-router-ssr-query for automatic dehydration, hydration, and streaming. Alternatively wire dehydrate and hydrate callbacks manually.

What is the difference between ensureQueryData and prefetchQuery in loaders?

ensureQueryData returns cached data or fetches and awaits it, blocking navigation for critical data. prefetchQuery fires without awaiting, letting non-critical queries stream during SSR while the component shows a loading state via useQuery.

Why does createRootRouteWithContext need double parentheses?

createRootRouteWithContext<Type>() is a factory that returns a function; the second call receives the route options. Writing createRootRouteWithContext<Type>()({ component }) correctly declares the context type and route configuration.