Skip to content

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

Open
renechoi wants to merge 1 commit into
TanStack:mainfrom
renechoi:fix/issue-10681-createqueries-empty-init
Open

fix(svelte-query): activate createQueries initialized with an empty array#11127
renechoi wants to merge 1 commit into
TanStack:mainfrom
renechoi:fix/issue-10681-createqueries-empty-init

Conversation

@renechoi

@renechoi renechoi commented Jul 27, 2026

Copy link
Copy Markdown

🎯 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.
    • 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: 6c883e84-79e3-4373-85f8-22d93893bd18

📥 Commits

Reviewing files that changed from the base of the PR and between e396148 and 7cf4f48.

📒 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

📝 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

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main fix: activating createQueries when initialized with an empty array.
Description check ✅ Passed The description follows the template and includes changes, verification, checklist, and release impact with relevant detail.
Linked Issues check ✅ Passed The implementation and regression test address #10681 by making empty createQueries instances reactive after queries are added.
Out of Scope Changes check ✅ Passed The changes are scoped to the bug fix, test coverage, and required changeset, with no clear unrelated additions.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ 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.

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

1 participant