generateImageMetadata
Learn how to generate multiple images in a single Metadata API special file.
You can use generateImageMetadata to generate different versions of one image or return multiple images for one route segment. This is useful for when you want to avoid hard-coding metadata values, such as for icons.
Parameters
generateImageMetadata function accepts the following parameters:
params (optional)
An object containing the dynamic route parameters object from the root segment down to the segment generateImageMetadata is called from.
| Route | URL | params | 
|---|---|---|
| app/shop/icon.js | /shop | undefined | 
| app/shop/[slug]/icon.js | /shop/1 | { slug: '1' } | 
| app/shop/[tag]/[item]/icon.js | /shop/1/2 | { tag: '1', item: '2' } | 
Returns
The generateImageMetadata function should return an array of objects containing the image's metadata such as alt and size. In addition, each item must include an id value which will be passed as a promise to the props of the image generating function.
| Image Metadata Object | Type | 
|---|---|
| id | string(required) | 
| alt | string | 
| size | { width: number; height: number } | 
| contentType | string | 
Image generation function props
When using generateImageMetadata, the default export image generation function receives the following props:
id
A promise that resolves to the id value from one of the items returned by generateImageMetadata. The id will be a string or number depending on what was returned from generateImageMetadata.
params (optional)
A promise that resolves to an object containing the dynamic route parameters from the root segment down to the segment the image is colocated in.
Examples
Using external data
This example uses the params object and external data to generate multiple Open Graph images for a route segment.
Version History
| Version | Changes | 
|---|---|
| v16.0.0 | idpassed to the Image generation function is now a promise that resolves tostringornumber | 
| v16.0.0 | paramspassed to the Image generation function is now a promise that resolves to an object | 
| v13.3.0 | generateImageMetadataintroduced. |