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

Đang mở
#12,529 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@KATT đang làm issue này rồi.

Từ ngày 3/11/2026.

  • #12511 của @KATT — đang mở

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
45/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
typescript
Lĩnh vực
backend-api-design

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

(🤖 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 }.
Ngôn ngữ chính
TypeScript
Star
50.6k
Fork
4.2k
Merge trung bình
9 giờ 50 phút
Pull request đã merge (30 ngày)
310

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của TanStack/query

Tất cả issue của TanStack/query

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.