Skip to content
Source
Sign inSign up
TanStack/query

Fix misc hydration bugs #9157

MergedEphem(GitHub profile, opens in a new tab)merged 5 commits intomain fromrefs/pull/9157/headmerged by TkDodo View on GitHub

This 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

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

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

      View your CI Pipeline Execution ↗ for commit fd7a6e03d40eae59261eb2c709342415c65d3edf.

      CommandStatusDurationResult
      nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded49sView ↗
      nx run-many --target=build --exclude=examples/*...✅ Succeeded1sView ↗

      ☁️ Nx Cloud last updated this comment at 2025-05-22 15:33:47 UTC

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

      @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@9157

      commit: fd7a6e0

    5. codecov[bot](GitHub profile, opens in a new tab)commented

      Codecov Report

      Attention: Patch coverage is 94.44444% with 2 lines in 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

      Impacted file tree graph

      @@             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     
      ComponentsCoverage Δ
      @tanstack/angular-query-devtools-experimental∅ <ø> (∅)
      @tanstack/angular-query-experimental85.04% <ø> (ø)
      @tanstack/eslint-plugin-query∅ <ø> (∅)
      @tanstack/query-async-storage-persister43.85% <ø> (ø)
      @tanstack/query-broadcast-client-experimental24.39% <ø> (ø)
      @tanstack/query-codemods∅ <ø> (∅)
      @tanstack/query-core97.89% <93.75%> (-0.10%):arrow_down:
      @tanstack/query-devtools3.56% <ø> (ø)
      @tanstack/query-persist-client-core78.32% <ø> (ø)
      @tanstack/query-sync-storage-persister84.61% <ø> (ø)
      @tanstack/query-test-utils∅ <ø> (∅)
      @tanstack/react-query95.98% <100.00%> (+0.63%):arrow_up:
      @tanstack/react-query-devtools10.00% <ø> (ø)
      @tanstack/react-query-next-experimental∅ <ø> (∅)
      @tanstack/react-query-persist-client100.00% <ø> (ø)
      @tanstack/solid-query78.20% <ø> (ø)
      @tanstack/solid-query-devtools∅ <ø> (∅)
      @tanstack/solid-query-persist-client100.00% <ø> (ø)
      @tanstack/svelte-query88.15% <ø> (ø)
      @tanstack/svelte-query-devtools∅ <ø> (∅)
      @tanstack/svelte-query-persist-client100.00% <ø> (ø)
      @tanstack/vue-query70.85% <ø> (ø)
      @tanstack/vue-query-devtools∅ <ø> (∅)
      :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 data
    86+ * 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 promises
    90+ * should not be considered part of the public API.
    91+ */
    92+export function tryResolveSync(promise: Promise<unknown> | Thenable<unknown>) {
    93+ let data: unknown
    94+
    95+ promise
    96+ .then((result) => {
    97+ data = result
    98+ return result
    99+ })
    100+ // This can be unavailable on certain kinds of thenable's
    101+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
    102+ ?.catch(() => {})
    TkDodocommented

    using noop from query-core should get rid of the eslint-disable:

    Suggested change
    101- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
    102- ?.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 .catch specifically to make it a tiny bit clearer.

  • TkDodo merged this9f5fba3

  • TkDodo closed this

  • tlazarov-whiskypartners(GitHub profile, opens in a new tab)commented

    Is this going to be a part of next release?

  • It's already released

  • Sign in to join this conversation.