Lesson

Configure staleTime and gcTime in Tanstack Router

Configure staleTime and gcTime in TanStack Router for data caching and freshness

Access
Included
Transcript
Needs source

In this lesson, we learn how to configure staleTime and gcTime in TanStack Router to manage data caching and freshness.

The staleTime parameter determines how long data is considered fresh before it is refetched in the background, while the gcTime parameter controls how long data is kept in the cache before being removed.

By default, data is considered stale immediately, but we can adjust these times to suit our needs. For example, setting staleTime to 10 seconds (10,000 milliseconds) means that if a user navigates back to a route within that time, the cached data will be used and refetched in the background.

Adjusting gcTime affects how long data remains cached after a route is unloaded. These settings help balance data freshness with performance by minimizing unnecessary network requests.