Skip to content

fix(svelte-query): activate createQueries initialized with an empty array - #11127

Merged
TkDodo merged 2 commits into
TanStack:mainfrom
renechoi:fix/issue-10681-createqueries-empty-init
Aug 24, 2026
Merged

fix(svelte-query): activate createQueries initialized with an empty array#11127
TkDodo merged 2 commits into
TanStack:mainfrom
renechoi:fix/issue-10681-createqueries-empty-init

Conversation

@renechoi

@renechoi renechoi commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

🎯 Changes

Fixes #10681.

createQueries stayed permanently inactive when it was initialized with an empty queries array. Adding entries later never revived any slot, while the same component worked if the initial array had at least one entry.

The results object comes from createRawRef, which turns each key into a $state.raw field as that key appears. Two reads have nothing to subscribe to:

  • an absent index (results[0] while the ref is still empty) has no field yet
  • length is a plain array property on the proxy target

So a consumer that observes the result while it is empty registers no reactive dependency at all, and never re-runs once the queries are added. The data does arrive on the observer, which is why reading results[0].data directly returns the right value while the rendered output stays pending forever.

This adds a key version that update() bumps whenever the key set changes, and tracks it from the proxy get trap for absent keys, hidden keys, and array length. createBaseQuery passes an object whose shape is stable, so it never bumps the version and its behavior is unchanged.

For reference, #10696 targeted this issue by restructuring the QueriesObserver lifecycle and was closed by its author without review. The test below shows the observer itself is already doing the right thing on main, so the remaining defect is in how the results ref is tracked.

Heads up that the open #10892 adds a line to the same update() function for an unrelated defect (#10341). The two changes are independent, but they are adjacent enough to conflict textually if both land.

✅ Verification

Added a regression test that starts createQueries with queries: [], then adds one query and asserts that the tracked results reach success. It fails on main with expected [] to match object [ { data: 'data1', status: 'success' } ], and reverting only containers.svelte.ts on this branch reproduces the same failure. In that failing state the preceding expect(result[0]?.data).toBe('data1') still passes, which is what isolates the problem to reactivity rather than fetching.

nx affected --targets=test:sherif,test:knip,test:docs,test:eslint,test:lib,test:types,test:build,build passes locally across the 13 affected projects. @tanstack/svelte-query is 23 files / 169 tests, and @tanstack/svelte-query-devtools and @tanstack/svelte-query-persist-client are green as well.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • Bug Fixes

    • Fixed an issue where queries initialized with an empty list could remain inactive after queries were added.
    • Improved reactive updates when object properties are added or removed, ensuring dependent views refresh correctly.
  • Tests

    • Added coverage for dynamically adding queries after initialization and confirming successful results.

createQueries stayed permanently inactive when it was initialized with an
empty queries array. Reads of an absent index have no $state.raw field to
subscribe to, and length is a plain array property, so a consumer that
observed the result while it was empty registered no reactive dependency
and never re-ran once queries were added.

Track those reads through a key version that update() bumps whenever the
key set changes.
@coderabbitai

coderabbitai Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 49f45b1e-2aed-42f8-911b-e84829b1fb99

📥 Commits

Reviewing files that changed from the base of the PR and between 250050d and 80e0e44.

📒 Files selected for processing (3)
  • .changeset/khaki-moons-invite.md
  • packages/svelte-query/src/containers.svelte.ts
  • packages/svelte-query/tests/createQueries/createQueries.svelte.test.ts
🚧 Files skipped from review as they are similar to previous changes (3)
  • packages/svelte-query/tests/createQueries/createQueries.svelte.test.ts
  • .changeset/khaki-moons-invite.md
  • packages/svelte-query/src/containers.svelte.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 8 remain after this review.


📝 Walkthrough

Walkthrough

createRawRef now tracks key-presence changes, allowing createQueries to reactivate when a reactive queries array changes from empty to populated. A regression test covers the transition and successful query resolution, with a patch changeset documenting the fix.

Changes

createQueries reactivity

