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 theloader 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: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
Keep loaders focused
Keep loaders focused
Loaders should only fetch data. Move business logic to separate functions.
Use AbortController
Use AbortController
Always pass the abort signal to fetch calls for proper cancellation.
Leverage caching
Leverage caching
Set appropriate
staleTime and gcTime to reduce unnecessary requests.Handle errors gracefully
Handle errors gracefully
Provide helpful error messages and recovery options.
Next steps
Error handling
Learn error boundary patterns
Loaders concept
Deep dive into loader architecture