Use when adding new error messages to React, or seeing "unknown error code" warnings.
npx skills add DeckardGer/tanstack-agent-skills --skill "tanstack-router-best-practices"
Install specific skill from multi-skill repository
# Description
TanStack Router best practices for type-safe routing, data loading, search params, and navigation. Activate when building React applications with complex routing needs.
# SKILL.md
name: tanstack-router-best-practices
description: TanStack Router best practices for type-safe routing, data loading, search params, and navigation. Activate when building React applications with complex routing needs.
TanStack Router Best Practices
Comprehensive guidelines for implementing TanStack Router patterns in React applications. These rules optimize type safety, data loading, navigation, and code organization.
When to Apply
- Setting up application routing
- Creating new routes and layouts
- Implementing search parameter handling
- Configuring data loaders
- Setting up code splitting
- Integrating with TanStack Query
- Refactoring navigation patterns
Rule Categories by Priority
| Priority | Category | Rules | Impact |
|---|---|---|---|
| CRITICAL | Type Safety | 4 rules | Prevents runtime errors and enables refactoring |
| CRITICAL | Route Organization | 5 rules | Ensures maintainable route structure |
| HIGH | Router Config | 1 rule | Global router defaults |
| HIGH | Data Loading | 6 rules | Optimizes data fetching and caching |
| HIGH | Search Params | 5 rules | Enables type-safe URL state |
| HIGH | Error Handling | 1 rule | Handles 404 and errors gracefully |
| MEDIUM | Navigation | 5 rules | Improves UX and accessibility |
| MEDIUM | Code Splitting | 3 rules | Reduces bundle size |
| MEDIUM | Preloading | 3 rules | Improves perceived performance |
| LOW | Route Context | 3 rules | Enables dependency injection |
Quick Reference
Type Safety (Prefix: ts-)
ts-register-routerβ Register router type for global inferencets-use-from-paramβ Usefromparameter for type narrowingts-route-context-typingβ Type route context with createRootRouteWithContextts-query-options-loaderβ Use queryOptions in loaders for type inference
Router Config (Prefix: router-)
router-default-optionsβ Configure router defaults (scrollRestoration, defaultErrorComponent, etc.)
Route Organization (Prefix: org-)
org-file-based-routingβ Prefer file-based routing for conventionsorg-route-tree-structureβ Follow hierarchical route tree patternsorg-pathless-layoutsβ Use pathless routes for shared layoutsorg-index-routesβ Understand index vs layout routesorg-virtual-routesβ Understand virtual file routes
Data Loading (Prefix: load-)
load-use-loadersβ Use route loaders for data fetchingload-loader-depsβ Define loaderDeps for cache controlload-ensure-query-dataβ Use ensureQueryData with TanStack Queryload-deferred-dataβ Split critical and non-critical dataload-error-handlingβ Handle loader errors appropriatelyload-parallelβ Leverage parallel route loading
Search Params (Prefix: search-)
search-validationβ Always validate search paramssearch-type-inheritanceβ Leverage parent search param typessearch-middlewareβ Use search param middlewaresearch-defaultsβ Provide sensible defaultssearch-custom-serializerβ Configure custom search param serializers
Error Handling (Prefix: err-)
err-not-foundβ Handle not-found routes properly
Navigation (Prefix: nav-)
nav-link-componentβ Prefer Link component for navigationnav-active-statesβ Configure active link statesnav-use-navigateβ Use useNavigate for programmatic navigationnav-relative-pathsβ Understand relative path navigationnav-route-masksβ Use route masks for modal URLs
Code Splitting (Prefix: split-)
split-lazy-routesβ Use .lazy.tsx for code splittingsplit-critical-pathβ Keep critical config in main route filesplit-auto-splittingβ Enable autoCodeSplitting when possible
Preloading (Prefix: preload-)
preload-intentβ Enable intent-based preloadingpreload-stale-timeβ Configure preload stale timepreload-manualβ Use manual preloading strategically
Route Context (Prefix: ctx-)
ctx-root-contextβ Define context at root routectx-before-loadβ Extend context in beforeLoadctx-dependency-injectionβ Use context for dependency injection
How to Use
Each rule file in the rules/ directory contains:
1. Explanation β Why this pattern matters
2. Bad Example β Anti-pattern to avoid
3. Good Example β Recommended implementation
4. Context β When to apply or skip this rule
Full Reference
See individual rule files in rules/ directory for detailed guidance and code examples.
# Supported AI Coding Agents
This skill is compatible with the SKILL.md standard and works with all major AI coding agents:
Learn more about the SKILL.md standard and how to use these skills with your preferred AI coding agent.