Next.js Discord

Discord Forum

generateMetadata throws an error when building

Unanswered
Oliver Sagala posted this in #help-forum
Open in Discord
Here's a reproduction of my code.
// app/blog/[slug].tsx

export async function generateMetadata({ params }: Props, parent?: ResolvingMetadata): Promise<Metadata> {
  try {
    const article = await getArticle(params.slug);
    const previousImages = (await parent)?.openGraph?.images || [];

    return {
      title: article.title,
      description: article.excerpt,
      openGraph: {
        images: [article.featuredImage.sourceUrl, ...previousImages],
      },
    };
  } catch (error) {
    notFound();
  }
}

export default async function Page({ params }: Props) {
  let article: Article;
  try {
    article = await getArticle(params.slug);
  } catch (error) {
    notFound();
  }

  return <ArticlePage article={article} />;
}


Am getting the following error on building. Not sure where am going wrong. Any help will be appreciate.
src/app/(site)/blog/[slug]/page.tsx
Type error: Type 'ResolvingMetadata | undefined' does not satisfy the constraint 'ResolvingMetadata'.
  Type 'undefined' is not assignable to type 'Promise<ResolvedMetadata>'.

0 Replies