Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
0xdiid wants to merge 11 commits into TanStack:main
base: main
Choose a base branch
Loading
from 0xdiid:diid/fetchedAfterMountFix

Conversation

Copy link

@0xdiid 0xdiid commented Oct 10, 2025
edited by coderabbitai bot
Loading

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

Copy link

changeset-bot bot commented Oct 10, 2025
edited
Loading

🦋 Changeset detected

Latest commit: 426c02c

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

Copy link
Contributor

coderabbitai bot commented Oct 10, 2025
edited
Loading

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
Replaces prior setData(manual: true) path with setState using a new successState(...) helper when applying initialData to a query without data; resets fetch-related meta/failure fields and consolidates success-state assignments in the reducer.
Tests: initialData & isFetchedAfterMount
packages/query-core/src/__tests__/query.test.tsx
Adds a test ensuring applying initialData to a prefetched query does not increment dataUpdateCount, and that an observer-triggered refetch increments it and sets isFetchedAfterMount.
Release metadata
.changeset/eight-webs-buy.md
Adds a patch changeset for @tanstack/query-core and a 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

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 (4 passed)
Check name Status Explanation
Title Check ✅ Passed The pull request title "fix: don't update dataUpdateCount when setting initial data" clearly and concisely describes the main change in the changeset. It directly summarizes the core fix: preventing dataUpdateCount incrementation when initial data is applied. The title is specific, avoids vague terms, and accurately reflects the primary objective addressed in the PR related to fixing the isFetchedAfterMount bug.
Linked Issues Check ✅ Passed The pull request successfully addresses all coding requirements from issue #9656. The test case verifies that initializing a prefetched query with initialData does not increment dataUpdateCount while subsequent fetches do increment it appropriately. The code changes in query.ts implement the core fix by using setState with a successState helper instead of calling setData when setting initial data, preventing the premature dataUpdateCount increment. The refactoring of success state handling and the changeset documentation reinforce these fixes. The isFetchedAfterMount behavior is restored through these changes, fulfilling the primary objectives of issue #9656.
Out of Scope Changes Check ✅ Passed All code changes in this pull request are within scope and directly support fixing issue #9656. The test case verifies the fix for initialData behavior, the modifications in setOptions implement the core fix by avoiding dataUpdateCount incrementation when setting initial data, and the introduction of the successState helper consolidates success state logic while maintaining existing behavior for fetch operations. The changeset entry documents the fix, and no unrelated changes or tangential modifications are present that would fall outside the scope of addressing the isFetchedAfterMount regression.
Description Check ✅ Passed The pull request description follows the required template with all mandatory sections completed. The 🎯 Changes section provides a clear explanation of the fix and links it to the underlying issue (#9656). The ✅ Checklist section shows both contributing guide and local testing boxes checked, and the 🚀 Release Impact section indicates that a changeset was generated for this published code change. The description is complete, follows the specified structure, and provides sufficient context for the change.
✨ 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 1e762ff and 426c02c.

📒 Files selected for processing (1)
  • packages/query-core/src/query.ts (3 hunks)
🔇 Additional comments (3)
packages/query-core/src/query.ts (3)

712-720: LGTM! Clean abstraction for success state.

The successState helper function correctly encapsulates the state shape for successful data updates, ensuring consistency between initialData application and fetch success handling. The optional dataUpdatedAt parameter with a sensible default makes it flexible for both use cases.


213-218: Excellent fix for the dataUpdateCount bug.

This change correctly prevents dataUpdateCount from being incremented when initialData is applied by using setState directly (bypassing the 'success' action). The use of the successState helper ensures consistency with the reducer's success handling, while the explicit resets of fetchMeta, fetchFailureReason, and fetchFailureCount properly initialize fetch-related state.


640-646: LGTM! Consistent use of the successState helper.

The reducer's 'success' case now correctly uses the successState helper to set data/status/error/isInvalidated fields while still incrementing dataUpdateCount for actual fetch completions (line 641). The explicit fetchMeta: null assignment on line 646 improves clarity for the non-manual success path.


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.

Copy link

nx-cloud bot commented Oct 12, 2025
edited
Loading

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

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
fetchStatus: 'idle' as const,
fetchFailureCount: 0,
fetchFailureReason: null,
fetchMeta: null,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let’s not change this here please. I think I’ll investigate if this is necessary separately

Comment on lines +213 to 218
this.setState({
...successState(defaultState.data, defaultState.dataUpdatedAt),
fetchMeta: null,
fetchFailureReason: null,
fetchFailureCount: 0,
})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.setState(
 successState(defaultState.data, defaultState.dataUpdatedAt),
)

The whole idea was to have one defined successState so that these two places don’t diverge. I’ll try to fix fetchMeta and fetchFailureCount separately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@TkDodo TkDodo TkDodo left review comments

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

isFetchedAfterMount was broken in commit 1c8a921 / v5.87.1

2 participants

AltStyle によって変換されたページ (->オリジナル) /