DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk6 min

TanStack Query Cache Problems: What to Check and How to Fix Them

Cached data can be present and still be stale. Find the right fix for repeated requests, ineffective invalidation, missing inactive data, hydration fetches, or discarded persisted cache.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If TanStack Query makes an unexpected request, shows old data, or appears to lose a cache entry, the cache may be working as designed: cached data can still be present but already be considered stale. Start by identifying whether the problem is freshness, inactive-cache retention, invalidation, or hydration. Each has a different fix; increasing one cache setting will not solve them all.

First identify what the cache problem looks like

  • A request repeats while data is visible: check freshness and the event that triggered a refetch.
  • Data vanishes after a component unmounts: check how long inactive queries are retained.
  • A successful mutation leaves old data on screen: check which cache entry the mutation updates or invalidates.
  • Invalidation seems ineffective: check whether the query is disabled or marked static.
  • A request happens after server rendering or hydration: check freshness timing, query keys, and QueryClient instances.
  • A persisted cache disappears earlier than expected: compare its persistence maxAge with gcTime.
  • Prefetched data is followed by a component fetch: check the freshness settings on both prefetch and useQuery.

The documentation links below are TanStack’s current “latest” React documentation, accessed October 7, 2026. APIs and defaults can vary across major versions, so verify the guidance against the version installed in your project, particularly when using older React Query examples.

As an Amazon Associate I earn from qualifying purchases.

Why does TanStack Query refetch data that is already cached?

By default, cached query data is considered stale immediately. Stale does not mean missing: the value can remain in the cache and be displayed while TanStack Query fetches an update. A stale query may refetch when a new observer mounts, the browser window regains focus, or network connectivity returns. Those requests can be expected behavior rather than evidence of a broken cache. See TanStack Query’s Important Defaults.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Set freshness with staleTime

staleTime controls how long data is treated as fresh. Set it to match how quickly that data needs to reflect changes in your application; there is no universally correct duration. It can be configured for a query or through QueryClient defaults. TanStack’s documentation says, “Setting staleTime is the recommended way to avoid excessive refetches.”

#1 Best Overall
Sale
Pearson Computer Networking, 8E
  • brand: Pearson
  • Computer Networking, 8e

The Important Defaults guide gives 2 * 60 * 1000 (two minutes) as an example: it says that setting staleTime to that value prevents refetches for two minutes, or until the query is manually invalidated. This is an illustration, not a recommendation for every query.

Choose Infinity or static deliberately

  • staleTime: Infinity prevents the query from becoming stale due to elapsed time; manual invalidation can still make it eligible for an update.
  • staleTime: 'static' is stricter: the Important Defaults guide says invalidateQueries has no effect on a static query. Consider it for data that cannot change while the app is running, such as boot-time feature flags, permissions loaded at login, or static reference tables.

Do not use 'static' simply to silence requests if the data can change and needs to respond to invalidation.

What is the difference between staleTime and gcTime?

They govern separate parts of cache behavior. staleTime determines freshness; gcTime determines how long unused data stays in memory after its query becomes inactive. Raising gcTime can preserve inactive data longer, but it does not make stale data fresh or prevent a stale-driven refetch when the query becomes active again. TanStack documents the browser default for inactive retention as five minutes and the server default as Infinity; these are documented defaults, not measured performance results. See the QueryObserverOptions reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why does a mutation succeed but the screen still show old data?

Check that the mutation targets the query key that owns the displayed data. invalidateQueries marks matching queries invalidated and, unless refetching is disabled, refetches eligible matches according to the filters. By default, it refetches active matches; refetchType can change which matches are refetched. Invalidation does not remove the cache entry: data can remain present while marked invalid. Consult the QueryClient and Query references.

If the mutation response contains the complete updated resource, the TanStack Start integration guide notes that setQueryData can write it directly to the corresponding cache entry. Use that when you have the authoritative updated value; invalidate when a refetch is needed to obtain or reconcile the current server state.

Why does invalidateQueries appear to do nothing?

Check whether the query is disabled

A query with enabled: false does not automatically fetch on mount or in the background and ignores invalidateQueries and refetchQueries calls that would normally trigger a refetch. The disabling guide states: “The query will ignore query client invalidateQueries and refetchQueries calls that would normally result in the query refetching.” Check whether the condition controlling enabled is still false. A disabled query can be triggered with its returned refetch method, subject to the documented skipToken limitation. See Disabling/Pausing Queries.

Check for static freshness

If the query uses staleTime: 'static', the Important Defaults guide says invalidation has no effect. If the query should respond to manual invalidation, use a freshness policy that permits it, such as Infinity where appropriate.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Check the key, filters, and refetchType

Confirm that the key passed to the invalidation call matches the query you intend to refresh and that filters select it. Also inspect refetchType: invalidating a match and choosing whether to refetch it are related but distinct behaviors.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why does a query fetch again after SSR or hydration?

TanStack Query measures staleness from dataUpdatedAt, using UTC timing. With the default staleTime of zero, data can be stale by the time the page hydrates, so a background request on page load may be expected. If avoiding that extra request suits the data, configure a higher staleTime for the relevant query. The Server Rendering & Hydration guide also documents server-side gcTime as Infinity and warns that setting gcTime: 0 can cause hydration errors if data is garbage-collected before rendering references it.

For repeated reads immediately after hydration in TanStack Start, check whether server and client use matching query keys, whether hydration is configured as intended, and whether the app accidentally creates extra QueryClient instances. The TanStack Query integration guide distinguishes Query data from router-owned loader data: queryClient.invalidateQueries refreshes Query data, while router.invalidate() reloads router-owned loader data and route context. If a mutation changes both, invalidate both owners; if it changes only one, refresh that owner.

Why is persisted cache data discarded?

Compare persistence maxAge with Query cache gcTime. The persistQueryClient guide says gcTime should be the same as or higher than maxAge when a restored cache is meant to remain available for the configured persistence period. The persistence plugin also supports a buster string to intentionally discard cache from an outdated build or application state. Use that mechanism when an old cache should not be restored, rather than treating every cache reset as an unexplained failure.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why does prefetched data still trigger a component fetch?

Prefetching uses the QueryClient’s default staleTime unless the prefetch call supplies its own value. A per-call staleTime applies to that prefetch operation; it does not automatically set the freshness policy for the later useQuery. If the component should treat the prefetched data as fresh for a matching interval, configure staleTime for the subsequent query too. See Prefetching & Router Integration.

A compact diagnostic checklist

  1. Identify the symptom: repeated request, missing inactive data, stale post-mutation display, ignored invalidation, hydration request, or lost persisted data.
  2. For repeated requests, inspect the query’s staleTime and whether mount, focus, or reconnect made a stale query eligible to refetch.
  3. For data lost while inactive, inspect gcTime; do not expect it to control freshness.
  4. For old post-mutation data, verify the query key and invalidation filters, or update the cache with complete mutation data.
  5. For ineffective invalidation, check enabled, staleTime: 'static', and refetchType.
  6. For SSR, hydration, or prefetch, compare freshness settings across server, prefetch, and client queries, and verify keys and QueryClient ownership.
  7. For persisted state, align gcTime with maxAge if the restored cache should remain available for that period.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.