Consider the following server side component that's part of some page (page.js) in the app directory, representing a person:
export default async function Person({ params }) {
const person = await getPerson(params.id);
return (
<h1> {person.name} </h1>
<p> {person.bio} </p>
)
}
Obviously, the intent here is to dynamically render new pages, with the idea being that the URL looks something like /person/{id}
, where ID is the unique ID of each person.
The issue I'm running into is that I need to set metadata for the page. For example, I want the title to be person.name
, and the description to be person.description
. From what I'm reading, the only way to do that in NextJS 13 with the app directory is to use generateMetadata
, which would look something like this:
export async function generateMetadata({ params }) {
const person = await getPerson(params.id);
return {
title: person.name,
description: person.description
}
}
I'm sure you can see the issue now: I need to make the exact same request (getPerson
) twice in the same page. From what I've seen, there isn't a way to pass data between generateMetadata
and the page component.
The <Head>
component from next/head
seems like the ideal solution, but it doesn't appear to work in the app directory with NextJS 13.
Am I missing something really obvious here? Or is this an absolutely massive oversight by Next's devs?
fetch
underneathgetPerson
– Melmela