can a root layout/component be a Client Component or is always a Server Component?
Answered
Jumbo flying squid posted this in #help-forum
Jumbo flying squidOP
I was just curious if its possible to create a root html/body with minimal interactive/dynamic-apis react (or no interactive/dynamic-apis at all) and passing that from NextJS to the browser?
Or no and that root html/body layout/page is always a Server Component (and any interactive/dynamic-apis must be in a Client Comp under it)?
NextJS docs makes me assume its always a Server component: "Starting with the root layout, which is a Server Component, you can then render certain subtrees of components on the client by adding the "use client" directive."
Or no and that root html/body layout/page is always a Server Component (and any interactive/dynamic-apis must be in a Client Comp under it)?
NextJS docs makes me assume its always a Server component: "Starting with the root layout, which is a Server Component, you can then render certain subtrees of components on the client by adding the "use client" directive."
Answered by luis_llanes
You should always keep layouts as Server Components (which is the default), to improve performance and make use of the Server features and Next.js specific APIs such as metadata, etc. Especially the RootLayout.
It still works if you add "use client" at the very top but you shouldn't, It's not what Layouts are made for, you're loosing all the nice features for no reason.
It still works if you add "use client" at the very top but you shouldn't, It's not what Layouts are made for, you're loosing all the nice features for no reason.
2 Replies
I believe layout is always a server component, but a template.tsx can be either server or client.
You should always keep layouts as Server Components (which is the default), to improve performance and make use of the Server features and Next.js specific APIs such as metadata, etc. Especially the RootLayout.
It still works if you add "use client" at the very top but you shouldn't, It's not what Layouts are made for, you're loosing all the nice features for no reason.
It still works if you add "use client" at the very top but you shouldn't, It's not what Layouts are made for, you're loosing all the nice features for no reason.
Answer