Skip to content

About Open Graph Images

· 3 min

This post explains how to configure Open Graph (OG) images in the Astro AntfuStyle Theme.

What Are Open Graph Images?#

OG images, short for Open Graph images, are metadata images used by social media platforms to visually represent a webpage when shared. Originating from Facebook’s Open Graph protocol, they make shared content more engaging.

Platforms like Facebook, Twitter, LinkedIn, and Discord use these images. While Twitter uses its own protocol (Twitter Cards), “OG image” is often used as a general term for these images.

Configuring OG Images#

There are three ways to configure OG images in this theme, listed by priority:

Method 1: Specify the ogImage field in the Markdown/MDX frontmatter with a custom image, which should be saved in /public/og-images/. For example:

src/content/blog/about-open-graph-images.md
---
title: About Open Graph Images
# The specified OG image must be saved: `public/og-images/specified-og-image.png`
ogImage: specified-og-image.png
---
WARNING

If the image isn’t found in /public/og-images/, a fallback og-image.png will be used.

Method 2: Set ogImage to true or remove the field to automatically generate an OG image, saved in /public/og-images/. The generated filename will match the Markdown/MDX file, or the directory if it’s named index.md or index.mdx. For example:

src/pages/blog/index.mdx
---
title: Blog
# Auto-generated OG image is saved: `public/og-images/blog.png`
ogImage: true
---
WARNING

OG images won’t generate if the frontmatter has redirect, draft: true, or is missing the title field.

Method 3: Set ogImage to false to prevent generation and use the fallback OG image instead. A fallback OG image named og-image.png is stored in /public/og-images/, ensuring that any page shared on social platforms displays an image.

IMPORTANT

If you need to regenerate the fallback OG image or after modifying FEATURE.ogImage, delete og-image.png to create a new fallback image.

Template for Auto-Generated OG Images#

The template (located in plugins/og-template/markup.ts) for auto-generated OG images is built with HTML + UnoCSS and processed by natemoo-re/satori-html. It takes three parameters:

For details on how to replace the logo in the template, refer to Advanced Configuration - Customizing Logo.

“Below are the OG images generated with bgType set to ‘plum’, ‘rose’, ‘dot’, and ‘particle’:

How This Theme Automatically Generates OG Images#

This theme uses a custom remark plugin (located in plugins/remark-generate-og-image.ts) to handle the automatic generation of OG images. The decision to use a remark plugin instead of creating Astro endpoints was made for the following reasons:

Extra Tips#

Auto-generated OG images are compressed with lovell/sharp. For custom images, consider manual compression.

If an image with the same name as the Markdown/MDX file (or the directory for index.md/index.mdx) already exists in /public/og-images/, it won’t be regenerated. To regenerate it, manually delete the corresponding image from the /public/og-images/ directory.

If you want to globally disable the auto-generation, set FEATURES.ogImage to false or [false, {...}] in src/config.ts.

Thank you for reading. Happy coding! 💃

> share on twitter / mastodon
> cd ..