Skip to content
Source
Sign inSign up
TanStack/query

fix(svelte-query): untrack observer subscription #11542

Opengzuuus(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/11542/headopened View on GitHub

Showing 5 changed files with 23 additions and 2 deletions (loading 2).

x
0 of 5 viewed, 5 left
5 additions and 0 deletions.changeset/svelte-query-untrack-subscribe.mdAdded
View file
@@ -0,0 +1,5 @@
+---
+'@tanstack/svelte-query': patch
+---
+​
+Run the observer subscription inside `untrack()` so that reactive state read by a `queryFn` before its first `await` can no longer become a dependency of the subscription effect, which tore the observer down (cancelling the in-flight fetch) and re-subscribed on every write to that state.
9 additions and 1 deletionpackages/svelte-query/src/createBaseQuery.svelte.tsModified
View file
@@ -1,3 +1,4 @@
+import { untrack } from 'svelte'
import { useIsRestoring } from './useIsRestoring.js'
import { useQueryClient } from './useQueryClient.js'
import { createRawRef } from './containers.svelte.js'
@@ -72,9 +73,16 @@
​
$effect(() => {
+ // Keep the observer read tracked (changing options must re-subscribe), but
+ // untrack the subscription itself: subscribing synchronously executes
+ // `queryFn` for queries without cached data, and any reactive state it reads before
+ // its first `await` would otherwise become a dependency of this effect.
+ // Writes to that state would then tear the observer down (cancelling the
+ // in-flight fetch) and re-subscribe indefinitely.
+ const o = observer
const unsubscribe = isRestoring.current
? () => undefined
- : observer.subscribe(() => update(createResult()))
+ : untrack(() => o.subscribe(() => update(createResult())))
observer.updateResult()
return unsubscribe