Prefetching
Prefetching loads route code and data before users navigate to a route. This creates instant-feeling navigation by eliminating loading states and code splitting delays.Why Prefetching Matters
Prefetching dramatically improves perceived performance:- Instant navigation - Routes load instantly when prefetched
- No loading states - Users see content immediately
- Smart resource usage - Only prefetch what users are likely to need
- Code splitting friendly - Load code chunks before navigation
Prefetch Strategies
TanStack Router supports multiple prefetching strategies.Intent-Based Prefetching
Prefetch when users show intent to navigate:- Mouse hover - Desktop users
- Touch start - Mobile users
- Focus - Keyboard navigation
Viewport Prefetching
Prefetch when links enter the viewport:Render Prefetching
Prefetch immediately when the link renders:Manual Prefetching
Programmatically control prefetching:Global Prefetch Defaults
Set default prefetch behavior for all routes:packages/router-core/src/router.ts:189-207, available options:
Preload Delay
Wait before prefetching to avoid unnecessary requests:Per-Route Prefetch Configuration
Override defaults for specific routes:Route Preload Options
What Gets Prefetched?
Prefetching loads multiple aspects of a route:Code Splitting
Load route code chunks:Loader Data
Execute route loaders:preload: true in context.
Component Assets
Load components, error boundaries, and pending components:Prefetch Cache Control
Control how long prefetched data stays fresh.Stale Time
How long data is considered fresh:Garbage Collection Time
How long to keep prefetched data in cache:packages/router-core/src/router.ts:239-255, the defaults are:
defaultPreloadStaleTime: 30 secondsdefaultPreloadGcTime: 30 minutes
Stale vs GC Time
Detecting Prefetch in Loaders
Conditionally run logic based on prefetch:preload flag indicates prefetch vs navigation.
Prefetching with Search Params
Prefetch specific search param combinations:Preload Dependencies
Search params inloaderDeps are included:
Prefetch Best Practices
Use intent-based as default
Use intent-based as default
Intent-based prefetching (
preload="intent") provides the best balance between performance and bandwidth usage.Add preload delay
Add preload delay
Set
defaultPreloadDelay to 50-100ms to avoid prefetching when users briefly hover over links.Disable for expensive routes
Disable for expensive routes
Don’t prefetch routes with heavy loaders or large bundles. Let users explicitly navigate to them.
Consider mobile data usage
Consider mobile data usage
Be conservative with viewport and render prefetching on mobile networks. Intent-based is safer.
Tune cache times appropriately
Tune cache times appropriately
Set
preloadStaleTime based on data freshness needs. News sites want short times, dashboards can use longer.Advanced Patterns
Conditional Prefetching
Prefetch on Route Enter
Batch Prefetching
Prefetch Metrics
Track prefetch effectiveness:Next Steps
Caching
Learn about cache management for prefetched data
Loaders
Understand how loaders work with prefetching
Navigation
Explore navigation patterns with prefetching
Routes
Configure route-level prefetch options