code-enforce-route-data

Enforce consistent TanStack Start route data prefetching with React Query.

22|3|Updated Jul 28, 2024
One-click install
npx skills add https://github.com/webdevcody/go-mailing-list --skill code-enforce-route-data
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: code-enforce-route-data
Source: https://github.com/webdevcody/go-mailing-list/tree/main/.claude/skills/code-enforce-route-data
Command: npx skills add https://github.com/webdevcody/go-mailing-list --skill code-enforce-route-data

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

It prevents cache drift and SSR/client mismatches by enforcing a single, consistent pattern for how TanStack Start routes prefetch server data and how components read from React Query.

Core Features & Use Cases

  • Colocated queryOptions + hooks: For each feature, routes import a single source of truth from src/queries so the queryKey and queryFn stay identical between loader and component.
  • Loader prefetch with ensureQueryData: Routes pre-warm the React Query cache using queryClient.ensureQueryData (including Promise.all for multiple queries) so the first paint has data and there is no loading flash.
  • Intent prefetch via <Link> navigation: In-app navigation uses @tanstack/react-router <Link> (including Button-as-child patterns) to warm loader cache before the user clicks, aligning navigation behavior with data requirements.

Quick Start

Use this skill when adding or editing any TanStack Start route that reads server data, ensuring you add a loader that calls ensureQueryData with the colocated queryOptions and read the same query in the route component.

Frequently Asked Questions about code-enforce-route-data

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

FAQPage Schema
How do I prevent SSR hydration drift when prefetching route data in TanStack Start?

Prevent SSR hydration drift in TanStack Start by aligning server-side loader prefetch with React Query reads using colocated queryOptions and ensureQueryData, ensuring the server and client share an identical data source to avoid mismatches.

What's the best way to prefetch React Query cache in a TanStack Start route loader?

Prefetch the React Query cache in a TanStack Start route loader by calling queryClient.ensureQueryData with your colocated queryOptions. Use Promise.all to pre-warm multiple queries simultaneously so the first paint has data without loading flashes.

Why does my TanStack Start component show different data than the server loader?

TanStack Start components show different data than the server loader when the prefetch and read patterns diverge, causing cache drift. Enforce a single source of truth by importing colocated queryOptions and hooks from src/queries in both the loader and the component.

How do I prefetch route data when queries depend on URL search params in TanStack Start?

Prefetch route data when queries depend on URL search params by wiring loaderDeps and validateSearch in your TanStack Start route. This passes validated search parameters to your loader, allowing ensureQueryData to prefetch the correct colocated queryOptions.

Can I use useLoaderData to read server data in TanStack Start with React Query?

You cannot use useLoaderData to read server data when enforcing this TanStack Start pattern. You must read data through React Query hooks using colocated queryOptions, while the loader strictly uses ensureQueryData to warm the cache and avoid hydration mismatches.

How do I warm route data cache before navigation using TanStack Router?

Warm route data cache before navigation by using the @tanstack/react-router Link component for intent prefetching. This warms the loader cache before the user clicks, aligning navigation behavior with data requirements to eliminate loading flashes.