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
How Caching Works
Every route match has its own cache entry identified by:- Route path - The matched route
- Path parameters - Values of
$paramsegments - Loader dependencies - Values from
loaderDeps
Cache Lifecycle
Cached data goes through distinct phases:Stale Time
How long data is considered fresh:- Cached data used immediately
- No loader execution
- No loading states
Garbage Collection Time
How long to keep data in cache:- Data removed from cache
- Next navigation runs loader fresh
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:Per-Route Cache Configuration
Fine-tune caching per route:Cache Keys and Dependencies
Cache keys include loader dependencies fromloaderDeps.
Basic Dependencies
/posts+{ page: 1, filter: undefined }/posts+{ page: 2, filter: undefined }/posts+{ page: 1, filter: 'react' }
Path Parameters in Cache
Path params automatically part of cache key:/posts/$postId+{ postId: '123' }/posts/$postId+{ postId: '456' }
loaderDeps.
Cache Invalidation
Manually invalidate cached data:Invalidate All Routes
Invalidate Specific Routes
Reloading Routes
Force reload without cache:shouldReload
Control reload behavior per route: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 frompackages/router-core/src/lru-cache.ts:7-74:
Optimistic Updates
Update cache before server confirms:Integration with React Query
Combine TanStack Router caching with React Query:- Router ensures data loaded before render
- React Query handles cache, background refetch, mutations
- Best of both worlds
Cache Persistence
Persist cache across page reloads:sessionStorage or localStorage for persistence.
Cache Best Practices
Match staleTime to data volatility
Match staleTime to data volatility
Frequently changing data (live scores) should have short staleTime. Rarely changing data (user profiles) can have long staleTime.
Use loaderDeps for cache granularity
Use loaderDeps for cache granularity
Include search params in
loaderDeps to cache different filter/sort/page combinations separately.Set reasonable gcTime
Set reasonable gcTime
Don’t set gcTime too long - it wastes memory. 5-30 minutes is reasonable for most apps.
Invalidate related routes
Invalidate related routes
Consider React Query for complex caching
Consider React Query for complex caching
For advanced cache needs (background refetch, mutations, infinite queries), use React Query with Router.
Cache Debugging
Inspect cache state: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
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