Browse documentation
Lesson 10 · Cache time semantics

Define when data is fresh

Separate staleTime from gcTime and co-locate keys, fetchers, and time policy in shared queryOptions.

TanStack QueryCore20 minREV 02Markdown .md
After this lesson

Set freshness from business change frequency and explain why garbage-collection time does not prevent background refetching.

01

Freshness and retention are different questions

staleTime controls how long data remains fresh; gcTime controls how long an inactive query without observers remains cached. Cached data is stale by default, while inactive queries are normally collected after five minutes. Increasing gcTime does not make data fresh.

02

Put time policy in the query contract

queryOptions returns the supplied configuration at runtime while preserving the type relationship between queryKey and queryFn. Loader prefetching, component reads, and cache writes should reuse the contract so one resource does not acquire conflicting time policies.

src/features/issues/queries.tsts
import { queryOptions } from '@tanstack/react-query'

export const issueOptions = (issueId: string) => queryOptions({
  queryKey: ['issues', 'detail', issueId] as const,
  queryFn: () => getIssue(issueId),
  staleTime: 60_000,
  gcTime: 10 * 60_000,
})
PRACTICE

Turn this lesson into a verifiable skill

Completion checkpoint

Choose staleTime from business change frequency and distinguish freshness from inactive-cache collection.

Exercise

Define distinct freshness policies for issue lists, issue details, and a static priority dictionary, then justify them.

Verification

Use Devtools to observe fresh, stale, and inactive states and verify focus refetching matches the policy.

Common pitfalls
  • Using gcTime to control whether data refetches.
  • Disabling every automatic refetch to hide an incorrect freshness policy.
SOURCE REFERENCES · CHECKED 2026-08-03https://tanstack.com/query/latest/docs/framework/react/guides/important-defaultshttps://tanstack.com/query/latest/docs/framework/react/guides/query-options
TanStack Atlas

Original bilingual knowledge · verified against primary sources

Friend linksGitHub