WEBDASTUDIO
0%
Initializing Global Environment
Loading core modules...
Back to Blog
Marketing

Dynamic OpenGraph Image Generation using Vercel Satori and Next.js

By Md Tanjim Rahat Badhon
January 25, 2024
8 min read
Dynamic OpenGraph Image Generation using Vercel Satori and Next.js

Static OpenGraph banners do not scale for content sites containing thousands of dynamic articles. With Vercel Satori and Next.js, you can convert React HTML layouts directly into optimized PNGs. Let's build a generator.

1. Next.js Image Response API

Next.js exposes the ImageResponse constructor from next/og. It intercepts request routes to render customized HTML components, styles them with Flexbox rules, maps variables (like post titles, authors, and images), and exports them as flat images.

2. Font Parsing and Size Optimization

Satori requires absolute font assets to draw characters accurately. We pull compressed Google Fonts using server-side fetches, caching them in edge memory to prevent generator latencies. All styled elements must use Flexbox since grid layouts are not supported in Satori engine models.

Conclusion

Programmatic OG image generation boosts user sharing conversions. It ensures every dynamic link shared on social media is branded with correct headers, vector art, and user avatars.

Share this writeup:

Related Articles