Skip to content
Source
Sign inSign up
TanStack/query

fix: support async Svelte #9810

Mergeddummdidumm(GitHub profile, opens in a new tab)merged 4 commits intomain fromrefs/pull/9810/headmerged by lachlancollins(GitHub profile, opens in a new tab) View on GitHub

Showing 2 changed files with 32 additions and 5 deletions.

x
0 of 2 viewed, 2 left
5 additions and 0 deletions.changeset/thin-bugs-change.mdAdded
View file
@@ -0,0 +1,5 @@
+---
+'@tanstack/svelte-query': patch
+---
+​
+fix: support async Svelte
27 additions and 5 deletionspackages/svelte-query/src/createBaseQuery.svelte.tsModified
View file
@@ -1,3 +1,4 @@
+import { onDestroy } from 'svelte'
import { useIsRestoring } from './useIsRestoring.js'
import { useQueryClient } from './useQueryClient.js'
import { createRawRef } from './containers.svelte.js'
@@ -71,13 +72,34 @@
createResult(),
​
- $effect(() => {
- const unsubscribe = isRestoring.current
+ // The following is convoluted but necessary:
Thread on line 75

Thanks for tackling async Svelte lifecycles! A few technical considerations on this implementation:

  1. SSR Subscription Teardown: Because typeof window === 'undefined' evaluates to false on the server, observer.subscribe(...) will run eagerly during SSR. Given the try...catch around onDestroy, if this runs in streaming SSR or an edge context where onDestroy lifecycle guarantees differ, could this risk leaking query subscriptions in long-running Node processes?

  2. Immediate Redundant Resubscription: The eager observer.subscribe() at initialization is immediately followed by watchChanges(..., 'pre'), which runs its setup phase and immediately tears down (unsubscribe()) and recreates the subscription. Could the eager call be integrated cleanly into the watcher to prevent churn on initial component mount?

  3. Regression Test Coverage: Because this touches tricky boundaries between eager SSR subscription, $effect.pre, and uncompleted async component teardown, adding a regression test in packages/svelte-query/tests (simulating an unmount before async resolution) would be invaluable to safeguard against future Svelte compiler updates.