Next.js Discord

Discord Forum

Why do nextjs sends multiple fetch requests from server side components

Answered
Asiatic Lion posted this in #help-forum
Open in Discord
Asiatic LionOP
I have a fetch call in my root layout.tsx to pass data to a client side react context to provide initial data for my client
when I inspect the backend logs I see for every full page refresh I send 6 request for a single fetch call
Answered by Asiatic Lion
I found out it was a weird bug with posthog provider that caused the rerender of layout in dev environment
ty for your time @B33fb0n3
View full answer

4 Replies

@Asiatic Lion I have a fetch call in my root layout.tsx to pass data to a client side react context to provide initial data for my client when I inspect the backend logs I see for every full page refresh I send 6 request for a single fetch call
maybe some of them are due to reactStrictMode. To avoid them, make sure to wrap them into React.cache. When the same request is made again, the cache will be used
Asiatic LionOP
i have this outside of layout component
const getUserDetails = cache(prefetchUserDetails)
and in the layout compoenent use it like this
const defaultValues = await getUserDetails()
still requests sends out multiple times
Asiatic LionOP
I found out it was a weird bug with posthog provider that caused the rerender of layout in dev environment
ty for your time @B33fb0n3
Answer