tanstack-integration-best-practices

Integrate TanStack Query with TanStack Router for SSR-safe data fetching and caching.

Updated May 17, 2026
One-click install
npx skills add https://github.com/cenjie/skills --skill tanstack-integration-best-practices-cenjie
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: tanstack-integration-best-practices
Source: https://github.com/cenjie/skills/tree/main/skills/tanstack-integration
Command: npx skills add https://github.com/cenjie/skills --skill tanstack-integration-best-practices-cenjie

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Prevents conflicting caches, loading waterfalls, and broken SSR hydration when using TanStack Query alongside TanStack Router and TanStack Start by establishing a single source of truth and predictable data flow across loaders, components, and server renders.

Core Features & Use Cases

  • Pass a per-request QueryClient through the router context to enable SSR-safe caching and type-safe access in loaders and hooks.
  • Use loaders that call ensureQueryData so navigation preloads data into Query's cache and components can rely on useSuspenseQuery without loading waterfalls.
  • Configure the router with defaultPreloadStaleTime set to 0 and use setupRouterSsrQueryIntegration to automate dehydration, hydration, streaming support, and redirect handling.
  • Coordinate mutations and invalidation patterns so optimistic updates, background refetches, and cross-route cache sharing behave consistently.

Quick Start

Create a per-request QueryClient, pass it in the router context, call setupRouterSsrQueryIntegration, and use loaders with ensureQueryData to hydrate the Query cache before rendering.

Frequently Asked Questions about tanstack-integration-best-practices

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

FAQPage Schema
How do I integrate TanStack Query with TanStack Router for SSR hydration?

Integrate TanStack Query with TanStack Router by passing a per-request QueryClient through router context and invoking setupRouterSsrQueryIntegration to automate dehydration, hydration, and streaming SSR. This establishes a single source of truth for data fetching and caching across server renders.

Why does TanStack Router preloading cause loading waterfalls with React Query?

Loading waterfalls occur when router preloading and component-level fetching are uncoordinated. Fix this by configuring the router with defaultPreloadStaleTime set to 0 and using loaders that call ensureQueryData to hydrate the Query cache before components render with useSuspenseQuery.

Do I need a per-request QueryClient for TanStack Start SSR?

Yes, a per-request QueryClient is required for TanStack Start SSR. Passing it through the router context enables SSR-safe caching, prevents conflicting caches across requests, and provides type-safe access to the cache within loaders and hooks.

How do I handle mutation invalidation across routes with TanStack Query and Router?

Handle mutation invalidation by coordinating optimistic updates, background refetches, and cross-route cache sharing through the shared per-request QueryClient. This ensures consistent cache behavior and predictable data flow across navigation-driven loaders and components.

What's the best way to prevent broken SSR hydration with TanStack Start?

Prevent broken SSR hydration by using setupRouterSsrQueryIntegration to automate dehydration and hydration. Combine this with navigation-driven loaders calling ensureQueryData so the server and client share a single, predictable data flow without cache conflicts.

Does TanStack Router support useSuspenseQuery without loading waterfalls?

Yes, TanStack Router supports useSuspenseQuery without loading waterfalls when loaders use ensureQueryData to preload data into the Query cache. Components can then rely on the preloaded cache, preventing sequential fetches during navigation and rendering.