Next.js Discord

Discord Forum

Transmit data from generateMetadata function

Answered
Mini Satin posted this in #help-forum
Open in Discord
Avatar
Mini SatinOP
Hey there, building a web page editor and i'm wondering if there's a way of transmitting data from generateMetadata function ?
So the point here is that i need to fetch my page datas from my database to generate the metadatas.
I also need the page datas to render my page so right now i've got something that look like this :
export const generateMetadata = async () => {
  console.log("generating metadata");
  const page = await getPage({ path: "/" });
  return {
    title: page.title,
    //... other metadatas
  };
};

export default async function Page(props) {
  console.log("props", props);
  const page = await getPage({ path: "/" });
  return <Renderer page={page} />;
}

Obviously there's nothing in props that permit me to retrieve the metadatas so i might split my model to avoid fetching the same data twice.
But there's still some common shared data like the author of the page etc... So be able to transmit the data from the generateMetadata function could be convenient to me. Also because generateMetadata always run before the rendering of the Page component...

So if someone knows a way i would be glad to try it out !
Answered by B33fb0n3
to avoid fetching the same data twice.
wrap your getPage function inside a react cache and then even if you write the function twice, the function will only be executed once
View full answer

7 Replies

Answer
Avatar
happy to help
Avatar
@B33fb0n3 happy to help
Avatar
Australian Freshwater Crocodile
You get all the questions solved before we can even take a look 😭😭 leave some for us😭😭
Jk
Avatar
@Australian Freshwater Crocodile You get all the questions solved before we can even take a look 😭😭 leave some for us😭😭 Jk
Avatar
There are a lot of questions, that you (and others) answer before me. I skipping is lot in the last time πŸ™‚
I saw you in some threads. Keep up the good work πŸ‘
Avatar
@B33fb0n3 There are a lot of questions, that you (and others) answer before me. I skipping is lot in the last time πŸ™‚ I saw you in some threads. Keep up the good work πŸ‘
Avatar
Australian Freshwater Crocodile
I'm just kidding! I always read the threads to learn and your answers are on point so keep teaching us trough replies, I don't mind πŸ˜„ haha. And thanks, I try!