Skip to main content

Caching

TanStack Router includes built-in caching for loader data, preventing unnecessary refetches and providing instant navigation. The caching system is automatic, configurable, and designed for optimal performance.

Why Caching Matters

Effective caching delivers major benefits:
  • Faster navigation - Revisiting routes is instant when cached
  • Reduced server load - Fewer redundant API requests
  • Better UX - No loading spinners for recently visited routes
  • Offline resilience - Cached data available without network
TanStack Router’s caching is inspired by React Query’s proven approach.

How Caching Works

Every route match has its own cache entry identified by:
  1. Route path - The matched route
  2. Path parameters - Values of $param segments
  3. Loader dependencies - Values from loaderDeps
Example cache keys:
Each unique combination gets its own cache entry.

Cache Lifecycle

Cached data goes through distinct phases:

Stale Time

How long data is considered fresh:
Within stale time:
  • Cached data used immediately
  • No loader execution
  • No loading states

Garbage Collection Time

How long to keep data in cache:
After GC time:
  • Data removed from cache
  • Next navigation runs loader fresh
From packages/router-core/src/router.ts:232-287, the defaults are:
  • defaultStaleTime: 0 (always stale)
  • defaultGcTime: 30 minutes

Global Cache Configuration

Set defaults for all routes:
Individual routes can override these defaults.

Per-Route Cache Configuration

Fine-tune caching per route:
Match cache strategy to data characteristics.

Cache Keys and Dependencies

Cache keys include loader dependencies from loaderDeps.

Basic Dependencies

Cache entries:
  • /posts + { page: 1, filter: undefined }
  • /posts + { page: 2, filter: undefined }
  • /posts + { page: 1, filter: 'react' }
Each combination is cached separately.

Path Parameters in Cache

Path params automatically part of cache key:
Cache entries:
  • /posts/$postId + { postId: '123' }
  • /posts/$postId + { postId: '456' }
No need to include params in loaderDeps.

Cache Invalidation

Manually invalidate cached data:

Invalidate All Routes

Useful after logout or global data changes.

Invalidate Specific Routes

Reloading Routes

Force reload without cache:

shouldReload

Control reload behavior per route:
From packages/router-core/src/route.ts:930-945, cause can be:
  • 'enter' - Navigating to this route
  • 'stay' - Already on this route
  • 'preload' - Prefetching

LRU Cache Implementation

TanStack Router uses an LRU (Least Recently Used) cache implementation from packages/router-core/src/lru-cache.ts:7-74:
This ensures memory usage stays bounded even with many route combinations.

Optimistic Updates

Update cache before server confirms:

Integration with React Query

Combine TanStack Router caching with React Query:
This pattern:
  • Router ensures data loaded before render
  • React Query handles cache, background refetch, mutations
  • Best of both worlds

Cache Persistence

Persist cache across page reloads:
Combine with sessionStorage or localStorage for persistence.

Cache Best Practices

Frequently changing data (live scores) should have short staleTime. Rarely changing data (user profiles) can have long staleTime.
Include search params in loaderDeps to cache different filter/sort/page combinations separately.
Don’t set gcTime too long - it wastes memory. 5-30 minutes is reasonable for most apps.
For advanced cache needs (background refetch, mutations, infinite queries), use React Query with Router.

Cache Debugging

Inspect cache state:
Use TanStack Router DevTools for visual cache inspection.

Memory Management

The router automatically manages memory:
  • LRU eviction - Oldest entries removed when cache is full
  • GC cleanup - Stale entries removed after gcTime
  • Match cleanup - Unused route matches removed on navigation
No manual memory management required.

Next Steps

Loaders

Understand how loaders populate the cache

Prefetching

Learn how prefetching interacts with cache

Routes

Configure cache options on routes

Search Params

Use search params in cache keys