A hydrated query that rejects ignores the reader's `retry` and `retryDelay`

未关闭
#12,529 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@KATT 已经在做这个了。

开始于 2026年11月3日。

  • #12511 来自 @KATT —— 未关闭

评估

难度
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 sets retry: 1: the query never retries. It ends in error with the message redacted, since dehydrate redacts 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
Steps to reproduce
  1. Open the StackBlitz link. It installs deps, runs npm start by itself and prints the output in the terminal. Or clone the repo and run npm install && npm start.
  2. npm start runs this repro.ts with tsx, 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)
  1. It prints { status: 'error', data: undefined, error: 'redacted' }. The reader's retry: 1 never 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: useQuery and useSuspenseQuery under HydrationBoundary read the query through a QueryObserver.
  • hydrate resumes a pending query with query.fetch(undefined, { initialPromise }) before any observer exists. The retryer reads retry and retryDelay from 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 retry and retryDelay an observer set itself, then the hydrate options, 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

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

TanStack/query 的其他 Issue

查看 TanStack/query 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。