Next.js Discord

Discord Forum

GenerateMetaData in Layout

Answered
LaPerm posted this in #help-forum
Open in Discord
Avatar
LaPermOP
Does GenerateMetaData work in layout or just pages in Next13 app router? I am pulling my data in from a CMS and so I want default meta data. Does this not work on the layout file?

export async function generateMetaData(): Promise<Metadata> { const { metaTitle, metaDescription, ogImage } = await sanityFetch<ISettings>({ query: settingsQuery, }); return { title: metaTitle ', description: metaDescription, openGraph: { images: [ogImage.url], }, }; }

Or is there a way to pass a prop from the layout to all children components? If this doesn't work I can fetch the data in the layout function and pass it down somehow?
Answered by risky
it is generateMetadata not generateMetaData
View full answer

8 Replies

Avatar
risky
it is generateMetadata not generateMetaData
Answer
Avatar
LaPermOP
Ok so it is detecting the function but my title tag is not being set and the data definately is coming through.
Avatar
LaPermOP
export async function generateMetadata(): Promise<Metadata> {
const { metaTitle, metaDescription, ogImage } = await sanityFetch<ISettings>({
query: settingsQuery,
});

return {
metadataBase: new URL('http://localhost:3000'),
title: {
default: metaTitle,
template: %s | ${metaTitle},
},
description: metaDescription,
openGraph: {
images: [ogImage?.url],
},
robots: {
index: false,
},
};
}
this is on pages as well
Avatar
risky
ohh is that what you meant... i didn't realise you wanted the template...
Avatar
LaPermOP
I am just trying different things to get it to show I don’t mind if it’s just title without template but no dynamic values are working