fix(solid-query): don't trigger Suspense when data is already cached #10592
main fromrefs/pull/10592/head·opened Summary#
Fixes #9955.
When data was preloaded via ensureQueryData (e.g. from a TanStack Router loader), accessing state.data in a Suspense boundary would re-trigger the Suspense fallback even though the data was already in cache and isSuccess was true.
Root cause#
In useBaseQuery.ts, the proxy data getter reads queryResource.latest?.data whenever state.data !== undefined. Per Solid's docs, .latest falls back to a read()-equivalent (and therefore triggers Suspense) when the resource has no prior resolved value.
In the preloaded-cache case, the resource's fetcher synchronously calls resolve(...) because !observerResult.isLoading, but the resource doesn't transition out of the initial pending state until the next microtask. Reading .latest inside that microtask gap suspends the component even though state.data already holds the cached value.
Fix#
If the store already has data and no fetch is in-flight (!state.isFetching), return state.data directly without going through queryResource. state.isFetching is read via untrack so it doesn't widen the data subscriber's reactive deps (preserves existing reconcile / memo-churn tests).
When a fetch is in-flight, the existing queryResource.latest?.data path is preserved so background-refresh and re-mount semantics are unchanged.
Verification#
- Added regression test in
packages/solid-query/src/__tests__/suspense.test.tsxthat pre-populates the cache viaqueryClient.ensureQueryData(...), renders inside<Suspense>, and asserts the fallback never mounts. Test fails onmain(Suspense fires), passes with the fix. -
pnpm exec nx run @tanstack/solid-query:test:lib— all 310 tests pass. -
pnpm exec nx run @tanstack/solid-query:test:types— all TS versions pass. -
pnpm exec nx run @tanstack/solid-query:test:eslint— clean. - Changeset added (
patchfor@tanstack/solid-query).
Test plan#
- Regression test reproduces #9955 and now passes.
- All existing solid-query tests still pass (suspense, refetch-on-remount, reconcile referential equality, memo-churn).
🤖 Generated with Claude Code Generated by Ora Studio Vibe coded by ousamabenyounes
Summary by CodeRabbit#
Bug Fixes
- Prevented unnecessary Suspense activation when query data is already available (including data preloaded into cache), so components render immediately without showing loading fallbacks.
Tests
- Added regression tests to ensure Suspense fallbacks do not mount when data is cached or preloaded.
Chores
- Added a changeset patch note documenting the fix.
Generated by Ora Studio (with Claude Code) Vibe coded by Ben Younes Ousama
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: solid-query
View your CI Pipeline Execution ↗ for commit 654c8e7b6ad02a4f83dd20a1bcca9247367bddf4
Command Status Duration Result nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded 1m 19s View ↗ nx run-many --target=build --exclude=examples/*...✅ Succeeded <1s View ↗
☁️ Nx Cloud last updated this comment at
2026-05-11 08:26:23UTCMore templates
- @tanstack/query-example-angular-auto-refetching
- @tanstack/query-example-angular-basic
- @tanstack/query-example-angular-basic-persister
- @tanstack/query-example-angular-devtools-panel
- @tanstack/query-example-angular-infinite-query-with-max-pages
- @tanstack/query-example-angular-optimistic-updates
- @tanstack/query-example-angular-pagination
- @tanstack/query-example-angular-query-options-from-a-service
- @tanstack/query-example-angular-router
- @tanstack/query-example-angular-rxjs
- @tanstack/query-example-angular-simple
- @tanstack/query-example-lit-basic
- @tanstack/query-example-lit-pagination
- @tanstack/query-example-lit-ssr
- @tanstack/query-example-preact-simple
- @tanstack/query-example-react-algolia
- @tanstack/query-example-react-auto-refetching
- @tanstack/query-example-react-basic
- @tanstack/query-example-react-basic-graphql-request
- @tanstack/query-example-chat
- @tanstack/query-example-react-default-query-function
- @tanstack/query-example-react-devtools-panel
- @tanstack/query-example-eslint-legacy
- @tanstack/query-example-eslint-plugin-demo
- @tanstack/query-example-react-infinite-query-with-max-pages
- @tanstack/query-example-react-load-more-infinite-scroll
- @tanstack/query-example-react-nextjs
- @tanstack/query-example-react-nextjs-app-prefetching
- @tanstack/query-example-nextjs-suspense-streaming
- @tanstack/query-example-react-offline
- @tanstack/query-example-react-optimistic-updates-cache
- @tanstack/query-example-react-optimistic-updates-ui
- @tanstack/query-example-react-pagination
- @tanstack/query-example-react-playground
- @tanstack/query-example-react-prefetching
- @tanstack/query-example-react-react-native
- @tanstack/query-example-react-router
- @tanstack/query-example-react-rick-morty
- @tanstack/query-example-react-shadow-dom
- @tanstack/query-example-react-simple
- @tanstack/query-example-react-star-wars
- @tanstack/query-example-react-suspense
- @tanstack/query-example-solid-astro
- @tanstack/query-example-solid-basic
- @tanstack/query-example-solid-basic-graphql-request
- @tanstack/query-example-solid-default-query-function
- @tanstack/query-example-solid-offline
- @tanstack/query-example-solid-simple
- @tanstack/query-example-solid-start-streaming
- @tanstack/query-example-svelte-auto-refetching
- @tanstack/query-example-svelte-basic
- @tanstack/query-example-svelte-load-more-infinite-scroll
- @tanstack/query-example-svelte-optimistic-updates
- @tanstack/query-example-svelte-playground
- @tanstack/query-example-svelte-simple
- @tanstack/query-example-svelte-ssr
- @tanstack/query-example-svelte-star-wars
- @tanstack/query-example-vue-2.6-basic
- @tanstack/query-example-vue-2.7-basic
- @tanstack/query-example-vue-basic
- @tanstack/query-example-vue-dependent-queries
- @tanstack/query-example-vue-nuxt3
- @tanstack/query-example-vue-persister
- @tanstack/query-example-vue-simple
@tanstack/angular-query-experimental
npm i https://pkg.pr.new/@tanstack/angular-query-experimental@10592@tanstack/eslint-plugin-query
npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@10592@tanstack/lit-query
npm i https://pkg.pr.new/@tanstack/lit-query@10592@tanstack/preact-query
npm i https://pkg.pr.new/@tanstack/preact-query@10592@tanstack/preact-query-devtools
npm i https://pkg.pr.new/@tanstack/preact-query-devtools@10592@tanstack/preact-query-persist-client
npm i https://pkg.pr.new/@tanstack/preact-query-persist-client@10592@tanstack/query-async-storage-persister
npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@10592@tanstack/query-broadcast-client-experimental
npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@10592@tanstack/query-core
npm i https://pkg.pr.new/@tanstack/query-core@10592@tanstack/query-devtools
npm i https://pkg.pr.new/@tanstack/query-devtools@10592@tanstack/query-persist-client-core
npm i https://pkg.pr.new/@tanstack/query-persist-client-core@10592@tanstack/query-sync-storage-persister
npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@10592@tanstack/react-query
npm i https://pkg.pr.new/@tanstack/react-query@10592@tanstack/react-query-devtools
npm i https://pkg.pr.new/@tanstack/react-query-devtools@10592@tanstack/react-query-next-experimental
npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@10592@tanstack/react-query-persist-client
npm i https://pkg.pr.new/@tanstack/react-query-persist-client@10592@tanstack/solid-query
npm i https://pkg.pr.new/@tanstack/solid-query@10592@tanstack/solid-query-devtools
npm i https://pkg.pr.new/@tanstack/solid-query-devtools@10592@tanstack/solid-query-persist-client
npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@10592@tanstack/svelte-query
npm i https://pkg.pr.new/@tanstack/svelte-query@10592@tanstack/svelte-query-devtools
npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@10592@tanstack/svelte-query-persist-client
npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@10592@tanstack/vue-query
npm i https://pkg.pr.new/@tanstack/vue-query@10592@tanstack/vue-query-devtools
npm i https://pkg.pr.new/@tanstack/vue-query-devtools@10592commit:
654c8e7🧹 Nitpick comments (1)
packages/solid-query/src/__tests__/suspense.test.tsx (1)
911-950: ⚡ Quick winConsider spying on the
Pagequery function to assert it was never invoked.The test correctly guards against the fallback mounting, but it does not verify that the background
queryFn(returning'fresh') is never called. Avi.fn()spy would make the test more explicit about the "no refetch" contract implied bystaleTime: Infinity, and would catch regressions where the fix avoids Suspense but still triggers an unintended network call.✅ Suggested improvement
+ const freshFn = vi.fn(() => sleep(10).then(() => 'fresh')) + function Page() { const state = useQuery(() => ({ queryKey: key, - queryFn: () => sleep(10).then(() => 'fresh'), + queryFn: freshFn, staleTime: Infinity, })) return <div>data: {state.data}</div> }Then add at the end:
expect(rendered.getByText('data: preloaded')).toBeInTheDocument() expect(fallbackMounted).toBe(false) + expect(freshFn).not.toHaveBeenCalled() })🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/solid-query/src/__tests__/suspense.test.tsx` around lines 911 - 950, Add a vi.fn() spy for the queryFn used inside the Page component and assert it was never called after render: replace the inline queryFn in Page's useQuery with a spied function (e.g., const pageQueryFn = vi.fn(() => sleep(10).then(() => 'fresh')) and pass that to useQuery), then after rendering assert pageQueryFn was not called (expect(pageQueryFn).not.toHaveBeenCalled()) to ensure the background refetch never ran; keep the ensureQueryData preload untouched.🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. Nitpick comments: In `@packages/solid-query/src/__tests__/suspense.test.tsx`: - Around line 911-950: Add a vi.fn() spy for the queryFn used inside the Page component and assert it was never called after render: replace the inline queryFn in Page's useQuery with a spied function (e.g., const pageQueryFn = vi.fn(() => sleep(10).then(() => 'fresh')) and pass that to useQuery), then after rendering assert pageQueryFn was not called (expect(pageQueryFn).not.toHaveBeenCalled()) to ensure the background refetch never ran; keep the ensureQueryData preload untouched.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID:
39a9d48e-01a7-4062-9fd2-663ae1782d1f📥 Commits
Reviewing files that changed from the base of the PR and between c947290edefba0372160878d569a73fbb99f5ef6 and 7a99cf863c882acfa37925b8fcc2b8f6b2c3745c.
📒 Files selected for processing (3)
.changeset/solid-query-suspense-ensure-data.mdpackages/solid-query/src/__tests__/suspense.test.tsxpackages/solid-query/src/useBaseQuery.ts
✅ Files skipped from review due to trivial changes (1)
- .changeset/solid-query-suspense-ensure-data.md
🚧 Files skipped from review as they are similar to previous changes (1)
- packages/solid-query/src/useBaseQuery.ts
Addressed CodeRabbit nitpick in e815276e7: spied on
Page'squeryFnwithvi.fn()and addedexpect(pageQueryFn).not.toHaveBeenCalled()to assert the background refetch never ran.🦋 Changeset detected#
Latest commit: 35ee67ce37523b2a0785e2c38029794b69d6e0d2
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 24 packages
Name Type @tanstack/solid-query Patch @tanstack/solid-query-devtools Patch @tanstack/solid-query-persist-client Patch @tanstack/angular-query-experimental Patch @tanstack/eslint-plugin-query Patch @tanstack/preact-query-devtools Patch @tanstack/preact-query-persist-client Patch @tanstack/preact-query Patch @tanstack/query-async-storage-persister Patch @tanstack/query-broadcast-client-experimental Patch @tanstack/query-core Patch @tanstack/query-devtools Patch @tanstack/query-persist-client-core Patch @tanstack/query-sync-storage-persister Patch @tanstack/react-query-devtools Patch @tanstack/react-query-next-experimental Patch @tanstack/react-query-persist-client Patch @tanstack/react-query Patch @tanstack/vue-query Patch @tanstack/lit-query Patch @tanstack/svelte-query Patch @tanstack/svelte-query-devtools Patch @tanstack/vue-query-devtools Patch @tanstack/svelte-query-persist-client Patch Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR
TkDodo closed this
Sign in to join this conversation.
Note
Reviews paused#
It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the
reviews.auto_review.auto_pause_after_reviewed_commitssetting.Use the following commands to manage reviews:
@coderabbitai resumeto resume automatic reviews.@coderabbitai reviewto trigger a single review.Use the checkboxes below for quick actions:
📝 Walkthrough
Walkthrough#
This PR prevents unnecessary Solid Suspense activation when query data is already cached (including
ensureQueryDatapreloads) by changing thedatagetter to return cachedstate.dataonly when no fetch is in-flight, adding a regression test, and adding a changeset note.Changes#
Solid Query Suspense Fix
packages/solid-query/src/useBaseQuery.tsuntrackto Solid imports to allow conditional dependency checks.packages/solid-query/src/useBaseQuery.tsprop === 'data'now returnsstate.dataonly whenuntrack(() => state.isFetching)is falsy; otherwise it reads from the Solid resource (queryResource.latest?.data/queryResource()?.data).packages/solid-query/src/__tests__/suspense.test.tsxqueryClient.ensureQueryData(withstaleTime: Infinity) and asserts that mountinguseQueryinsideSuspenserenders the preloaded value without mounting the fallback..changeset/solid-query-suspense-ensure-data.mdensureQueryDatascenarios.Sequence Diagram(s)#
Estimated code review effort#
🎯 3 (Moderate) | ⏱️ ~20 minutes
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)#
✅ Passed checks (4 passed)
#9955by fixing the Suspense trigger when accessing cached data after ensureQueryData, including regression test and verification.#9955: the proxy getter logic in useBaseQuery.ts, the regression test, and the changeset entry.✨ Finishing Touches
🧪 Generate unit tests (beta)
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