I am currently in need of retrieving information from the database, generating metadata, and displaying the page content. The current method I am using is as follows:
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();
}
// ...
};
However, this approach requires querying the database twice to render the same page.
Is there a more efficient way to retrieve data once and utilize it in both functions?