0

I'm trying to dynamically generate metadata for my Next.js 13.4 app using the following code:

export async function generateMetadata(
    { params, searchParams }: Props,
) {
    try {
        // read route params
        const id = params.itemId;

        // fetch data
        const product = await fetch(`${baseUrl}/catelog/item/find/${id}`, {cache: 'no-store'}).then((res) => res.json());

        return {
            title: `Example | ${product.product_name}`,
            description: product.product_description ?? `best selling items at Example store`,
            openGraph: {
                images: [product.product_image],
                title: `Example | ${product.product_name}`,
                description: product.product_description ?? `best selling items at Example store`,
                type: "website"
            },
        };
    } catch (e) {
        return {
            title: `Example | Not found`,
            description: `Product not found`,
        }
    }
}

This code works fine in development, but I'm encountering an error when trying to build the app. The error message is:

Type error: Type 'ResolvingMetadata | undefined' does not satisfy the constraint 'ResolvingMetadata'.
  Type 'undefined' is not assignable to type 'Promise<ResolvedMetadata>'.

I'm not sure how to resolve this error. Can someone please help me understand the root cause of this issue and provide guidance on how to fix it? Thank you!

2
  • @jacob still getting the same error with npm run build, Type error: Type 'ResolvingMetadata | undefined' does not satisfy the constraint 'ResolvingMetadata'. Type 'undefined' is not assignable to type 'Promise<ResolvedMetadata>'. 28 | if ('generateMetadata' in entry) { 29 | checkFields<Diff<PageProps, FirstArg<MaybeField<TEntry, 'generateMetadata'>>, 'generateMetadata'>>() > 30 | checkFields<Diff<ResolvingMetadata, SecondArg<MaybeField<TEntry, 'generateMetadata'>>, 'generateMetadata'>>() | ^ 31 | } Commented Aug 12, 2023 at 5:27
  • Could you give a sample code project can produce your error by any share code platform? Commented Aug 12, 2023 at 13:02

1 Answer 1

0

Build error goes away when you replace it with below snippet.

export async function generateMetadata(): Promise<Metadata>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.