Capítulo 167 de 456
Gera imagens dinâmicas (PNG) a partir de JSX e CSS, usado tipicamente para imagens Open Graph, Twitter cards e outras imagens sociais.
next/og, converte um elemento React + opções em uma resposta de imagem via Satori + Resvg (@vercel/og).{ name, data, weight, style }; só suporta ttf, otf, woff (ttf/otf parseiam mais rápido).display: grid).// app/opengraph-image.tsx
import { ImageResponse } from 'next/og'
export const alt = 'My site'
export const size = { width: 1200, height: 630 }
export const contentType = 'image/png'
export default async function Image() {
return new ImageResponse(
(
<div style={{ fontSize: 128, background: 'white', width: '100%', height: '100%', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
My site
</div>
),
{ ...size }
)
}
opengraph-image.tsx, reaproveitando size exportado.// Fonte customizada lida uma vez em module scope (não depende de request)
const interSemiBold = await readFile(join(process.cwd(), 'assets/Inter-SemiBold.ttf'))
export default async function Image() {
return new ImageResponse(
(/* ... */),
{ ...size, fonts: [{ name: 'Inter', data: interSemiBold, style: 'normal', weight: 400 }] }
)
}
| Version | Changes |
|---|---|
| v14.0.0 | ImageResponse movido de next/server para next/og |
| v13.3.0 | importável de next/server |
| v13.0.0 | introduzido via pacote @vercel/og |
opengraph-image.tsx para build-time/request-time via convenção de arquivo.ImageResponse costuma ser usado.