generateMetadata not being called on page?
Answered
Otterhound posted this in #help-forum
OtterhoundOP
I have a page I am trying to implement
generateMetadata on and it never seems to be called. I have included the smallest example I can make, and I don't see any logging outputted. Similarly, I dont see the meta tags either. Anyone have any ideas why this might be? import { Metadata, ResolvingMetadata } from "next"
export async function generateMetadata(
{ params }: any,
parent: ResolvingMetadata
): Promise<Metadata> {
console.log("Hello World!")
return {
title: "Piercy Test",
}
}
export default function Foo() {
return <div>Bar</div>
}10 Replies
@Otterhound I have a page I am trying to implement `generateMetadata` on and it never seems to be called. I have included the smallest example I can make, and I don't see any logging outputted. Similarly, I dont see the meta tags either. Anyone have any ideas why this might be?
TS
import { Metadata, ResolvingMetadata } from "next"
export async function generateMetadata(
{ params }: any,
parent: ResolvingMetadata
): Promise<Metadata> {
console.log("Hello World!")
return {
title: "Piercy Test",
}
}
export default function Foo() {
return <div>Bar</div>
}
this is weird, the code looks fine to me... is this a page.tsx file or a layout.tsx file?
if no: you can only use the metadata api in page/layout files
if yes: give us a reproduction repository
OtterhoundOP
Erm. I am a little new to this, so I am not sure of the difference. What I can say is the file exists under
Ill try come up with a small reproduction project.
/pages/foo.tsx. So, presumably that makes it a page and it should work?Ill try come up with a small reproduction project.
Answer
it only works in the app router
for the pages router you use next/head https://nextjs.org/docs/pages/api-reference/components/head
the code above would work if in
app/foo/page.tsxOtterhoundOP
App Router looks like its new and shiney, so might be the better way to go before i get to deep into it
OtterhoundOP
Ok, yeah it works when using app router. I am going to swap to App router, this project is fairly new, and swapping now will probably save me future headaches.
Thanks for the info
Thanks for the info