Why does SSR with Next.js runs queries twice?
I have the following code following the official docs and I constantly see two requests. Why? Isn't supposed the dehydrate pass the data
automagically? - I am using also the _app.tsx config from the docs (https://tanstack.com/query/v4/docs/react/guides/ssr)
Code:
When I see the logs on the API, I get 2/3 requests everytime I reload.SSR | TanStack Query Docs
React Query supports two ways of prefetching data on the server and passing that to the queryClient.
Prefetch the data yourself and pass it in as initialData
8 Replies
quickest-silver•3y ago
It's likely this: https://tanstack.com/query/v4/docs/react/guides/ssr#staleness-is-measured-from-when-the-query-was-fetched-on-the-server
SSR | TanStack Query Docs
React Query supports two ways of prefetching data on the server and passing that to the queryClient.
Prefetch the data yourself and pass it in as initialData
provincial-silverOP•3y ago
I don't see it clear, I'm using hydration and I expect only one request to the db on initial load. The one used by the getServerSideProps. Is that correct? In my code I'm replicating what you sent @TkDodo 🔮
I'm trying to play with staletime, still it doesn't work. Should I load initial data from the dehydrated query? @TkDodo 🔮 ?
I think the docs are wrong, the dehydrated stated is not passed automatically to the query you use later on, you have also to pass initialData
quickest-silver•3y ago
It is if you use the Hydrate component
provincial-silverOP•3y ago
Ah, so hydrate is meant to always run once in server + once in client?
My goal is to not have it run twice. But have the benefit of the sub-queries
Should I combine initialData + hydrate to have it fully working as I expect? Meaning having only one request and cache in sub-components that use the same query?
cc @TkDodo 🔮 - Could you clarify please? I still don't get it 😅
quickest-silver•3y ago
please show your app.tsx
my guess is you are not using the
<Hydrate> component
- dehydrate runs on the server, you fetch there, then pass it to app.tsx as a prop
- from there, you pass that prop to the <Hydrate> component, which will put those queries into the query cache you have on the client
with that, you don't need initialData, because data is already in the cache after that
if you see another fetch, it should be a background refetch, because of staleness. Set staleTime to avoid itprovincial-silverOP•3y ago
This is my app.tsx:
When I do a console.log on I can see the dehydrateQuery, though the requests still happens twice, once on the server and once on the client:
Could it be the staleTime or anyother missing options?
quickest-silver•3y ago
yes, it's staleTime. I think I said this 3 times now 🙂
provincial-silverOP•3y ago
God, you're right. 🙏 sorry and thanks!