A hydrated query that rejects ignores the reader's `retry` and `retryDelay`
维护者通常 1 天内回复
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 45/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- typescript
调研方向
Start with the query-core retryer and hydration flow described in the issue: hydrate resumes the pending query before a QueryObserver exists, so later observer options may not reach the retryer. The issue says fix #12511 implements the desired precedence; inspect that change and the related query-core tests. Done means the repro succeeds with reader retry and retryDelay, while unset options still use hydrate options and client defaults.
由索引模型根据 Issue 内容生成。
描述
(🤖 Drafted with Opus 5.5, then read and edited word by word by me.)
Describe the bug
If the server dehydrates a query that's still pending, hydrate resumes it in the browser before any component reads it. If that promise rejects, the retries use the options the fetch started with - the hydrate options and the client's defaults. The retry and retryDelay the reader sets are ignored, even if the reader mounted before the promise settled.
So a reader can't recover from a failed server fetch:
- Client default
retry: false, reader setsretry: 1: the query never retries. It ends inerrorwith the messageredacted, sincedehydrateredacts the server's error. - Reader sets
retry: false: the query still retries, after the client's default delay of about a second. - Reader sets
retryDelay: 10: the retry still waits about a second.
Your minimal, reproducible example
- StackBlitz: https://stackblitz.com/github/KATT/tanstack-query-hydrated-retry-repro?file=repro.ts&terminalHeight=40&view=editor
- Repository: https://github.com/KATT/tanstack-query-hydrated-retry-repro
Steps to reproduce
- Open the StackBlitz link. It installs deps, runs
npm startby itself and prints the output in the terminal. Or clone the repo and runnpm install && npm start. npm startruns thisrepro.tswithtsx, against@tanstack/[email protected]:
import {
QueryClient,
QueryObserver,
defaultShouldDehydrateQuery,
dehydrate,
hydrate,
} from '@tanstack/query-core'
const sleep = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms))
const queryKey = ['post', 1]
// 🖥️ Server: the query is still pending at dehydrate, and rejects later
const serverClient = new QueryClient({
defaultOptions: {
dehydrate: {
// include pending queries in dehydration, as in the Advanced SSR guide
shouldDehydrateQuery: (query) =>
defaultShouldDehydrateQuery(query) || query.state.status === 'pending',
},
},
})
serverClient
.query({
queryKey,
queryFn: () => sleep(10).then(() => Promise.reject(new Error('server error'))),
retry: false,
})
.catch(() => {})
const dehydratedState = dehydrate(serverClient)
// 🌐 Browser: retries are off by default, so we can see whether the reader's option wins
const browserClient = new QueryClient({
defaultOptions: { queries: { retry: false } },
})
hydrate(browserClient, dehydratedState)
// 👀 This reader asks for 1 retry
const observer = new QueryObserver(browserClient, {
queryKey,
queryFn: () => sleep(10).then(() => 'data from the browser'),
retry: 1,
retryDelay: 10,
})
observer.subscribe(() => {})
await sleep(500)
const { status, data, error } = browserClient.getQueryState(queryKey)!
console.log({ status, data, error: error?.message })
// ✅ Expected: { status: 'success', data: 'data from the browser', error: undefined }
// 🐛 Actual on 5.106.0: { status: 'error', data: undefined, error: 'redacted' }
process.exit(0)
- It prints
{ status: 'error', data: undefined, error: 'redacted' }. The reader'sretry: 1never ran.
Expected behavior
I'd expect a hydrated query that rejects to retry with its reader's retry and retryDelay, like any other fetch. The script should print { status: 'success', data: 'data from the browser', error: undefined }. Today it keeps the client's defaults and ends with the redacted server error.
Options the reader doesn't set should still come from the hydrate options first and the client's defaults second, like now.
How often does this bug happen?
Every time
Screenshots or Videos
None - the script's output above shows the result.
Platform
- OS: any. I reproduced it on Linux.
- Runtime: Node 22.23.4. It also runs in StackBlitz's WebContainer. The bug is in query-core, so the browser doesn't matter.
Tanstack Query adapter
vanilla
TanStack Query version
v5.106.0
TypeScript version
7.0.2
Additional context
- React hits the same bug:
useQueryanduseSuspenseQueryunderHydrationBoundaryread the query through aQueryObserver. hydrateresumes a pending query withquery.fetch(undefined, { initialPromise })before any observer exists. The retryer readsretryandretryDelayfrom the options that fetch started with, so options an observer sets later never reach it.- Fix in #12511: when a fetch resumes a hydrated promise, the retryer uses the
retryandretryDelayan observer set itself, then thehydrateoptions, then the client's defaults. With it, the script above prints{ status: 'success', data: 'data from the browser', error: undefined }.
- 主要语言
- TypeScript
- 星标
- 50.6k
- 派生
- 4.2k
- 平均合并
- 9 小时 50 分钟
- 30 天内合并 PR
- 310
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
TanStack/query 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
TanStack/query#12466 · 1 条评论 ·
维护者通常 1 天内回复
-
solid-query: STRICT_READ_UNTRACKED on Solid 2 — client()/options() read in component body (useMutation, useBaseQuery)可能重新可做 @LeonxLJX 于 77 天前认领,目前没有进行中的 PR。 未关闭
难度 2/5 1-3 小时 新手友好度 84/100
TanStack/query#11358 · 2 条评论 · 1 个 reaction ·
维护者通常 1 天内回复
-
select that throws for placeholderData brings back the selection of data removed by resetQueries可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 3/5 1-2 天 新手友好度 56/100
维护者通常 1 天内回复
-
Render loop since v5.105.2 when placeholderData, or its select result, is not referentially stable未关闭
难度 4/5 3-5 天 新手友好度 54/100
维护者通常 1 天内回复
-
useQueries: stable combine returns a stale result when a query updates before setQueries runs可能已有人在做 @sakaintp 于 1 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 62/100
TanStack/query#12626 · 2 条评论 ·
维护者通常 1 天内回复
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
fb55/htmlparser2#2579 ·
维护者通常 1 天内回复
-
bug good first issue needs-review
难度 1/5 1-3 小时 新手友好度 83/100
维护者通常 1 天内回复
-
documentation public
难度 1/5 1-3 小时 新手友好度 68/100
midnightntwrk/umbradb#65 ·
维护者通常 2 天内回复
-
area/channels kind/bug priority/medium
难度 2/5 1-3 小时 新手友好度 78/100
nanocoai/nanoclaw#4501 · 1 条评论 ·
维护者通常 1 天内回复
-
needs verification
难度 2/5 1-3 小时 新手友好度 74/100
维护者通常 13 天内回复