[svelte-query] Reactive options updates can cancel and restart an in-flight refetch without changing the query key or enabled value
维护者通常 1 天内回复
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 48/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 活跃
- 技术栈
- typescript
- 领域
- frontend
调研方向
Start with the provided regression test as a .svelte.test.ts file in a Svelte-enabled Vitest setup using client/browser conditions and jsdom. Trace how createInfiniteQuery recomputes reactive options during the focus-triggered refetch, particularly cancellation and infinite-query page fetching. Done means the test completes without aborting the in-flight request and reports aborted: false with pages [0, 1].
由索引模型根据 Issue 内容生成。
描述
Describe the bug
With createInfiniteQuery, updating reactive state used by the options accessor can cancel and restart an in-flight refetch, even though:
- The query key stays the same.
enabledremainstrue.- The QueryClient stays the same.
- The component remains mounted.
- No explicit cancellation or invalidation occurs.
For example, replacing reactive state { enabled: true } with a new { enabled: true } object during a focus-triggered refetch can abort the current request and restart fetching from the first page.
With two cached pages, the reproduction fetches pages [0, 0, 1] instead of [0, 1]. The first request's AbortSignal becomes aborted.
Your minimal, reproducible example
See tests below
Steps to reproduce
The regression test below uses only Svelte, TanStack Query and Vitest. Run it as a .svelte.test.ts file in a Svelte-enabled Vitest setup using the client/browser resolution conditions and jsdom.
import { expect, it, vi } from 'vitest'
import { flushSync } from 'svelte'
import {
QueryClient,
InfiniteQueryObserver,
createInfiniteQuery,
infiniteQueryOptions,
focusManager,
} from '@tanstack/svelte-query'
it('does not cancel a refetch when reactive options retain the same values', async () => {
let state = $state({ enabled: true })
let now = Date.now()
vi.spyOn(Date, 'now').mockImplementation(() => now)
const client = new QueryClient({
defaultOptions: {
queries: { staleTime: 60_000, retry: false },
},
})
client.mount()
const key = ['items'] as const
let response: ReturnType<typeof Promise.withResolvers<void>> | undefined
const reads: Array<{ page: number; signal: AbortSignal }> = []
const options = () =>
infiniteQueryOptions({
queryKey: key,
enabled: state.enabled,
initialPageParam: 0,
queryFn: async ({ pageParam, signal }) => {
reads.push({ page: pageParam, signal })
await response?.promise
return {
items: [pageParam],
next: pageParam === 0 ? 1 : undefined,
}
},
getNextPageParam: (page) => page.next,
})
// Load two pages, then leave the query unobserved.
const first = new InfiniteQueryObserver(client, options())
const stop = first.subscribe(() => {})
await vi.waitFor(() =>
expect(first.getCurrentResult().isSuccess).toBe(true),
)
await first.fetchNextPage()
stop()
// Return to the cached query after it becomes stale.
now += 60_001
const dispose = $effect.root(() => {
const query = createInfiniteQuery(options, () => client)
$effect(() => {
void query.data
void query.isFetchingNextPage
void query.fetchStatus
})
})
try {
flushSync()
await vi.waitFor(() => expect(reads).toHaveLength(4))
await vi.waitFor(() =>
expect(client.getQueryState(key)?.fetchStatus).toBe('idle'),
)
flushSync()
now += 60_001
reads.length = 0
response = Promise.withResolvers<void>()
focusManager.setFocused(false)
focusManager.setFocused(true)
await vi.waitFor(() => expect(reads).toHaveLength(1))
// Recompute options without changing enabled or query identity.
state = { enabled: true }
flushSync()
const aborted = reads[0]!.signal.aborted
response.resolve()
await vi.waitFor(() =>
expect(client.getQueryState(key)?.fetchStatus).toBe('idle'),
)
expect({
aborted,
pages: reads.map(({ page }) => page),
}).toEqual({
aborted: false,
pages: [0, 1],
})
} finally {
response?.resolve()
dispose()
client.clear()
client.unmount()
vi.restoreAllMocks()
focusManager.setFocused(true)
}
})
- Load two pages of an infinite query whose query function reads the provided
signal. - Unsubscribe, retaining the cached pages.
- Let the query become stale, mount it through
createInfiniteQuery, and allow its initial refetch to finish. - Let it become stale again and trigger a window-focus refetch.
- While the first page request is pending, replace reactive state read by the options accessor with an equivalent object. Keep the query key and
enabledvalue unchanged. - Observe that the pending request is aborted and the first page is requested again.
Expected behavior
The existing refetch should continue without cancellation or duplicate requests when the query identity and enabled state remain unchanged.
Expected: aborted: false, requested pages [0, 1].
Actual: aborted: true, requested pages [0, 0, 1].
How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
- OS: Windows
- Reproduced using Vitest 4.1.11 with jsdom 30.0.1
Tanstack Query adapter
svelte-query
TanStack Query version
6.1.48
TypeScript version
6.0.3
Additional context
No response
- 主要语言
- TypeScript
- 星标
- 50.4k
- 派生
- 4.2k
- 平均合并
- 7 小时 37 分钟
- 30 天内合并 PR
- 420
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
TanStack/query 的其他 Issue
-
QueryClient.isFetching and useIsFetching accept a fetchStatus filter and ignore it可能已有人在做 @Apu-Emdad 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
TanStack/query#11930 · 1 条评论 ·
维护者通常 1 天内回复
-
solid-query: STRICT_READ_UNTRACKED on Solid 2 — client()/options() read in component body (useMutation, useBaseQuery)可能重新可做 @LeonxLJX 于 39 天前认领,目前没有进行中的 PR。 未关闭
难度 2/5 1-3 小时 新手友好度 84/100
TanStack/query#11358 · 2 条评论 · 1 个 reaction ·
维护者通常 1 天内回复
-
solid-query: switching the queryClient accessor strands the new client's cache (subscription stays on the old observer)可能已有人在做 @MaNaN1803 于 82 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 84/100
TanStack/query#11106 · 1 条评论 ·
维护者通常 1 天内回复
-
restoreQueries and persisterGc throw on malformed persisted entries可能已有人在做 @VGontier-cmd 于 7 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 72/100
维护者通常 1 天内回复
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 77/100
apache/rocketmq-dashboard#6610 ·
维护者通常 3 天内回复
-
nextjs-tracking
难度 2/5 1-3 小时 新手友好度 69/100
cloudflare/vinext#3920 ·
维护者通常 1 天内回复
-
Team:Dashboards
难度 2/5 1-3 小时 新手友好度 65/100
elastic/kibana#297299 · 1 条评论 ·
维护者通常 1 天内回复
-
analytics beginner friendly community contributions-welcome good first issue hacktoberfest help wanted up-for-grabs
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 1 天内回复
-
affected/v17 affected/v18 area/frontend type/bug
难度 2/5 1-3 小时 新手友好度 82/100
umbraco/Umbraco-CMS#24186 ·
维护者通常 2 天内回复