Next.js Discord

Discord Forum

Is it okay to do fetching in layout.tsx?

Answered
zhefciad posted this in #help-forum
Open in Discord
I have a navbar in my layout.tsx. Should I fetch it in layout and pass it down to navbar or should I fetch it inside my Navbar which is a client component? What's the standard?
Answered by B33fb0n3
fetch it only if it's part of layout. For example when you load the navigation links from a database, then this data is part of the layout. So you want to fetch it
View full answer

8 Replies

@zhefciad I have a navbar in my layout.tsx. Should I fetch it in layout and pass it down to navbar or should I fetch it inside my Navbar which is a client component? What's the standard?
fetch it only if it's part of layout. For example when you load the navigation links from a database, then this data is part of the layout. So you want to fetch it
Answer
in my case it's the userProfile from my auth, so I guess I should fetch it in the client?
thank you
sure thing. Please mark solution
Just an option, something I do at work, when the user authenticates I write back a nav only cookie, which the navbar relies on for rendering.... Someone could manipulate that cookie because its plan json.... but thats just them manipulating the UI, there is still checks on navigation to ensure they are allowed to access the page.

Just my two cents on it.