Fix misc hydration bugs #9157
main fromrefs/pull/9157/head·merged by TkDodo View on GitHubThis PR:
- Fixes #8677 - A bug where hydrating failed queries (and possibly other conditions) could result in an infinite loop
- Fixes an undocumented bug where an old promise could overwrite newer data
- Fixes an undocumented bug where if a promise finished fast and was delivered as a synchronous thenable before React markup hydration, React Query would not hydrate it synchronously.
- The effect would be that a page would show its full content, but revert to the Suspense state on hydration because the data was not available in the React Query cache, only to immediately show the full content again.
- This behaviour also seem to have slowed down the initial load of pages with very fast APIs somewhat, so should be snappier now.
- Implements tests for these bugs
At the point of hydrating a promise, we don't know when the promise will finish, that is, we don't know the dataUpdatedAt of the hydrated query, so we can't compare that to the existing query. The PR sidesteps this by introducing dehydratedAt as an approximation. This is not perfect, but is an improvement.
To fix the infinite loop bug I also opted to never hydrate promises if the existing query is already pending, as there is no way to know which of these pending queries will finish first or have the newest data. We could do fancier things like a Promise.race and use whatever finishes first, but that seemed like overengineering at this point.
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: react-query
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: query-core
More templates
- @tanstack/query-example-angular-auto-refetching
- @tanstack/query-example-angular-basic
- @tanstack/query-example-angular-basic-persister
- @tanstack/query-example-angular-devtools-panel
- @tanstack/query-example-angular-infinite-query-with-max-pages
- @tanstack/query-example-angular-optimistic-updates
- @tanstack/query-example-angular-pagination
- @tanstack/query-example-angular-query-options-from-a-service
- @tanstack/query-example-angular-router
- @tanstack/query-example-angular-rxjs
- @tanstack/query-example-angular-simple
- @tanstack/query-example-react-algolia
- @tanstack/query-example-react-auto-refetching
- @tanstack/query-example-react-basic
- @tanstack/query-example-react-basic-graphql-request
- @tanstack/query-example-chat
- @tanstack/query-example-react-default-query-function
- @tanstack/query-example-react-devtools-panel
- @tanstack/query-example-eslint-legacy
- @tanstack/query-example-react-infinite-query-with-max-pages
- @tanstack/query-example-react-load-more-infinite-scroll
- @tanstack/query-example-react-nextjs
- @tanstack/query-example-react-nextjs-app-prefetching
- @tanstack/query-example-nextjs-suspense-streaming
- @tanstack/query-example-react-offline
- @tanstack/query-example-react-optimistic-updates-cache
- @tanstack/query-example-react-optimistic-updates-ui
- @tanstack/query-example-react-pagination
- @tanstack/query-example-react-playground
- @tanstack/query-example-react-prefetching
- @tanstack/query-example-react-react-native
- @tanstack/query-example-react-router
- @tanstack/query-example-react-rick-morty
- @tanstack/query-example-react-shadow-dom
- @tanstack/query-example-react-simple
- @tanstack/query-example-react-star-wars
- @tanstack/query-example-react-suspense
- @tanstack/query-example-solid-astro
- @tanstack/query-example-solid-basic
- @tanstack/query-example-solid-basic-graphql-request
- @tanstack/query-example-solid-default-query-function
- @tanstack/query-example-solid-simple
- @tanstack/query-example-solid-start-streaming
- @tanstack/query-example-svelte-auto-refetching
- @tanstack/query-example-svelte-basic
- @tanstack/query-example-svelte-load-more-infinite-scroll
- @tanstack/query-example-svelte-optimistic-updates
- @tanstack/query-example-svelte-playground
- @tanstack/query-example-svelte-simple
- @tanstack/query-example-svelte-ssr
- @tanstack/query-example-svelte-star-wars
- @tanstack/query-example-vue-2.6-basic
- @tanstack/query-example-vue-2.7-basic
- @tanstack/query-example-vue-basic
- @tanstack/query-example-vue-dependent-queries
- @tanstack/query-example-vue-nuxt3
- @tanstack/query-example-vue-persister
- @tanstack/query-example-vue-simple
@tanstack/angular-query-devtools-experimental
npm i https://pkg.pr.new/@tanstack/angular-query-devtools-experimental@9157@tanstack/angular-query-experimental
npm i https://pkg.pr.new/@tanstack/angular-query-experimental@9157@tanstack/eslint-plugin-query
npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@9157@tanstack/query-broadcast-client-experimental
npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@9157@tanstack/query-async-storage-persister
npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@9157@tanstack/query-core
npm i https://pkg.pr.new/@tanstack/query-core@9157@tanstack/query-devtools
npm i https://pkg.pr.new/@tanstack/query-devtools@9157@tanstack/query-persist-client-core
npm i https://pkg.pr.new/@tanstack/query-persist-client-core@9157@tanstack/query-sync-storage-persister
npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@9157@tanstack/react-query
npm i https://pkg.pr.new/@tanstack/react-query@9157@tanstack/react-query-devtools
npm i https://pkg.pr.new/@tanstack/react-query-devtools@9157@tanstack/react-query-next-experimental
npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@9157@tanstack/react-query-persist-client
npm i https://pkg.pr.new/@tanstack/react-query-persist-client@9157@tanstack/solid-query
npm i https://pkg.pr.new/@tanstack/solid-query@9157@tanstack/solid-query-devtools
npm i https://pkg.pr.new/@tanstack/solid-query-devtools@9157@tanstack/solid-query-persist-client
npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@9157@tanstack/svelte-query
npm i https://pkg.pr.new/@tanstack/svelte-query@9157@tanstack/svelte-query-devtools
npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@9157@tanstack/svelte-query-persist-client
npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@9157@tanstack/vue-query
npm i https://pkg.pr.new/@tanstack/vue-query@9157@tanstack/vue-query-devtools
npm i https://pkg.pr.new/@tanstack/vue-query-devtools@9157commit:
fd7a6e0Codecov Report#
Attention: Patch coverage is
94.44444%with2 linesin your changes missing coverage. Please review.Project coverage is 59.46%. Comparing base (
065b5f4) to head (fd7a6e0). Report is 2 commits behind head on main.Additional details and impacted files
@@ Coverage Diff @@ ## main #9157 +/- ## =========================================== + Coverage 45.00% 59.46% +14.46% =========================================== Files 206 138 -68 Lines 8219 5482 -2737 Branches 1836 1471 -365 =========================================== - Hits 3699 3260 -439 + Misses 4080 1925 -2155 + Partials 440 297 -143:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
TkDodo reviewed
@@ -82 +82,21 @@8282 }83+84+/**85+ * This function takes a Promise-like input and detects whether the data86+ * is synchronously available or not.87+ *88+ * It does not inspect .status, .value or .reason properties of the promise,89+ * as those are not always available, and the .status of React's promises90+ * should not be considered part of the public API.91+ */92+export function tryResolveSync(promise: Promise<unknown> | Thenable<unknown>) {93+ let data: unknown94+95+ promise96+ .then((result) => {97+ data = result98+ return result99+ })100+ // This can be unavailable on certain kinds of thenable's101+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition102+ ?.catch(() => {})using
noopfromquery-coreshould get rid of the eslint-disable:Suggested change101- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition102- ?.catch(() => {})101+ ?.catch(noop)It doesn't get rid of the lint warning unfortunately, that's warning about the "unnecessary"
?.here (this is typed as a promise, but might be a kind of thenable without.catch).Still a good idea to use the
noop, and I updated the comment to mention.catchspecifically to make it a tiny bit clearer.TkDodo merged this9f5fba3
TkDodo closed this
Is this going to be a part of next release?
It's already released
Sign in to join this conversation.
View your CI Pipeline Execution ↗ for commit fd7a6e03d40eae59261eb2c709342415c65d3edf.
nx affected --targets=test:sherif,test:knip,tes...nx run-many --target=build --exclude=examples/*...☁️ Nx Cloud last updated this comment at
2025-05-22 15:33:47UTC