Next.js Discord

Discord Forum

Is it possible to pass server data to layouts?

Answered
Transvaal lion posted this in #help-forum
Open in Discord
Transvaal lionOP
I want to get list of recent posts from the server and pass it in the layout, is that possible?
Answered by Transvaal lion
yes, so the recent posts widget will appear in multiple pages
View full answer

34 Replies

Transvaal lionOP
or it should be done in the page
The layout will be rendered before the page loads. Also there are several reasons why you shouldn’t fetch in your layout. Layout is for lay-out-ing stuff. Inside your page you can fetch your data. If you need the same data in multiple components, that you render in your layout, fetch multiple times. The cache will hit and you have the same data in your components. Makes sense? @Transvaal lion
Transvaal lionOP
yes, so the recent posts widget will appear in multiple pages
Answer
Transvaal lionOP
thats why I thought it is best to use in the layout
so it is possible to fetch data in layout?
Technically it is possible, yes. But as I said you shouldn’t want to fetch in layout.

Create a function that gets your data and call this function in multiple places of your app, that needs these data. Do not fetch inside your layout.
Transvaal lionOP
thank you
Transvaal lionOP
@B33fb0n3 I dont see answered tag, how can I mark this post as resolved? close the post?
@Transvaal lion it's perfectly fine to get data from a layout
if you need the data in all the pages below this layout
typically you can get data from an RSC layout, and then feed a client context to make them available to all client component beneath
Creating a function that fetch your data, wrapping it with "cache", and using it in multiple places, is the equivalent of this but for RSCs
so to sum it up you can:
- craft a function to get data, using "cache" to guarantee deduplication
- use in your layout to feed a client context
- use the function in other RSCs directly
Transvaal lionOP
@Eric Burel thanks, what I did was I created a component called "Latest Posts" and inside that component I fetched the data
@Transvaal lion <@769111741098622976> thanks, what I did was I created a component called "Latest Posts" and inside that component I fetched the data
yeah makes sense, Next allows you to fetch data as close as possible to where you need them
the layout is an extreme case, where the data are needed in multiple client components
typically we put the current user in a context there
and it's really for client components only, you don't need that for RSC
@B33fb0n3 " If you need the same data in multiple components, that you render in your layout, fetch multiple times." that's true for RSCs but not for client components
"there are several reasons why you shouldn’t fetch in your layout." sorry I've already asked that but can you list some reasons?
As stated by Next documentation:
"Layouts can fetch data. View the Data Fetching section for more information."
@Eric Burel As stated by Next documentation: "Layouts can fetch data. View the Data Fetching section for more information."
Technically it is possible, yes. But as I said you shouldn’t want to fetch in layout.
@B33fb0n3 > Technically it is possible, yes. But as I said you shouldn’t want to fetch in layout.
Why would you repeat that to beginners asking questions with no justifications or any details, while the documentation states the exact opposite?
It's fine to go against what the documentation says sometime, it's how we make the framework progress and create new knowledge, but why do you refuse to give any explanation on what makes you think something that goes against what Next.js core team says?
Do you have insider knowledge that we don't have?
I think we already talked about that topic. Are you reading my message?

Docs:
Technically it is possible, yes

Personal recommendation:
But as I said you shouldn’t want to fetch in layout.
Transvaal lionOP
@Eric Burel thank you for the great explanation, if I fetch the data inside the component, the data is cached for every page that uses that layout right? for example, in my case I want to have a recent posts widget that is inside the layout, and the same layout is used for blogs, news, contact and etc. If the component is marked as 'use client' I cant fetch the data?
- "cache" works for the tree of RSCs, so if a layout, a page, a component of the page call the exact same function, wrapped with "cache", data are only fetched once for the whole request
- Client Components will fetch data client-side, so you won't use the same mechanism as for RSCs
in client components it's common to use a React Context to achieve a similar purpose
Transvaal lionOP
@Eric Burel thanks, I think that makes sense now, thank you