generateImageMetadata is a Next.js App Router API used to generate metadata for dynamic images. It is commonly used with Open Graph and Twitter image routes to define image metadata for better SEO and social media sharing.
- Generates metadata for multiple dynamic image variants.
- Used with Open Graph and Twitter image routes.
- Supports dynamic image generation based on route parameters.
- Improves SEO and social media sharing.
Syntax:
export function generateImageMetadata() {
return [
{
id: "1",
alt: "Image description",
size: {
width: 1200,
height: 630,
},
contentType: "image/png",
},
];
}
The generateImageMetadata() function returns an array of image metadata objects. Each object can contain the following properties:
- id: Unique identifier for the generated image.
- alt: Alternative text describing the image.
- size: Width and height of the generated image.
- contentType: MIME type of the generated image (for example, image/png).
Steps to Create a Next.js Project
Follow the steps given below:
Step 1: Create a New Next.js Application
Create a Next.js project using the following commands:
npx create-next-app@latest next-image-metadata-app
cd next-image-metadata-app
Step 2: Project Structure
Create the following files:
next-image-metadata-app/
│
├── app/
│ ├── opengraph-image.js
│ └── page.js
├── package.json
└── ...
Step 3: Create the Home Page
Create the following file:
app/page.jsexport default function Home() {
return (
<main