Skip to main content
TanStack Router provides a flexible system for creating custom route types and extending the core routing behavior. This allows you to adapt the router to specialized use cases and domain requirements.

Overview

While most applications use the standard Route and RootRoute classes, you can create custom route types for:
  • Domain-specific route requirements
  • Specialized validation or authorization logic
  • Custom route option types
  • Integration with other libraries or frameworks
  • Type-safe API route definitions

Route Class Anatomy

Understanding the route class structure is essential for customization:

Creating Route Factories

Route factories help create routes with shared configuration:

Basic Route Factory

Route Factory with Validation

Extending the Route Class

Create custom route classes by extending the base Route class:

Custom Route Options

Extend route options for domain-specific needs:

Custom Root Routes

Create specialized root routes for different application sections:

Type-Safe API Routes

Create fully typed API route definitions:

Route Hooks Customization

Create custom hooks for your route types:

Route Middleware Pattern

Implement a middleware pattern for routes:

Framework-Specific Route Types

Create routes optimized for specific frameworks:

Route Composition

Compose routes from smaller pieces:

Validating Custom Routes

Ensure your custom routes maintain type safety:

Best Practices

  1. Extend, don’t replace - Build on top of base Route class
  2. Maintain type safety - Ensure custom routes preserve TypeScript types
  3. Document custom behavior - Clearly document what your custom routes do
  4. Test thoroughly - Custom routes need comprehensive testing
  5. Keep it simple - Only customize when standard routes are insufficient
  6. Use factories - Prefer factory functions over class extension when possible
  7. Composition over inheritance - Use composition patterns for flexibility

Common Patterns

Layout Routes

Resource Routes