GitHub is an example of using a dynamic thumbnail; when you share a repository, its thumbnail represents the "near" up-to-date stars, contributors, issues, and more.
To explore more Open Graph properties, visit this website: Open Graph Protocol.
2. Use cases
Generate thumbnails for thousands of articles or product pages automatically.
Synchronize thumbnail designs.
Raise awareness with your own thumbnail design featuring your brand.
3. How to Make Og-Image Dynamic
Traditionally, websites usually declare a fixed image for the og:image property.
To make it dynamic, you should replace this fixed image URL with an API endpoint that receives the identifier of each site (such as the article slug or profile ID) and then generates an image based on the received object identifier.
4. Approach #1 - Screenshot HTML
In the first version of dynamic-thumbnail-service, I used Puppeteer (a Node library that provides a high-level API to control headless Chrome or Chromium) to capture an HTML page and return it as an image.
The flow looks like this:
Article ID -> Service -> Generate HTML for thumbnail -> Capture HTML -> Return Image
This model is simple and easy to implement but has some drawbacks:
Resource consumption: Because it uses a headless browser, it requires significant resources.
Slow response: It must generate HTML and then capture it as an image, resulting in a slow response time, taking about 1-2 seconds for a request.
In this version, we no longer use Puppeteer to capture HTML and return images. Instead, we utilize the @vercel/og library, which employs Satori as its core engine. Satori is a library that converts HTML and CSS into SVG.
In this project, I use Next.js for implementation, which includes @vercel/og in its core, eliminating the need to import this library.
This approach makes the 2nd version of dynamic-thumbnail-service-v2 up to 5 times faster than the previous one.
Cons:
Satori currently supports a limited subset of HTML and CSS features. You can find the list of supported CSS features here.
This is dynamic-thumbnail-service-v2. It's X5 faster compared to the previous version.
In this version, we no longer use Puppeteer (a headless browser) to capture HTML and then return images. Instead, we utilize the @vercel/og library, which employs Satori as its core engine.
(Satori: Enlightened library is used to convert HTML and CSS into SVG))
Usage
Run
Run local
pnpm install
pnpm run dev
Run Docker
docker run -p 3001:3000 huanttok/dynamic-thumbnail-service-v2:latest