Next.js Discord

Discord Forum

Is there any way to 'use client' on layout and still export metadata?

Answered
Yann posted this in #help-forum
Open in Discord
I want to change navbar when the website resides on home, which works perfectly fine. The problem is that I'm completing that through 'use client' and pathname. as illustrated in the code and when you try to export the metadata from a 'use client' file you're met with an error. Is there any possible to work around that?
Answered by riský
Also make your layout file import a client file of nav that is client to make use of usePathname but not have the layout client
View full answer

6 Replies

Common House-Martin
This component doesnt need to be a client component, you could just get the url like:
 import { headers } from "next/headers";

 const heads = headers()

 const pathname = heads.get('next-url')
also i would recommend using layouts for this and make use of the app folder features
Also make your layout file import a client file of nav that is client to make use of usePathname but not have the layout client
Answer
Another method you could use here is route groups (it's slightly more advanced but would be better heare if you just want to have different on root)
Thank you very much