Next.js Discord

Discord Forum

Exporting a Metadata Object in layout.js seems to override the contents of page.js

Answered
Barbary Lion posted this in #help-forum
Open in Discord
Avatar
Barbary LionOP
I used create-next-app to create my project, and I'm having the above issue.

Before I export the metadata object, my page.js file works as expected and shows a heading of 'One Player'. But as soon as I create the exported metadata object, the page greys out and nothing is displayed.

Any ideas on how to troubleshoot this?
Image
Image
Answered by Plague
What you're doing in the Play page won't even work, the useRouter hook from next/navigation doesn't even have a query property on it.

This isn't pages router. If you need access to the searchParams in your page server component you need to grab it from the props.
View full answer

13 Replies

Avatar
The example code you showed above shows that your page will have the title "One Player" are you saying this isn't happening when using the metadata object inside the layout.js?
Avatar
Barbary LionOP
yes exactly
Avatar
Hmm, what is your folder structure?
Avatar
Barbary LionOP
Image
Avatar
Yeah everything seems right, I've encountered this issue before as well specifically with layouts. It's most likely a bug, and it seems to happen with title, and description tags. For now you can just export the metadata object in the page and it'll work.

Hopefully they will fix this soon.
Avatar
Barbary LionOP
so I've tried doing it that way and it works, but the issue is that I need to use useRouter, which needs me to turn the page into a client component, which means that I can't export metadata
Image
Image
Avatar
What you're doing in the Play page won't even work, the useRouter hook from next/navigation doesn't even have a query property on it.

This isn't pages router. If you need access to the searchParams in your page server component you need to grab it from the props.
Answer
and if you need it in a client component you need to use useSearchParams
Avatar
Barbary LionOP
cool I'll try this out, thanks
Avatar
Barbary LionOP
this worked, thanks a lot for your help @Plague 🎉