fix(preact-query): do not go into optimistic fetching state when not subscribed - #11259
fix(preact-query): do not go into optimistic fetching state when not subscribed#11259giaBaoJS wants to merge 1 commit into
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (5)
Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review. 📝 WalkthroughWalkthroughThe Preact query hooks now disable optimistic fetching and observer subscriptions when ChangesPreact subscription behavior
Estimated code review effort: 2 (Simple) | ~10 minutes Merge Risk: ⚪ Minimal · up to This change prevents unsubscribed queries from reporting a fetch that never occurs and keeps subscription toggles consistent; no actionable merge-blocking risk remains after normal checks and review. Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ 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. Comment |
Problem
subscribed: falseis broken in@tanstack/preact-query. A hook that opts out of subscribing still reportsisFetching: true/fetchStatus: 'fetching'on first render, even though no observer is ever attached and thequeryFnis never called.The Preact adapter was forked from React in #9935 (Feb 2026) and then missed two fixes that landed on the React side afterwards:
useBaseQueryuseQueriesBoth hooks in
preact-querystill set_optimisticResults = 'optimistic'unconditionally. IncreateResult, that branch computesfetchOnMount = !mounted && shouldFetchOnMount(query, options); with no listeners attached,mountedisfalseand the result is optimistically flipped intofetchState— a fetch that will never happen.useQueriesadditionally leftsubscribedout of theuseMemodependency array, so toggling it did not recompute the defaulted options.Fix
Port the two upstream changes verbatim: derive
subscribedonce, use it to gate_optimisticResults(undefinedwhen not subscribed), add it to theuseQueriesdependency array, and reuse it forshouldSubscribe. The diff is identical in shape toreact-query's currentuseBaseQuery.ts/useQueries.ts.Tests
Two regression tests, mirroring the ones added upstream:
useQuery.test.tsx— added to the existingsubscribeddescribe blockuseQueries.test.tsx— the Preact counterpart of the test from fix(react-query): keep unsubscribed useQueries idle #11130Both are synchronous, so there is no timer race.
Validation
Run from the repo root with
pnpm nx run @tanstack/preact-query:test:lib --skip-nx-cache.main: 34 files / 524 tests passing, no type errors.useBaseQuery.tsfails just theuseQuerytest; reverting onlyuseQueries.tsfails just theuseQueriestest. Reverting both fails both, withisFetching: true/fetchStatus: fetchingrendered wherefalse/idleis expected.test:types,test:eslint,test:buildandbuildfor the package, plusprettier --checkon the touched files.Summary by CodeRabbit
Bug Fixes
isFetching: falseand an idle fetch status.Tests
useQueryanduseQueries.