Skip to content
Source
Sign inSign up
TanStack/query

fix: don't update dataUpdateCount when setting initial data #9743

Merged0xdiid(GitHub profile, opens in a new tab)merged 16 commits intomain fromrefs/pull/9743/headmerged by TkDodo View on GitHub

🎯 Changes

This introduces a check when updating dataUpdateCount so that it isn't updated when setting initial data. This in turn fixes the isFetchedAfterMount bug discussed in #9656

fixes #9656

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm test:pr

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • Bug Fixes

    • Fixed isFetchedAfterMount when initialData is applied.
    • Prevented dataUpdateCount from incrementing until a real refetch occurs after initialData.
    • Ensure state is marked success and prior fetch metadata/errors are cleared when initialData is used.
  • Tests

    • Added tests covering prefetched queries with initialData and observer-initiated refetch behavior.
  • Chores

    • Prepared patch release notes for the affected package.
    1. changeset-bot[bot](GitHub profile, opens in a new tab)commented

      🦋 Changeset detected

      Latest commit: b0bcd54e77b81424a6e59d3080b680ac3774b33a

      The changes in this PR will be included in the next version bump.

      This PR includes changesets to release 19 packages
      NameType
      @tanstack/query-corePatch
      @tanstack/angular-query-experimentalPatch
      @tanstack/query-async-storage-persisterPatch
      @tanstack/query-broadcast-client-experimentalPatch
      @tanstack/query-persist-client-corePatch
      @tanstack/query-sync-storage-persisterPatch
      @tanstack/react-queryPatch
      @tanstack/solid-queryPatch
      @tanstack/svelte-queryPatch
      @tanstack/vue-queryPatch
      @tanstack/angular-query-persist-clientPatch
      @tanstack/react-query-persist-clientPatch
      @tanstack/solid-query-persist-clientPatch
      @tanstack/svelte-query-persist-clientPatch
      @tanstack/react-query-devtoolsPatch
      @tanstack/react-query-next-experimentalPatch
      @tanstack/solid-query-devtoolsPatch
      @tanstack/svelte-query-devtoolsPatch
      @tanstack/vue-query-devtoolsPatch

      Not sure what this means? Click here to learn what changesets are.

      Click here if you're a maintainer who wants to add another changeset to this PR

    2. github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: query-core

    3. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Walkthrough

      Applies initialData to queries by updating the query state directly (avoiding setData) to prevent accidental dataUpdateCount increments; adds a test verifying prefetched-query + initialData behavior and observer refetch; includes a changeset noting the isFetchedAfterMount fix.

      Changes

      Cohort / File(s)Summary of changes
      Core logic: state initialization
      packages/query-core/src/query.ts
      Replace setData(manual: true) path with setState(...) using a new successState(data, dataUpdatedAt?) helper when seeding initialData; consolidate success-state construction in the reducer and preserve/reset fetch meta fields accordingly.
      Tests: initialData & isFetchedAfterMount
      packages/query-core/src/__tests__/query.test.tsx
      Add test ensuring applying initialData to a prefetched query does not increment dataUpdateCount; an observer-triggered refetch increments dataUpdateCount, updates data, and sets isFetchedAfterMount.
      Release metadata
      .changeset/eight-webs-buy.md
      Add patch changeset for @tanstack/query-core with note: "Fixed isFetchedAfterMount in cases where initialData is applied."

      Sequence Diagram(s)

      sequenceDiagram
        autonumber
        actor App
        participant Query as Query
        participant Observer as QueryObserver
        participant State as QueryState
        participant Fetcher as Fetch
      
        App->>Query: prefetch() (no data)
        Query->>State: create pending state (data: undefined)
      
        App->>Observer: mount(initialData)
        Observer->>Query: setOptions(initialData)
        Query->>State: setState(successState(initialData))
        Note right of State #dff0d8: dataUpdateCount remains 0\nfetchMeta/fetchFailure cleared
      
        App->>Observer: observer.refetch()
        Observer->>Query: fetch()
        Query->>Fetcher: network fetch
        Fetcher-->>Query: resolved data
        Query->>State: setData(fetched)  -- increments dataUpdateCount
        Note right of State #f7ecb5: isFetchedAfterMount becomes true

      Estimated code review effort

      🎯 3 (Moderate) | ⏱️ ~20 minutes

      • TanStack/query#9620 — Also modifies Query.setOptions / initialData seeding; closely related to restoring correct dataUpdateCount / isFetchedAfterMount behavior.

      Suggested reviewers

      • TkDodo

      Poem

      A rabbit hops through code at night, I tuck initialData in just right. Counts stay calm until fetch is true, Then heartbeat hops — a data view. Tests cheer, fixes snug — carrots for you! 🥕🐇

      Pre-merge checks and finishing touches

      ❌ Failed checks (1 warning)
      Check nameStatusExplanationResolution
      Docstring Coverage⚠️ WarningDocstring coverage is 0.00% which is insufficient. The required threshold is 80.00%.You can run @coderabbitai generate docstrings to improve docstring coverage.
      ✅ Passed checks (3 passed)
      Check nameStatusExplanation
      Title Check✅ PassedThe title "fix: don't update dataUpdateCount when setting initial data" is concise, clear, and directly reflects the primary change in the pull request. It accurately summarizes the main objective—preventing dataUpdateCount from being incremented when initialData is applied—which is the core fix for the isFetchedAfterMount regression described in issue #9656. The title avoids vague language, emojis, and unnecessary details while being specific enough for readers to understand the key change at a glance.
      Linked Issues Check✅ PassedThe changes in this PR directly address the core requirements from issue #9656. The modifications to query.ts introduce logic that prevents dataUpdateCount from being incremented when initialData is applied by using a new successState helper instead of calling setData with manual:true. The added test in query.test.tsx verifies this behavior by confirming that prefetching a query with initialData does not increment dataUpdateCount and that isFetchedAfterMount correctly reflects whether data was fetched after mount. A changeset has also been included to document the fix for release, and the contributor confirmed running local tests as required.
      Out of Scope Changes Check✅ PassedAll changes in this pull request are directly related to fixing the isFetchedAfterMount regression from issue #9656. The modifications include a test file verifying the fix, core changes to query.ts implementing the dataUpdateCount prevention logic, and a changeset documenting the patch. There are no extraneous changes such as unrelated refactoring, formatting updates, or modifications to code outside the scope of addressing the dataUpdateCount and isFetchedAfterMount issues.
      ✨ Finishing touches
      • 📝 Generate docstrings
        🧪 Generate unit tests (beta)
      • Create PR with unit tests
      • Post copyable unit tests in a comment

      📜 Recent review details

      Configuration used: CodeRabbit UI

      Review profile: CHILL

      Plan: Pro

      📥 Commits

      Reviewing files that changed from the base of the PR and between 426c02c912e4eb3fdf1c82279115b94f612d8da5 and c7031cfcd8325e7efc2f671ae1232eec7cf8d28a.

      📒 Files selected for processing (1)
      • packages/query-core/src/query.ts (3 hunks)
      🧰 Additional context used
      🧬 Code graph analysis (1)
      packages/query-core/src/query.ts (1)
      packages/query-core/src/mutation.ts (1)
      • action (315-383)
      🔇 Additional comments (3)
      packages/query-core/src/query.ts (3)

      708-716: LGTM! Well-structured helper that ensures consistency.

      The successState helper centralizes the construction of success state fields, ensuring that initialData application and fetch success don't diverge in the future. This directly addresses the guidance from past reviews.

      Based on past review comments.


      213-216: LGTM! Core fix correctly bypasses dataUpdateCount increment.

      This change correctly applies initialData via setState instead of setData, preventing the dataUpdateCount increment. The setState action (lines 667-671) simply spreads the provided state without modifying counters, ensuring that isFetchedAfterMount remains false when data comes from initialData.

      The update to #initialState on line 216 is also correct, capturing the state with initial data applied.


      634-649: LGTM! Correct usage of successState while preserving increment behavior.

      The reducer correctly uses the successState helper for consistency while still explicitly incrementing dataUpdateCount on line 638. This ensures that actual fetches increment the counter (as intended), while initialData applied via the setOptions path (lines 213-215) does not.

      The spread order and conditional logic are correct.


      Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands and usage tips.
    4. nx-cloud[bot](GitHub profile, opens in a new tab)commented

      View your CI Pipeline Execution ↗ for commit b0bcd54e77b81424a6e59d3080b680ac3774b33a

      CommandStatusDurationResult
      nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded5m 40sView ↗
      nx run-many --target=build --exclude=examples/*...✅ Succeeded1m 20sView ↗

      ☁️ Nx Cloud last updated this comment at 2025-11-01 14:34:42 UTC

    5. TkDodo reviewed

    6. pkg-pr-new[bot](GitHub profile, opens in a new tab)commented
      More templates

      @tanstack/angular-query-experimental

      npm i https://pkg.pr.new/@tanstack/angular-query-experimental@9743

      @tanstack/eslint-plugin-query

      npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@9743

      @tanstack/query-async-storage-persister

      npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@9743

      @tanstack/query-broadcast-client-experimental

      npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@9743

      @tanstack/query-core

      npm i https://pkg.pr.new/@tanstack/query-core@9743

      @tanstack/query-devtools

      npm i https://pkg.pr.new/@tanstack/query-devtools@9743

      @tanstack/query-persist-client-core

      npm i https://pkg.pr.new/@tanstack/query-persist-client-core@9743

      @tanstack/query-sync-storage-persister

      npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@9743

      @tanstack/react-query

      npm i https://pkg.pr.new/@tanstack/react-query@9743

      @tanstack/react-query-devtools

      npm i https://pkg.pr.new/@tanstack/react-query-devtools@9743

      @tanstack/react-query-next-experimental

      npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@9743

      @tanstack/react-query-persist-client

      npm i https://pkg.pr.new/@tanstack/react-query-persist-client@9743

      @tanstack/solid-query

      npm i https://pkg.pr.new/@tanstack/solid-query@9743

      @tanstack/solid-query-devtools

      npm i https://pkg.pr.new/@tanstack/solid-query-devtools@9743

      @tanstack/solid-query-persist-client

      npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@9743

      @tanstack/svelte-query

      npm i https://pkg.pr.new/@tanstack/svelte-query@9743

      @tanstack/svelte-query-devtools

      npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@9743

      @tanstack/svelte-query-persist-client

      npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@9743

      @tanstack/vue-query

      npm i https://pkg.pr.new/@tanstack/vue-query@9743

      @tanstack/vue-query-devtools

      npm i https://pkg.pr.new/@tanstack/vue-query-devtools@9743

      commit: 6f28855

    7. TkDodo reviewed

      packages/query-core/src/query.ts lines 213 to 217OutdatedResolvedby TkDodo
    8. TkDodo reviewed

    9. @TkDodo let me know if you have any more feedback here!

    10. TkDodo approved these changes

    11. TkDodo merged this1638c02

    12. TkDodo closed this

Sign in to join this conversation.