fix(solid-query): prevent suspense trigger with pre-cached data #10053
main fromrefs/pull/10053/head·opened When using refetchOnMount: false with pre-cached data, the first setQueryData call would incorrectly trigger a Suspense fallback. This happened because the Proxy handler was calling queryResource() even when cached data was available.
This fix ensures that:
- The resource resolves immediately when data is already available, regardless of the
isLoadingstate - The Proxy handler returns cached data directly when
isFetchingis false and data exists, avoiding unnecessary resource access
Closes #9883
🎯 Changes#
useBaseQuery.ts (line 263): Added || observerResult.data !== undefined condition to resolve the resource immediately when cached data exists, even if isLoading is true
useBaseQuery.ts (line 378-390): Modified the Proxy handler to check isFetching state before accessing queryResource(). When isFetching is false and data exists in either state or observerResult, return it directly without triggering Suspense
suspense.test.tsx: Added test case to verify that Suspense is never triggered when using refetchOnMount: false with pre-cached data, including initial render and subsequent setQueryData calls
result#
https://github.com/user-attachments/assets/2518236a-1b99-406f-9a52-e7dce58894f2
✅ 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
- Improved Suspense mode handling to better manage pre-cached query data, preventing unnecessary suspension during data mutations.
- Enhanced data retrieval logic to prioritize available cached data and optimize loading state detection.
Tests
- Added comprehensive test coverage for Suspense mode behavior with pre-cached data and disabled refetch on mount.
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: solid-query
Caution
Review failed#
The pull request is closed.
Walkthrough
-
Fixes a bug where mutations with
refetchOnMount: falseincorrectly trigger suspense when data is already cached. Modifies data retrieval precedence and loading conditions in Solid Query's base hook to prevent unnecessary suspense activation.Changes#
Cohort / File(s) Summary Test Addition
packages/solid-query/src/__tests__/suspense.test.tsxAdds test case verifying suspense is not triggered when setQueryDatamutates a pre-cached key withrefetchOnMount: false, validating both initial render and subsequent mutations.Core Logic Modification
packages/solid-query/src/useBaseQuery.tsRelaxes server/client resolver loading condition to resolve when observerResult.dataexists. Enhances data accessor precedence to checkobserverResult.databefore falling back toqueryResource()?.data, usingisFetchinginstead ofisLoadingfor cached data returns.Estimated code review effort#
🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs#
- TanStack/query#9772: Modifies data tracking behavior to prefer
observerResult.dataand avoid suspense triggers when cached data exists, addressing the same underlying observer behavior. - TanStack/query#9822: Updates
useBaseQuery.tsserver-side render and data-resolution logic with different resolver/data-return strategies on the same file.
Suggested labels#
package: solid-querySuggested reviewers#
- TkDodo
- TanStack/query#9772: Modifies data tracking behavior to prefer
🚥 Pre-merge checks | ✅ 4 | ❌ 1
-
❌ Failed checks (1 warning)
-
Check name Status Explanation Resolution Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✏️ Tip: You can configure your own custom pre-merge checks in the settings.✅ Passed checks (4 passed)
-
Check name Status Explanation Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled. Title check ✅ Passed The title accurately summarizes the main change: preventing suspense triggers when using pre-cached data, which is the core issue being fixed. Linked Issues check ✅ Passed The PR successfully implements all coding requirements from issue #9883: it prevents suspense triggers with pre-cached data when refetchOnMount is false, modifies the resource resolution logic to check for cached data, and includes a comprehensive test.Out of Scope Changes check ✅ Passed All changes are directly related to fixing the suspense trigger bug described in issue #9883. The test addition, resolver logic changes, and proxy handler modifications are all within scope.
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
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.
-
shinseongsu(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: c76a14f28e4f91a857835dcb5358bac5632344b0
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 1 package
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