I need to lookup an entry from the database and generate the metadata and render the actual page contents.
At the moment, I am doing it like so:
export const generateMetadata = async ({
params: { questionSlug },
}: Props): Promise<Metadata> => {
const sql = await connectToPostgres();
const question = await findQuestion(sql, questionSlug);
if (!question) {
return {};
}
return {
alternates: {
canonical: `https://ray.run/questions/${question.slug}`,
},
title: question.question,
};
};
const Page = async ({ params: { questionSlug } }: Props) => {
const sql = await connectToPostgres();
const question = await findQuestion(sql, questionSlug);
if (!question) {
return notFound();
}
// ...
};
but this means that I have to make the same database query twice to render the same page.
Is there a way to fetch data once and use it in both methods?