Layer / File(s) Summary
Track reactive key changes
packages/svelte-query/src/containers.svelte.ts
createRawRef tracks absent-key and array-length reads, and increments keyVersion when keys are added or removed.
Validate empty-array activation
packages/svelte-query/tests/createQueries/createQueries.svelte.test.ts, .changeset/khaki-moons-invite.md
Adds coverage for populating an initially empty reactive queries array and documents the patch release.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to 80e0e

The change restores reactivity for queries added after an initially empty list and includes targeted regression coverage with passing checks. No actionable merge-blocking risk remains beyond normal review.

Sequence Diagram(s)

sequenceDiagram
  participant QueriesArray
  participant createRawRef
  participant createQueries
  QueriesArray->>createRawRef: Add a query
  createRawRef->>createRawRef: Increment keyVersion
  createRawRef->>createQueries: Notify the tracked key
  createQueries->>QueriesArray: Read the populated array
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the fix for inactive createQueries instances initialized with an empty array.
Description check ✅ Passed The description explains the defect, implementation, regression test, verification, checklist, and release impact.
Linked Issues check ✅ Passed The implementation and regression test satisfy issue #10681 by activating and resolving queries added after empty initialization.
Out of Scope Changes check ✅ Passed The changeset, reactive key tracking, and regression test directly support the linked issue and stated pull request objective.
Docstring Coverage ✅ Passed Docstring check was indeterminate for this PR — some files could not be analyzed in time. Not blocking.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Michael-Obele Michael-Obele left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, fix looks good to me.

@coderabbitai

coderabbitai Bot commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@nx-cloud

nx-cloud Bot commented Aug 24, 2026

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 80e0e44

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 3m 11s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded 8s View ↗

☁️ Nx Cloud last updated this comment at 2026-08-24 12:47:16 UTC

@pkg-pr-new

pkg-pr-new Bot commented Aug 24, 2026

Copy link
Copy Markdown
More templates

@tanstack/angular-query-experimental

npm i https://pkg.pr.new/@tanstack/angular-query-experimental@11127

@tanstack/eslint-plugin-query

npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@11127

@tanstack/lit-query

npm i https://pkg.pr.new/@tanstack/lit-query@11127

@tanstack/preact-query

npm i https://pkg.pr.new/@tanstack/preact-query@11127

@tanstack/preact-query-devtools

npm i https://pkg.pr.new/@tanstack/preact-query-devtools@11127

@tanstack/preact-query-persist-client

npm i https://pkg.pr.new/@tanstack/preact-query-persist-client@11127

@tanstack/query-async-storage-persister

npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@11127

@tanstack/query-broadcast-client-experimental

npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@11127

@tanstack/query-core

npm i https://pkg.pr.new/@tanstack/query-core@11127

@tanstack/query-devtools

npm i https://pkg.pr.new/@tanstack/query-devtools@11127

@tanstack/query-persist-client-core

npm i https://pkg.pr.new/@tanstack/query-persist-client-core@11127

@tanstack/query-sync-storage-persister

npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@11127

@tanstack/react-query

npm i https://pkg.pr.new/@tanstack/react-query@11127

@tanstack/react-query-devtools

npm i https://pkg.pr.new/@tanstack/react-query-devtools@11127

@tanstack/react-query-next-experimental

npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@11127

@tanstack/react-query-persist-client

npm i https://pkg.pr.new/@tanstack/react-query-persist-client@11127

@tanstack/solid-query

npm i https://pkg.pr.new/@tanstack/solid-query@11127

@tanstack/solid-query-devtools

npm i https://pkg.pr.new/@tanstack/solid-query-devtools@11127

@tanstack/solid-query-persist-client

npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@11127

@tanstack/svelte-query

npm i https://pkg.pr.new/@tanstack/svelte-query@11127

@tanstack/svelte-query-devtools

npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@11127

@tanstack/svelte-query-persist-client

npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@11127

@tanstack/vue-query

npm i https://pkg.pr.new/@tanstack/vue-query@11127

@tanstack/vue-query-devtools

npm i https://pkg.pr.new/@tanstack/vue-query-devtools@11127

commit: 80e0e44

@TkDodo
TkDodo merged commit 320ed25 into TanStack:main Aug 24, 2026
9 checks passed
@github-actions github-actions Bot mentioned this pull request Aug 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[svelte-query] createQueries becomes permanently inactive when initialized with an empty queries array

3 participants