-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix(react-query/HydrationBoundary): prevent unnecessary refetch during hydration #9968
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
base: main
Are you sure you want to change the base?
fix(react-query/HydrationBoundary): prevent unnecessary refetch during hydration #9968
Conversation
🦋 Changeset detectedLatest commit: d3bf064 The changes in this PR will be included in the next version bump. This PR includes changesets to release 19 packages
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 |
📝 WalkthroughWalkthroughAdds hydration-aware tracking to prevent double-fetch during HydrationBoundary hydration: introduces a WeakSet to mark pending queries, updates QueryObserver to skip fetches for pending queries (unless forced), updates HydrationBoundary to mark/clear pending queries, adds docs, tests, and a changeset. Changes
Sequence Diagram(s)sequenceDiagram
participant App as App / Router
participant HB as HydrationBoundary
participant QC as QueryClient / Cache
participant QO as QueryObserver
participant Net as Network
rect rgba(230,240,255,0.5)
Note over App,HB: Render phase (sync)
App->>HB: Render with dehydrated state
HB->>QC: Hydrate cache (useMemo)
HB->>QC: For each hydrated query: add to pendingHydrationQueries
Note over HB,QC: Queries flagged as pending
end
rect rgba(220,255,230,0.5)
Note over QO,Net: Subscription / mount
QO->>QO: onSubscribe runs
QO->>QC: Check pendingHydrationQueries for query
alt pending and refetchOnMount != 'always'
QO->>QO: Skip initial fetch (avoid double-fetch)
else not pending or refetchOnMount == 'always'
QO->>Net: Fetch data
end
end
rect rgba(255,250,220,0.5)
Note over HB: Effect phase (async)
HB->>HB: useEffect runs after paint
HB->>QC: Complete hydration
HB->>QC: clear pendingHydrationQueries for hydrated queries
Note over HB,QC: Cleanup ensures no stale pending flags
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Comment |
|
View your CI Pipeline Execution ↗ for commit d3bf064
☁️ Nx Cloud last updated this comment at |
86745a3 to
8626d42
Compare
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #9968 +/- ##
===========================================
+ Coverage 46.05% 59.97% +13.92%
===========================================
Files 200 129 -71
Lines 8542 5769 -2773
Branches 1979 1591 -388
===========================================
- Hits 3934 3460 -474
+ Misses 4150 1994 -2156
+ Partials 458 315 -143 🚀 New features to boost your workflow:
|
cfa5d74 to
695fa28
Compare
…tract 'clearPendingQueries' function
|
Thanks a lot for this. I hope it gets merged soon 🤞 |
🎯 Changes
Fixes #9610
When using
HydrationBoundarywith existing queries in the cache, an unnecessary refetch was triggered during hydration even though fresh data was about to be hydrated from the server.Root Cause
useQuery'sonSubscriberuns beforeHydrationBoundary'suseEffectcompletes hydrationSolution
pendingHydrationQueriesWeakSet to track queries pending hydrationuseMemo(before children render)queryObserver.onSubscribeif query is pending hydrationrefetchOnMount: 'always'still triggers refetch (user explicitly wants fresh data)useEffectafter hydration completesFiles Changed
packages/query-core/src/hydration.ts- ExportpendingHydrationQueriesWeakSetpackages/query-core/src/index.ts- Re-exportpendingHydrationQueriespackages/query-core/src/queryObserver.ts- Check pending hydration inonSubscribepackages/react-query/src/HydrationBoundary.tsx- Mark/clear pending hydration queries with cleanuppackages/react-query/src/__tests__/HydrationBoundary.test.tsx- Add 14 test casesdocs/framework/react/guides/ssr.md- Document new behaviordocs/framework/react/reference/hydration.md- Document new behavior✅ Checklist
pnpm run test:pr.🚀 Release Impact
Summary by CodeRabbit
Bug Fixes
Documentation
Tests
✏️ Tip: You can customize this high-level summary in your review settings.