fix(svelte-query): unsubscribe the observer when its effect root is disposed by Neekoras · Pull Request #11597 · TanStack/query · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/svelte-query-effect-root-cleanup.md
5 changes: 5 additions & 0 deletions packages/svelte-query/src/createBaseQuery.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ export function createBaseQuery<
return unsubscribe
},
)
// ...and because the above only returns a cleanup from its second run onwards, tear the initial
// subscription down with an effect of its own, so disposing an `$effect.root` unsubscribes it.
try {
$effect.pre(() => () => unsubscribe())
} catch (e) {}
// ...and finally also cleanup via onDestroy because that one runs on the server whereas $effect.pre does not.
// (in a try-catch because it theoretically can be called in a non-component context - that should not happen
// but it would be a breaking change technically to error out here. SSR-safe because this wouldn't be called during SSR if it was not in a component)
Expand Down