Client-Side Data Cache

A memory of what you already fetched: components share one copy, duplicate requests collapse into one, and refetching follows a freshness policy.

query cachedon't refetch what I already havekeeps data aroundclient side data cacheingstale while revalidatestop hitting the API every time I go back to a pageremembering API responses in the browserreact query cache

See it

Live demo coming soon

What it is

A client-side data cache sits between your components and the network. Ask for 'user 42' twice and the second ask is answered from memory instead of firing a second request, and two components asking at once collapse into one trip. It does not mean the network is touched once and never again: the same cache still revalidates in the background, refetches on your command, retries failures, and drops entries when you invalidate them. Libraries like TanStack Query, SWR, Apollo, and RTK Query key each response by the request that produced it, then share that one copy with every component asking for it.

The usual policy is stale-while-revalidate: hand over the cached copy instantly so the screen paints, then quietly refetch in the background and swap in fresh data if it changed. That is why a well-cached app feels instant when you navigate back to a page you already visited. Reach for it the moment two screens need the same server data, or the same screen is mounted and unmounted often.

The real trap is invalidation. After a mutation (create, edit, delete) the cache still holds the old answer, so the list you just added to looks unchanged. Every cache library gives you a way to mark keys dirty after a write, and forgetting to call it is the single most common bug. Second trap: a cache is not global state. Do not stuff form drafts or UI toggles into it.

Ask AI for it

Add TanStack Query to this app as the client-side data cache. Wrap the root in a QueryClientProvider, convert every direct fetch call in components into a useQuery with a structured key like ['posts', id], and set staleTime so revisiting a screen serves the cached copy first and revalidates in the background. Convert writes to useMutation and invalidate the affected keys in onSuccess so lists refresh after a create or delete. Show cached data immediately and only render a spinner on the very first load.

You might have meant

data fetchingoptimistic uistate managementside effectstreaming rendering

Go deeper