Skip to content

bug: Hydration error using loading state of useQuery when prefetching #6740

Closed

Description

@leanderriefel

Provide environment information

bunx envinfo --system --binaries --browsers --npmPackages "typescript,next,react,@trpc/server,@trpc/client,@trpc/react,@trpc/react-query,@trpc/client,@trpc/next,@tanstack/react-query"

  System:
    OS: Windows 11 10.0.26100
    CPU: (24) x64 AMD Ryzen 9 3900X 12-Core Processor            
    Memory: 11.88 GB / 31.92 GB
  Binaries:
    Node: 20.7.0 - C:\Program Files\nodejs\node.EXE
    npm: 10.1.0 - C:\Program Files\nodejs\npm.CMD
    pnpm: 8.14.3 - ~\AppData\Local\pnpm\pnpm.EXE
    bun: 1.2.10 - ~\.bun\bin\bun.EXE
  Browsers:
    Edge: Chromium (134.0.3124.93)
    Internet Explorer: 11.0.26100.1882
  npmPackages:
    @tanstack/react-query: ^5.74.7 => 5.74.7 
    @trpc/client: ^11.1.1 => 11.1.1 
    @trpc/server: ^11.1.1 => 11.1.1 
    next: 15.3.1 => 15.3.1 
    react: ^19.0.0 => 19.1.0 
    typescript: ^5 => 5.8.3 

Describe the bug

By voiding your prefetch calls on the server (instead of awaiting), the prefetch sometimes isn't done when rendering on the server, so when using some kind of loading indicators, these will get rendered by the server. Now sometimes on the client, the data is instantly available by the prefetch, so by the time the client render starts, the data gets instantaneously rendered and the loading state gets skipped. This causes a hydration error.

This problem was also discussed on the discord: https://discord.com/channels/867764511159091230/1311314438443499621 / https://discord-questions.trpc.io/m/1311314438443499621

I'm not sure if this is a trpc or tanstack-react-query bug or if im doing something wrong.

Temporary fixes include:

  • awaiting your prefetch instead of voiding them like the docs currently say it they should as await disables some of the streaming stuff
  • using useSuspenseQuery (was said in the discord channel / haven't tested)
  • forcefully set isPending to true on the very first client-side render

Link to reproduction

https://github.com/Entropy-10/hydration-failed-trpc-clerk (note that this is not my link but from someone in the discord, haven't tested this)

To reproduce

do a prefetch call in a rsc component and not await it. in a child client-component use this query and have a loading-state and "data available"-state.
sometimes the loading state gets rendered on the server and the "data available"-state gets rendered on the client, resulting in a hydration error due to mismatch.

Additional information

No response

👨‍👧‍👦 Contributing

  • 🙋‍♂️ Yes, I'd be down to file a PR fixing this bug!

Activity

  1. ammarmbe commented on May 27, 2025

    @ammarmbe

    i'm also getting this

  2. saiichihashimoto commented on Jun 3, 2025

    @saiichihashimoto

    I thought I was losing my mind, glad someone else can confirm this is happening

  3. epicmau5time commented on Jun 14, 2025

    @epicmau5time

    Also suppressHydrationWarning does not work on any components. Only thing I can do until a fix there is to comment out the voided prefetch call on the page.tsx and let the data be fetched after render in the client component. @leanderriefel, I feel it's repeatable enough for it to move from an unconfirmed tag.

  4. Icestonks commented on Jun 20, 2025

    @Icestonks

    I think I'm encountering the same error.

    I have tried to create a repo where the bug is being reproduced, as the one linked, is not available anymore: https://stackblitz.com/~/github.com/Icestonks/react-query-trpc-test

  5. DogPawHat commented on Jun 22, 2025

    @DogPawHat
    Contributor

    I was helping @Icestonks debug this. Its pretty infuriating, that much I can tell you.

    Id like to know more about the api calls that people are doing with this. I could only get this to work under throttling in the browser - if the page hydrates before the api resolves you'll never see this issue, and I suspect most api endpoints are slower then nextjs page loads (especially in production).

    Likewise, if anyone has managed to reproduce this issue outside of nextjs or trpc, just using react query - this feels very query specific to me.

  6. leanderriefel commented on Jun 22, 2025

    @leanderriefel
    Author

    Id like to know more about the api calls that people are doing with this. I could only get this to work under throttling in the browser - if the page hydrates before the api resolves you'll never see this issue, and I suspect most api endpoints are slower then nextjs page loads (especially in production).

    Well there shouldn't be an error - especially when using the library the way the docs say it should be used - because a query responds too fast

  7. saiichihashimoto commented on Jun 22, 2025

    @saiichihashimoto

    I'm imagining a way to repro this would be to put a setTimeout delay in a trpc route and the do the void prefetch. Then you'd get the page rendering while the prefetch is still occurring, which I believe is the source of the error.

  8. Icestonks commented on Jun 22, 2025

    @Icestonks

    @saiichihashimoto That's what I'm doing in the repo I sent a bit further up, where I'm encountering the error.

  9. KATT commented on Jul 9, 2025

    @KATT
    Member

    Okay sooo

    1. Server kicks off prefetch
    2. Server renders loading state
    3. Browser picks up stream
    4. When rendering in browser, sometimes the data is available immediately, leading to a hydration error

    This is an annoying race condition indeed. And a useSuspenseQuery() should circumvent it

    https://github.com/Icestonks/react-query-trpc-test

    Let me have a play and see if it's something we can fix.

  10. KATT commented on Jul 9, 2025

    @KATT
    Member

    Ah, note to self, I can only reproduce it reliably when first building and starting the project as then both the:

    • "loading state" is cached
    • & the result of the prefetch is cached
  11. KATT commented on Jul 9, 2025

    @KATT
    Member

    Okay, I've debugged this now & appears that you can reproduce it without tRPC using only @tanstack/react-query.

    @Icestonks, I've made a PR to your project to showcase it over @ Icestonks/react-query-trpc-test#4

    Could you open an issue in https://github.com/TanStack/query instead?

  12. added
    🐜 blocked: external bug
    and removed on Jul 9, 2025
  13. KATT commented on Jul 9, 2025

    @KATT
    Member

    Possibly related: TanStack/query#9157 where tryResolveSync(promise) was added which might be the root cause of this hydration issue

  14. margs163 commented on Jun 11, 2026

    @margs163

    Is this issue solved? Because I followed all of the documentation steps for setting up trpc and react query with SSR, however I am still getting hydration issues

  15. trpc-bot commented on Nov 3, 2026

    @trpc-bot
    Collaborator

    🤖 KATT traced this to TanStack Query's hydration (it reproduces without tRPC): with an unawaited prefetch, the server can render useQuery's loading state while the browser already has the data. Read those prefetches with useSuspenseQuery, or await the prefetch for components that use useQuery. #7838 updates the docs to say so. The fix belongs in TanStack Query, where it's tracked in TanStack/query#9399.

  16. github-actions commented on Nov 4, 2026

    @github-actions
    Contributor

    This issue has been locked because we are very unlikely to see comments on closed issues. If you are running into a similar issue, please create a new issue. Thank you.

  17. locked as resolved and limited conversation to collaborators on Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    blocked🐜 blocked: external bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions