Skip to main content

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
The result is a snappier, more responsive application.

Prefetch Strategies

TanStack Router supports multiple prefetching strategies.

Intent-Based Prefetching

Prefetch when users show intent to navigate:
Prefetching starts on:
  • Mouse hover - Desktop users
  • Touch start - Mobile users
  • Focus - Keyboard navigation
This balances performance with bandwidth usage.

Viewport Prefetching

Prefetch when links enter the viewport:
Uses Intersection Observer to detect visibility. Great for content-heavy pages.

Render Prefetching

Prefetch immediately when the link renders:
Use sparingly - can waste bandwidth on routes users never visit.

Manual Prefetching

Programmatically control prefetching:
Full control over when and what to prefetch.

Global Prefetch Defaults

Set default prefetch behavior for all routes:
From packages/router-core/src/router.ts:189-207, available options:

Preload Delay

Wait before prefetching to avoid unnecessary requests:
If user moves mouse away within delay, prefetch is cancelled. This prevents accidental prefetches.

Per-Route Prefetch Configuration

Override defaults for specific routes:

Route Preload Options

These control how long prefetched data remains fresh and cached.

What Gets Prefetched?

Prefetching loads multiple aspects of a route:

Code Splitting

Load route code chunks:
Prefetching loads the lazy chunk before navigation.

Loader Data

Execute route loaders:
Loader runs with preload: true in context.

Component Assets

Load components, error boundaries, and pending components:
All component code is prefetched.

Prefetch Cache Control

Control how long prefetched data stays fresh.

Stale Time

How long data is considered fresh:
If navigating within stale time, cached data is used immediately.

Garbage Collection Time

How long to keep prefetched data in cache:
From packages/router-core/src/router.ts:239-255, the defaults are:
  • defaultPreloadStaleTime: 30 seconds
  • defaultPreloadGcTime: 30 minutes

Stale vs GC Time

Detecting Prefetch in Loaders

Conditionally run logic based on prefetch:
The preload flag indicates prefetch vs navigation.

Prefetching with Search Params

Prefetch specific search param combinations:
Prefetches the route with exact search params.

Preload Dependencies

Search params in loaderDeps are included:

Prefetch Best Practices

Intent-based prefetching (preload="intent") provides the best balance between performance and bandwidth usage.
Set defaultPreloadDelay to 50-100ms to avoid prefetching when users briefly hover over links.
Don’t prefetch routes with heavy loaders or large bundles. Let users explicitly navigate to them.
Be conservative with viewport and render prefetching on mobile networks. Intent-based is safer.
Set preloadStaleTime based on data freshness needs. News sites want short times, dashboards can use longer.

Advanced Patterns

Conditional Prefetching

Priority posts prefetch immediately, others on hover.

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