tanstack-router
Skill266 starsChanged 10 months agoApache-2.0View on GitHub
Type-safe, file-based routing for React with TanStack Router. Use when defining routes with createFileRoute, validating search params with Zod, writing route loaders, setting up auth guards with beforeLoad, integrating TanStack Query into loaders, or configuring router context and preloading.
5 other projects use a version of this.
Adopted by
Most recently taken up by
- zkmake/skillstheir own version, first seen Oct 3, 2026
- louisbrulenaudet/monorepo-templatetheir own version, first seen Oct 2, 2026
- Spardutti/claude-skillstheir own version, first seen Aug 31, 2026
- lobomfz/skillstheir own version, first seen Apr 28, 2026
- Aver005/agent-skillstheir own version, first seen Feb 14, 2026
Other versions5 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 5
- louisbrulenaudet/monorepo-template★ 18same name
- Spardutti/claude-skills★ 3same name
- Aver005/agent-skills★ 2same name
- lobomfz/skills★ 2same name
- zkmake/skills★ 2same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository264 other files it gives its agents.
- accessibility-a11ySkill
- alpine-jsSkill
- analytics-data-analysisSkill
- android-developmentSkill
- angular-developmentSkill
- angularSkill
- anime-jsSkill
- anthropic-claude-developmentSkill
- api-developmentSkill
- apollo-graphqlSkill
- aspnet-coreSkill
- astroSkill
- auth0-authenticationSkill
- autogen-developmentSkill
- automl-hyperparameter-optimizationSkill
- aws-developmentSkill
- azureSkill
- backend-developmentSkill
- bash-scriptingSkill
- beautifulsoup-parsingSkill
- bitbucket-workflowSkill
- blazorSkill
- blender-python-addonSkill
- blockchainSkill
- bootstrapSkill
- business-central-developmentSkill
- cheerio-parsingSkill
- chrome-extension-developmentSkill
- ci-cd-best-practicesSkill
- clean-architectureSkill
- clean-codeSkill
- clerk-authenticationSkill
- cloudflare-developmentSkill
- computer-vision-opencvSkill
- convexSkill
- cppSkill
- c-sharpSkill
- cssSkill
- cypressSkill
- data-analysis-jupyterSkill
- data-analystSkill
- data-jupyter-pythonSkill
- deep-learning-pythonSkill
- deep-learning-pytorchSkill
- deep-learningSkill
- deno-typescriptSkill
- design-systemsSkill
- devopsSkill
- django-pythonSkill
- django-rest-api-developmentSkill
- dockerSkill
- dotnetSkill
- drizzle-ormSkill
- drupal-developmentSkill
- elasticsearch-best-practicesSkill
- electron-developmentSkill
- elixirSkill
- embedded-stm32Skill
- esbuild-bundlerSkill
- ethereumSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
10 rules. 0 are common, 1 is distinctive, and 9 are very specific to this project.
0
Common
Most of the 3 edited versions we read keep these.
1
Distinctive
This author's own choices. Worth a look.
9
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 23
Prefer file-based routing with @tanstack/router-vite-plugin (or @tanstack/router-plugin/vite) for scalability over manually constructed …
- Very specific: line 24
Always define routes with createFileRoute (leaf/nested routes) or createRootRoute / createRootRouteWithContext (root).
- Distinctive: line 26
Search params are first-class state — always define their schema with Zod (or another standard-schema validator) so they are typed and …
- Very specific: line 68
Always define search params with Zod and validateSearch.
- Very specific: line 96
Use <Link> for internal navigation — never a raw <a href>, which triggers a full page reload and loses client-side routing state.
- Very specific: line 97
Always pass typed params and search.
- Very specific: line 106
Put the QueryClient in router context so loaders can prefetch and cache through TanStack Query rather than duplicating fetch logic.
- Very specific: line 107
Use ensureQueryData (not fetchQuery) in loaders so an already-cached query is reused instead of refetched.
- Very specific: line 153
Prefer throw redirect(...) over imperative navigation inside components — it works during SSR, preloading, and client navigation alike.
- Very specific: line 158
Use React.lazy (or the router's built-in code-splitting via .lazy() route files) for route component code splitting on large apps.
“Common” is measured: kept by at least 6 in 10 of the 3 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
TanStack Router
TanStack Router is a fully type-safe router for React that treats routes, loaders, params, and search params as first-class typed data rather than untyped strings.
Workflow for Adding a New Route
- Create the route file — Add a file under
src/routes/following the file-based routing convention (see below). - Define the route — Export
RoutefromcreateFileRoute('/path')({...})withcomponent, and optionallyloader,validateSearch,beforeLoad,errorComponent, andpendingComponent. - Validate search params — If the route reads query params, define a Zod schema and pass it as
validateSearch. - Load data — Fetch data in
loader, not in a componentuseEffect; integrate with TanStack Query viaqueryClient.ensureQueryDatawhen caching is needed. - Guard access — Add
beforeLoadchecks (e.g. auth) thatthrow redirect({ to: '/login' })when a precondition fails. - Link to the route — Navigate with
<Link to="/path" params={...} search={...}>so the compiler validates params and search at every call site. - Regenerate the route tree — Ensure
routeTree.gen.tsis regenerated (automatic under the Vite plugin's dev server / build) before running or building the app.
Core Principles
- TanStack Router is 100% type-safe — lean on TypeScript generics for params, search params, and loader data instead of manual casting.
- Prefer file-based routing with
@tanstack/router-vite-plugin(or@tanstack/router-plugin/vite) for scalability over manually constructed route trees. - Always define routes with
createFileRoute(leaf/nested routes) orcreateRootRoute/createRootRouteWithContext(root). - Route data loading belongs in
loaderfunctions, not in componentuseEffect— this enables preloading, parallel loading, and pending/error states. - Search params are first-class state — always define their schema with Zod (or another standard-schema validator) so they are typed and validated on every read.
File-Based Route Conventions
src/routes/
__root.tsx ← Root layout
index.tsx ← / route
posts/
index.tsx ← /posts
$postId.tsx ← /posts/:postId (dynamic segment)
_layout.tsx ← Layout route (no path segment)
_auth/ ← Pathless auth layout group
dashboard.tsx- A leading underscore on a segment (
_layout,_auth) creates a pathless layout route used purely for grouping/shared UI. - A
$prefix ($postId) marks a dynamic path segment, matchingRoute.useParams(). index.tsxinside a folder matches the folder's own path with no additional segment.
Route Definition
export const Route = createFileRoute('/posts/$postId')({
loader: async ({ params }) => fetchPost(params.postId),
component: PostComponent,
errorComponent: ({ error }) => <ErrorBanner message={error.message} />,
pendingComponent: () => <PostSkeleton />,
})
function PostComponent() {
const post = Route.useLoaderData() // type-safe
const { postId } = Route.useParams() // type-safe
return <div>{post.title}</div>
}errorComponentrenders when the loader throws;pendingComponentrenders while the loader is in flight (subject todefaultPendingMs).- Loader return values and thrown errors are both fully typed and flow into
useLoaderData()anderrorComponentrespectively.
Type-Safe Search Params
- Always define search params with Zod and
validateSearch. - Access them with
Route.useSearch()— never readwindow.location.searchorURLSearchParamsdirectly, which bypasses type safety and validation.
const searchSchema = z.object({
page: z.number().int().min(1).default(1),
q: z.string().optional(),
})
export const Route = createFileRoute('/search')({
validateSearch: searchSchema,
component: SearchPage,
})
function SearchPage() {
const { page, q } = Route.useSearch()
const navigate = Route.useNavigate()
return (
<button onClick={() => navigate({ search: (prev) => ({ ...prev, page: page + 1 }) })}>
Next page
</button>
)
}Navigation
- Use
<Link>for internal navigation — never a raw<a href>, which triggers a full page reload and loses client-side routing state. - Always pass typed
paramsandsearch; the TypeScript compiler catches missing or mistyped route params at build time.
<Link to="/posts/$postId" params={{ postId: '123' }}>View Post</Link>
<Link to="/search" search={{ page: 1, q: 'react' }}>Search</Link>Loaders + TanStack Query Integration
- Put the
QueryClientin router context so loaders can prefetch and cache through TanStack Query rather than duplicating fetch logic. - Use
ensureQueryData(notfetchQuery) in loaders so an already-cached query is reused instead of refetched.
export const Route = createFileRoute('/posts')({
loader: ({ context: { queryClient } }) =>
queryClient.ensureQueryData(postsQueryOptions()),
component: PostsPage,
})
function PostsPage() {
const { data: posts } = useSuspenseQuery(postsQueryOptions())
return <PostList posts={posts} />
}Router Context for Dependency Injection
// __root.tsx
interface RouterContext {
queryClient: QueryClient
auth: AuthState
}
export const Route = createRootRouteWithContext<RouterContext>()({
component: RootLayout,
})
// main.tsx
const router = createRouter({ routeTree, context: { queryClient, auth } })- Router context flows down to every route's
loaderandbeforeLoadvia thecontextargument, giving each route typed access to shared dependencies without prop drilling or global singletons.
Auth Guards
export const Route = createFileRoute('/_auth/dashboard')({
beforeLoad: ({ context }) => {
if (!context.auth.isAuthenticated) throw redirect({ to: '/login' })
},
component: Dashboard,
})beforeLoadruns before the loader and before the component renders, making it the right place for auth checks, feature flag gates, and redirects.- Prefer
throw redirect(...)over imperative navigation inside components — it works during SSR, preloading, and client navigation alike.
Performance
- Set
defaultPreload: 'intent'on the router so links prefetch their route's data on hover/focus, making navigation feel instant. - Use
React.lazy(or the router's built-in code-splitting via.lazy()route files) for route component code splitting on large apps. - Install
@tanstack/router-devtoolsand render<TanStackRouterDevtools />in development to inspect route matches, pending states, and cache.
Common Mistakes
- Fetching data with
useEffectinside a route component instead of aloader, which loses preloading and creates render-then-fetch waterfalls. - Reading raw
window.location.searchinstead ofRoute.useSearch(), bypassing the search param schema and type safety. - Performing auth checks only inside components (
useEffectredirect) instead ofbeforeLoad, allowing a flash of protected content before redirecting. - Forgetting to add new route params/search fields to
<Link>call sites — let the compiler catch it rather than testing manually.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

