Skip to content
Source
Sign inSign up
TanStack/query

server error with nextjs , and hydration in v5 #8528

Send to my agent
Closedartainjast(GitHub profile, opens in a new tab) opened this issue 10 comments

Describe the bug

server error with nextjs , and hydration in v5

Your minimal, reproducible example

https://stackblitz.com/github/tanstack/query/tree/main/examples/react/nextjs-app-prefetching?embed=1&theme=light&preset=node&file=src/index.tsx

Steps to reproduce

  1. it does not need any steps , it occur when you starting the app (event on dev or production mode).

Expected behavior

as a user , i don't except this error happen , and it make some conflicts with building and compiling for production

How often does this bug happen?

Every time

Screenshots or Videos

Image

Platform

  • OS : [MacOS - stackblitz]

Tanstack Query adapter

react-query

TanStack Query version

"^5.64.0" ,

TypeScript version

"5.7.2"

Additional context

No response

  1. Thanks for filing. I’m not 100% sure what’s going on, but I debugged it a bit and the gist is that the initialPromise is passed as undefined to query.fetch here:

    https://github.com/TanStack/query/blob/bd3296bfed9b4db1882e42fe378f2f35dc240381/packages/query-core/src/hydration.ts#L215-L219

    That leads to us invoking the queryFn (because we have no promise to re-use), but the queryFn doesn’t exist yet, so it errors:

    https://github.com/TanStack/query/blob/18953be6f61553f4ed3dd888945dfa1683aa6e40/packages/query-core/src/retryer.ts#L153

    When inspecting the promise we get from the server, it’s there, but initialPromise is still undefined:

    Image

    @juliusmarminge does this have something to do with .then(deserializeData) ? Before that step, the promise seems to still be there. deserializeData is just our default one:

    https://github.com/TanStack/query/blob/bd3296bfed9b4db1882e42fe378f2f35dc240381/packages/query-core/src/hydration.ts#L17-L19

  2. TkDodo added labelsbug

  3. Regarding this issue, could this be the reason that the data are not fetched in the server side? As it can be seen in the provided link, the /pokemon api should be fetched on server side since it uses the prefetch, hydration and query client techniques. However, it can be observed in the chrome dev tools that the query is fetched in client side.

  4. Regarding this issue, could this be the reason that the data are not fetched in the server side? As it can be seen in the provided link, the /pokemon api should be fetched on render side since it is used the prefetch, hydration and query client techniques. However, it can be observed in the chrime dev tools that the query is rendered in client side.

    Actually i follow that issue and reached here , i amazed why that happens. That is good question too

  5. However, it can be observed in the chrome dev tools that the query is fetched in client side.

    It is fetched on the client side because we don’t pass an initialPromise to query.fetch, so it tries to execute an actual fetch, without a queryFn (so this can’t work).

    @juliusmarminge @Ephem this issue is baffling to me. maybe there is something that changed with next15 and using .then on this weird ReactPromise doesn’t work anymore?

  6. hmm am i missing something? seems to work fine for me locally:

    Image

  7. Huh,I was debugging it on stackbiltz (in a separate tab). Maybe it’s just a stackblitz issue then?

  8. hmm yea - installed the same node version (18.20) locally and ran with that and still working fine

  9. Hm, works locally for me too. It's an interesting one though. For some reason we are still trying to fetch during SSR and not finding the queryFn when doing so, but when I add some basic logging for both the promise and the queryFn both seem to be there correctly. 🤔

  10. hmm am i missing something? seems to work fine for me locally:

    Image

    i tried to implement this on nextjs 15.3 , and this happen yet . what react-query version you used ?

  11. Closing this since it's working fine for anything but stackblitz.

  12. TkDodo closed this

Sign in to join this conversation.