DeckardGer

tanstack-router-best-practices

4
1
# Install this skill:
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 inference
  • ts-use-from-param β€” Use from parameter for type narrowing
  • ts-route-context-typing β€” Type route context with createRootRouteWithContext
  • ts-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 conventions
  • org-route-tree-structure β€” Follow hierarchical route tree patterns
  • org-pathless-layouts β€” Use pathless routes for shared layouts
  • org-index-routes β€” Understand index vs layout routes
  • org-virtual-routes β€” Understand virtual file routes

Data Loading (Prefix: load-)

  • load-use-loaders β€” Use route loaders for data fetching
  • load-loader-deps β€” Define loaderDeps for cache control
  • load-ensure-query-data β€” Use ensureQueryData with TanStack Query
  • load-deferred-data β€” Split critical and non-critical data
  • load-error-handling β€” Handle loader errors appropriately
  • load-parallel β€” Leverage parallel route loading

Search Params (Prefix: search-)

  • search-validation β€” Always validate search params
  • search-type-inheritance β€” Leverage parent search param types
  • search-middleware β€” Use search param middleware
  • search-defaults β€” Provide sensible defaults
  • search-custom-serializer β€” Configure custom search param serializers

Error Handling (Prefix: err-)

  • err-not-found β€” Handle not-found routes properly
  • nav-link-component β€” Prefer Link component for navigation
  • nav-active-states β€” Configure active link states
  • nav-use-navigate β€” Use useNavigate for programmatic navigation
  • nav-relative-paths β€” Understand relative path navigation
  • nav-route-masks β€” Use route masks for modal URLs

Code Splitting (Prefix: split-)

  • split-lazy-routes β€” Use .lazy.tsx for code splitting
  • split-critical-path β€” Keep critical config in main route file
  • split-auto-splitting β€” Enable autoCodeSplitting when possible

Preloading (Prefix: preload-)

  • preload-intent β€” Enable intent-based preloading
  • preload-stale-time β€” Configure preload stale time
  • preload-manual β€” Use manual preloading strategically

Route Context (Prefix: ctx-)

  • ctx-root-context β€” Define context at root route
  • ctx-before-load β€” Extend context in beforeLoad
  • ctx-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.