Next.js Discord

Discord Forum

generateMetadata not being called on page?

Answered
Otterhound posted this in #help-forum
Open in Discord
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>
}
Answered by joulev
ah, pages router then it doesn't work
View full answer

10 Replies

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 /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
the code above would work if in app/foo/page.tsx
OtterhoundOP
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