Skip to content

fix(preact-query): do not go into optimistic fetching state when not subscribed - #11259

Open
giaBaoJS wants to merge 1 commit into
TanStack:mainfrom
giaBaoJS:fix/preact-unsubscribed-optimistic-fetching
Open

fix(preact-query): do not go into optimistic fetching state when not subscribed#11259
giaBaoJS wants to merge 1 commit into
TanStack:mainfrom
giaBaoJS:fix/preact-unsubscribed-optimistic-fetching

Conversation

@giaBaoJS

@giaBaoJS giaBaoJS commented Aug 23, 2026

Copy link
Copy Markdown

Problem

subscribed: false is broken in @tanstack/preact-query. A hook that opts out of subscribing still reports isFetching: true / fetchStatus: 'fetching' on first render, even though no observer is ever attached and the queryFn is 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:

Both hooks in preact-query still set _optimisticResults = 'optimistic' unconditionally. In createResult, that branch computes fetchOnMount = !mounted && shouldFetchOnMount(query, options); with no listeners attached, mounted is false and the result is optimistically flipped into fetchState — a fetch that will never happen. useQueries additionally left subscribed out of the useMemo dependency array, so toggling it did not recompute the defaulted options.

Fix

Port the two upstream changes verbatim: derive subscribed once, use it to gate _optimisticResults (undefined when not subscribed), add it to the useQueries dependency array, and reuse it for shouldSubscribe. The diff is identical in shape to react-query's current useBaseQuery.ts / useQueries.ts.

Tests

Two regression tests, mirroring the ones added upstream:

Both 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.

  • Baseline on main: 34 files / 524 tests passing, no type errors.
  • With this PR: 34 files / 526 tests passing, no type errors.
  • Red/green, each half checked separately: reverting only useBaseQuery.ts fails just the useQuery test; reverting only useQueries.ts fails just the useQueries test. Reverting both fails both, with isFetching: true / fetchStatus: fetching rendered where false / idle is expected.
  • Also ran test:types, test:eslint, test:build and build for the package, plus prettier --check on the touched files.

Summary by CodeRabbit

  • Bug Fixes

    • Prevented queries from entering a fetching state when subscription is disabled.
    • Disabled query execution and observer registration for unsubscribed queries.
    • Unsubscribed queries now correctly report isFetching: false and an idle fetch status.
  • Tests

    • Added coverage for unsubscribed behavior in useQuery and useQueries.

@coderabbitai

coderabbitai Bot commented Aug 23, 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: deb1723d-d4d8-4a0c-a6e8-03376d1f7585

📥 Commits

Reviewing files that changed from the base of the PR and between 0fe34b4 and 97c514a.

📒 Files selected for processing (5)
  • .changeset/tall-moons-repeat.md
  • packages/preact-query/src/__tests__/useQueries.test.tsx
  • packages/preact-query/src/__tests__/useQuery.test.tsx
  • packages/preact-query/src/useBaseQuery.ts
  • packages/preact-query/src/useQueries.ts

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


📝 Walkthrough

Walkthrough

The Preact query hooks now disable optimistic fetching and observer subscriptions when subscribed: false. Tests cover query execution, observer counts, and idle fetch state. A patch changeset records the fix.

Changes

Preact subscription behavior

Layer / File(s) Summary
Subscription state and optimistic results
packages/preact-query/src/useBaseQuery.ts, packages/preact-query/src/useQueries.ts
The hooks derive a shared subscription state. They disable optimistic results and observer subscriptions when subscribed is false, while preserving restoring behavior.
Unsubscribed query coverage
packages/preact-query/src/__tests__/useQueries.test.tsx, packages/preact-query/src/__tests__/useQuery.test.tsx, .changeset/tall-moons-repeat.md
Tests verify that unsubscribed queries do not call the query function, register observers, or report fetching. A patch changeset records the fix.

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

Merge Risk: ⚪ Minimal · up to 97c51

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: sukvvon, tkdodo

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main fix for unsubscribed Preact Query hooks.
Description check ✅ Passed The description thoroughly explains the problem, fix, tests, validation, and release impact, but it does not use the template headings or checklist.
Docstring Coverage ✅ Passed Docstring check was indeterminate for this PR — some files could not be analyzed in time. Not blocking.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ 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.

1 participant