Skip to content

fix: preserve query behavior during hydration fetch#10467

Open
SAY-5 wants to merge 1 commit intoTanStack:mainfrom
SAY-5:fix/prefetch-infinite-nextjs
Open

fix: preserve query behavior during hydration fetch#10467
SAY-5 wants to merge 1 commit intoTanStack:mainfrom
SAY-5:fix/prefetch-infinite-nextjs

Conversation

@SAY-5
Copy link
Copy Markdown

@SAY-5 SAY-5 commented Apr 13, 2026

When prefetchInfiniteQuery fails on the server and the promise gets streamed to the client, hydrate() calls query.fetch(undefined, { initialPromise }) which drops the behavior property. Without infiniteQueryBehavior set, the retried fetch stores the raw response instead of wrapping it in the expected { pages, pageParams } structure, causing "Cannot read properties of undefined (reading 'length')" on the client.

The fix passes the query's existing behavior through to the fetch call so infinite queries retain their data processing pipeline after hydration.

Fixes #8825

Summary by CodeRabbit

  • Bug Fixes
    • Fixed query hydration to properly apply configured query behaviors during initialization, ensuring streamed query results are processed through their intended behavior pipeline rather than stored as raw data.

When prefetchInfiniteQuery fails on the server and the promise streams
to the client, hydrate() calls query.fetch(undefined, ...) which drops
the behavior property. Without infiniteQueryBehavior, the retried fetch
stores raw data instead of the expected { pages, pageParams } structure.

Pass the query's existing behavior through to the fetch call so infinite
queries keep working after hydration.

Fixes TanStack#8825
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 13, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 640bfff1-4938-4e7e-8410-271f744e950a

📥 Commits

Reviewing files that changed from the base of the PR and between 24de260 and 9b06d03.

📒 Files selected for processing (1)
  • packages/query-core/src/hydration.ts

📝 Walkthrough

Walkthrough

Updated the hydration logic in hydrate to pass query.options.behavior as the first argument to query.fetch(), ensuring dehydrated queries are processed through their configured behavior pipeline (such as infiniteQueryBehavior) rather than treated as raw data.

Changes

Cohort / File(s) Summary
Hydration behavior handling
packages/query-core/src/hydration.ts
Modified query.fetch() invocation to conditionally forward query.options.behavior as the first argument, enabling streamed initial promises to be processed through the query's configured behavior pipeline instead of bypassing it.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A rabbit hops through hydration streams,
With behaviors now properly aligned to dreams,
Infinite queries fetch with grace,
No raw data lost in cyberspace,
The pipeline flows, the fix takes flight! ✨

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The PR description explains the problem and solution well, but is missing the required checklist and release impact sections from the template. Add the missing checklist section (contributing guide confirmation and test confirmation) and release impact section (changeset generation or dev-only note) from the description template.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix: preserve query behavior during hydration fetch' clearly and specifically describes the main change of ensuring query behavior is maintained during hydration.
Linked Issues check ✅ Passed The code changes directly address the issue #8825 by ensuring infinite queries retain their behavior/pipeline during hydration, fixing the client-side data transformation failure.
Out of Scope Changes check ✅ Passed The changes are narrowly focused on the hydration logic to pass query behavior through, with no out-of-scope modifications detected beyond the stated objective.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Next.js: exception encountered in prefetchInfiniteQuery causes useSuspenseInfiniteQuery to fail

1 participant