Skip to main content
TanStack Router provides powerful data loading capabilities through loaders, allowing you to fetch data before rendering routes.

Overview

Route loaders run before a route renders, ensuring data is available when components mount. Key features:
  • Parallel loading - Multiple loaders run concurrently
  • Type-safe - Full TypeScript support for loaded data
  • Caching - Built-in caching with configurable strategies
  • Preloading - Prefetch data before navigation
  • Streaming - Support for deferred data loading

Basic loader

Define a loader using the loader option:

Loader context

Loaders receive a context object with useful properties:

beforeLoad hook

Run code before the loader, useful for authentication or redirects:

Parallel vs sequential loading

Parallel loading (default)

Loaders run concurrently:

Sequential loading

Access parent data in child loaders:

Caching

Control how long loader data is cached:

Preloading

Prefetch data before navigation:
Preload modes:
  • false - No preloading
  • "intent" - Preload on hover or focus
  • "viewport" - Preload when link enters viewport
  • "render" - Preload immediately when link renders

Deferred data

Stream data that takes longer to load:

Error handling

Handle loader errors with error boundaries:

Invalidation

Invalidate cached loader data:

Loader dependencies

Specify explicit dependencies for cache invalidation:

Best practices

Loaders should only fetch data. Move business logic to separate functions.
Always pass the abort signal to fetch calls for proper cancellation.
Set appropriate staleTime and gcTime to reduce unnecessary requests.
Provide helpful error messages and recovery options.

Next steps

Error handling

Learn error boundary patterns

Loaders concept

Deep dive into loader architecture