Skip to content

Conversation

@0xdiid
Copy link

@0xdiid 0xdiid commented Oct 10, 2025

🎯 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.

@changeset-bot
Copy link

changeset-bot bot commented Oct 10, 2025

🦋 Changeset detected

Latest commit: c7031cf

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

This PR includes changesets to release 19 packages
Name Type
@tanstack/query-core Patch
@tanstack/angular-query-experimental Patch
@tanstack/query-async-storage-persister Patch
@tanstack/query-broadcast-client-experimental Patch
@tanstack/query-persist-client-core Patch
@tanstack/query-sync-storage-persister Patch
@tanstack/react-query Patch
@tanstack/solid-query Patch
@tanstack/svelte-query Patch
@tanstack/vue-query Patch
@tanstack/angular-query-persist-client Patch
@tanstack/react-query-persist-client Patch
@tanstack/solid-query-persist-client Patch
@tanstack/svelte-query-persist-client Patch
@tanstack/react-query-devtools Patch
@tanstack/react-query-next-experimental Patch
@tanstack/solid-query-devtools Patch
@tanstack/svelte-query-devtools Patch
@tanstack/vue-query-devtools Patch

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

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 10, 2025

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
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

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 name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring 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 name Status Explanation
Title Check ✅ Passed The 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 ✅ Passed The 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 ✅ Passed All 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 426c02c and c7031cf.

📒 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.

@nx-cloud
Copy link

nx-cloud bot commented Oct 12, 2025

View your CI Pipeline Execution ↗ for commit 426c02c

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ⛔ Cancelled 15s View ↗

☁️ Nx Cloud last updated this comment at 2025-10-17 07:36:17 UTC

@pkg-pr-new
Copy link

pkg-pr-new bot commented Oct 12, 2025

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: 478f5d0

@0xdiid 0xdiid requested a review from TkDodo October 15, 2025 05:32
@0xdiid 0xdiid requested a review from TkDodo October 17, 2025 05:43
@0xdiid 0xdiid requested a review from TkDodo October 21, 2025 04:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

isFetchedAfterMount was broken in commit 1c8a921 / v5.87.1

2 participants