Skip to main content

Routes

Routes are the building blocks of TanStack Router. Each route represents a URL pattern and defines what should happen when that URL is matched - including what to load, what to render, and how to handle errors.

Route Definition

Routes are defined using the createRoute or createFileRoute functions from the router package.

Code-Based Routes

File-Based Routes

File-based routes don’t need getParentRoute - the parent is inferred from the file path.

Route Options

Routes accept a comprehensive set of options to control their behavior.

Path and Identification

Routes can have either:
  • A path - Creates a routable URL segment
  • An id - Creates a layout/wrapper route with no URL segment

Component Options

Define what renders at different states:
All component options support lazy loading. Use the lazy method to code-split components.

Path Parameters

Path parameters capture dynamic segments from the URL.

Basic Parameters

In the route definition at packages/router-core/src/route.ts:169-172, parameters are typed based on the path pattern:

Parsing Parameters

Transform and validate parameters:

Optional Parameters

Make parameters optional with the {- syntax:

Search Parameter Validation

Validate and type search parameters using any validation library:

With Zod

Manual Validation

The validateSearch option is defined in packages/router-core/src/route.ts:928.

Route Context

Provide data to child routes via context:

Lifecycle Hooks

Routes have several lifecycle hooks that run at different times.

beforeLoad

Runs before the route loads. Perfect for authentication checks:
From packages/router-core/src/route.ts:969-993, beforeLoad receives:
  • context - Accumulated context from parent routes
  • location - Current location object
  • params - Path parameters
  • search - Validated search parameters
  • abortController - Signal for cancellation
If beforeLoad throws, the route loader will not run and navigation will be cancelled.

Context Function

Provides synchronous context to the route:

Loading States

Control how loading states are displayed:
This prevents flashing loading states for fast requests.

Error Handling

Handle errors at the route level:

Code Splitting

Lazy load route components and loaders:

Stale Time and Caching

Control when route data is considered fresh:

Best Practices

The beforeLoad hook is perfect for auth checks because it runs before the loader, preventing unnecessary data fetching for unauthorized users.
Always validate search parameters with a schema library like Zod. This provides type safety and runtime validation in one step.
Use the lazy method to split large components. This reduces initial bundle size and improves performance.
Every route should have an errorComponent to gracefully handle failures and provide recovery options.

Next Steps

Loaders

Learn how to load data for your routes

Navigation

Discover navigation patterns and APIs

Type Safety

Master type-safe routing patterns

Search Params

Deep dive into search parameter handling