HydrationBoundary double fetching on subsequent visits

未關閉
#9,610 5 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

@sukvvon 已經在處理了。

開始於 2025年12月14日。

  • #9617 來自 @joseph0926 —— 已關閉,未合併
  • #9968 來自 @sukvvon —— 未關閉
  • #11137 來自 @ostapondo —— 未關閉

評估

難度
4/5
預估耗時
3-5 天
新手友好度
35/100
Issue 類型
缺陷
描述清晰度
基本清楚
活躍度
停滯
技術堆疊
react, typescript
領域
frontend

研究方向

從 StackBlitz 重現開始,在離開並返回時追蹤 loader、HydrationBoundary、React.useMemo/useEffect 以及 refetchOnMount 的行為。確認伺服器計算了兩次新的 dehydrated 資料,然後驗證來自 loader 的新 query 在 hydration 完成之前不會在用戶端進行 refetch。

由索引模型根據 Issue 內容生成。

描述

Describe the bug

I used HydrationBoundary in React Router. In loader(), I get the data, dehydrate it, then hydrate the client side query client.

  1. For the first visit, since it's a new query, it got hydrated in React.useMemo
  2. For subsequent visits, it will be deferred to React.useEffect, however, if the cache is stale, query client will refetch before useEffect, which makes the server compute the data twice: one in loader, the other in the component

I looked into the source code and found that it's due to refetchOnMount, which is true by default. I think it would be plausible to prevent a refetch if the query in the dehydrated state is fresh.

There are some experiments in the provided code, e.g. initialData from React Query meets React Router, initialData + initialDataUpdatedAt, HydrationBoundary, and my custom hook use-hydrated-query which sets refetchOnMount to false for that query if not explicitly provided during hydration. You may navigate away and back for each experiment and see the log in console to reproduce the issue.

Your minimal, reproducible example

https://stackblitz.com/edit/vitejs-vite-yiggax3s

Steps to reproduce

mentioned in 'Describe the bug'

Expected behavior

If query is fresh from loader(), client side query client should not refetch

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

Any browser

Tanstack Query adapter

react-query

TanStack Query version

v5.85.3

TypeScript version

5.8.3

Additional context

No response

主要語言
TypeScript
星號
50.5k
分支
4.2k
平均合併
11 小時 2 分鐘
30 天內合併 PR
524

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

TanStack/query 的其他 Issue

查看 TanStack/query 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。