Skip to content

fix(query-core): memoize falsy combine results in QueriesObserver#11065

Open
zelinewang wants to merge 1 commit into
TanStack:mainfrom
zelinewang:fix/combine-falsy-memoization
Open

fix(query-core): memoize falsy combine results in QueriesObserver#11065
zelinewang wants to merge 1 commit into
TanStack:mainfrom
zelinewang:fix/combine-falsy-memoization

Conversation

@zelinewang

@zelinewang zelinewang commented Jul 15, 2026

Copy link
Copy Markdown

What changed

QueriesObserver treated a falsy combined result as if no cached result
existed. A stable combine callback returning 0, -0, NaN, false, an
empty string, null, undefined, or 0n was therefore called again even when
neither the callback nor any query result had changed.

This removes the value-truthiness check from the cache invalidation condition.
The existing result identity, query hash, and callback identity checks still
perform the initial calculation and invalidate the cache when an input changes.
The cached field now uses definite assignment because undefined is itself a
valid combined result and cannot serve as an initialization sentinel.

The regression test covers all eight standard primitive falsy values.

Testing

  • pnpm exec nx run @tanstack/query-core:test:lib --skip-nx-cache -- --run src/__tests__/queriesObserver.test.tsx -t 'should cache the falsy combined result' — 62 tests passed
  • pnpm exec nx run @tanstack/query-core:test:lib --skip-nx-cache — 24 files and 554 tests passed, with no type errors
  • pnpm exec nx run @tanstack/query-core:test:eslint --skip-nx-cache — passed with no errors
  • Prettier and git diff --check passed

Release impact

A patch changeset for @tanstack/query-core is included.

Summary by CodeRabbit

  • Bug Fixes

    • Improved query result memoization when combined results are falsy, including false, null, undefined, 0, and empty strings.
    • Prevented unnecessary recomputation when the underlying queries and combination logic remain unchanged.
  • Tests

    • Added coverage for caching all supported falsy combined result values.

@coderabbitai

coderabbitai Bot commented Jul 15, 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

Run ID: 135ab304-4f24-44f1-9a58-50b5b93f7167

📥 Commits

Reviewing files that changed from the base of the PR and between 79d2384 and e4b09de.

📒 Files selected for processing (3)
  • .changeset/tidy-lions-cache.md
  • packages/query-core/src/__tests__/queriesObserver.test.tsx
  • packages/query-core/src/queriesObserver.ts

📝 Walkthrough

Walkthrough

QueriesObserver now preserves cached combined results regardless of whether the value is truthy, with parameterized tests covering falsy values and a patch changeset documenting the fix.

Changes

QueriesObserver combined-result caching

Layer / File(s) Summary
Combined-result recomputation criteria
packages/query-core/src/queriesObserver.ts
#combinedResult is treated as definitely assigned, and recomputation is based on tracked results, query hashes, or the combine function reference rather than result truthiness.
Falsy-result regression coverage
packages/query-core/src/__tests__/queriesObserver.test.tsx, .changeset/tidy-lions-cache.md
Parameterized coverage verifies that falsy combined values are cached, and a patch changeset documents the behavior.

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

Suggested reviewers: tkdodo

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: memoizing falsy combine results in QueriesObserver.
Description check ✅ Passed The description covers the change, motivation, testing, and release impact, matching the template closely enough.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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