tanstack-router-best-practices
Original:🇺🇸 English
Not Translated
TanStack Router best practices for type-safe routing, data loading, search params, and navigation. Activate when building React applications with complex routing needs.
3installs
Sourcepedronauck/skills
Added on
NPX Install
npx skill4agent add pedronauck/skills tanstack-router-best-practicesSKILL.md Content
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 type for global inference
ts-register-router - — Use
ts-use-from-paramparameter for type narrowingfrom - — Type route context with createRootRouteWithContext
ts-route-context-typing - — Use queryOptions in loaders for type inference
ts-query-options-loader
Router Config (Prefix: router-
)
router-- — Configure router defaults (scrollRestoration, defaultErrorComponent, etc.)
router-default-options
Route Organization (Prefix: org-
)
org-- — Prefer file-based routing for conventions
org-file-based-routing - — Follow hierarchical route tree patterns
org-route-tree-structure - — Use pathless routes for shared layouts
org-pathless-layouts - — Understand index vs layout routes
org-index-routes - — Understand virtual file routes
org-virtual-routes
Data Loading (Prefix: load-
)
load-- — Use route loaders for data fetching
load-use-loaders - — Define loaderDeps for cache control
load-loader-deps - — Use ensureQueryData with TanStack Query
load-ensure-query-data - — Split critical and non-critical data
load-deferred-data - — Handle loader errors appropriately
load-error-handling - — Leverage parallel route loading
load-parallel
Search Params (Prefix: search-
)
search-- — Always validate search params
search-validation - — Leverage parent search param types
search-type-inheritance - — Use search param middleware
search-middleware - — Provide sensible defaults
search-defaults - — Configure custom search param serializers
search-custom-serializer
Error Handling (Prefix: err-
)
err-- — Handle not-found routes properly
err-not-found
Navigation (Prefix: nav-
)
nav-- — Prefer Link component for navigation
nav-link-component - — Configure active link states
nav-active-states - — Use useNavigate for programmatic navigation
nav-use-navigate - — Understand relative path navigation
nav-relative-paths - — Use route masks for modal URLs
nav-route-masks
Code Splitting (Prefix: split-
)
split-- — Use .lazy.tsx for code splitting
split-lazy-routes - — Keep critical config in main route file
split-critical-path - — Enable autoCodeSplitting when possible
split-auto-splitting
Preloading (Prefix: preload-
)
preload-- — Enable intent-based preloading
preload-intent - — Configure preload stale time
preload-stale-time - — Use manual preloading strategically
preload-manual
Route Context (Prefix: ctx-
)
ctx-- — Define context at root route
ctx-root-context - — Extend context in beforeLoad
ctx-before-load - — Use context for dependency injection
ctx-dependency-injection
How to Use
Each rule file in the directory contains:
rules/- Explanation — Why this pattern matters
- Bad Example — Anti-pattern to avoid
- Good Example — Recommended implementation
- Context — When to apply or skip this rule
Full Reference
See individual rule files in directory for detailed guidance and code examples.
rules/