logo
n min

📷

Dynamically Generating OGP Images during SSG Build with Next.js App Router.

Posted on

Updated on

Introduction

This article introduces the implementation method of implementing OGP generation on this site, and how to dynamically generate OGP during SSG build with Next.js App Router.

In this article, when we mention Next.js, we are referring to Next.js App Router.

Target Audience

  • Those using Static Site Generation (SSG) with Next.js App Router
  • Those who want to implement OGP generation
  • Those who want to generate OGP during build time

Environment

  • Next.js 15.3.2
  • @vercel/og (included with Next.js)

OGP Basics

What is OGP?

OGP stands for Open Graph Protocol, a protocol used to specify information such as images, titles, and descriptions that appear when content is shared on social networks.

The information displayed in link cards like the one below is an example of OGP.

Main OGP Meta Tags

The main meta tags used in OGP are as follows:

Implementing OGP in Next.js

1. Static OGP Configuration

In Next.js, you can set up static OGP by placing a file named opengraph-image.(jpg|jpeg|png|gif) in any route. It will be automatically included in the metadata.

<head>

If the file is not placed in the corresponding route, the file from the parent route will be used.

2. Dynamic OGP Generation

With App Router, since @vercel/og is included, you can easily generate OGP using next/og.

2.1 Basic Implementation

By generating in a route like /posts/[slug]/og.png/route.tsx, you can retrieve the OGP at https://example.com/posts/slug/og.png.

In this configuration, we can generate OGP during build time without using edge functions like /api/og.

src/app/posts/[slug]/og.png/route.tsx

2.2 Font Loading

When generating OGP images that include Japanese text, you need to explicitly specify the font. Here's an example implementation that loads a subset font from Google Fonts.

src/app/posts/[slug]/og.png/route.tsx

2.3 Applying Generated OGP

To apply the generated OGP image to your site, set the metadata as follows.

src/app/posts/[slug]/page.tsx

3. Implementation Notes

  1. Font Loading

    • When including Japanese text, you must explicitly specify the font
    • Even with only alphabets, I encountered errors in my environment
    • You can use Google Fonts' subsetting feature to load only the necessary characters
  2. Image Size

    • The recommended size for OGP images is 1200x630 pixels
    • Since many social networks crop to 1:1, it's recommended to place important content in the center 630x630 area
  3. Build-time Generation

    • Specify dynamic = 'force-static' to generate statically during build
    • You need to specify the parameters to generate in generateStaticParams
  4. Error Handling

    • Proper error handling is necessary for font loading and image generation
  5. Style Specification

    • The display property of div must be explicitly set to either flex | block | none
  6. Metadata Configuration

    • Metadata can be set using generateMetadata
    • Missing or incorrect metadata settings can negatively impact SEO

Summary

With Next.js App Router, you can generate dynamic OGP images during SSG build time by making some adjustments to the routing.

By paying attention to proper Japanese font loading and metadata configuration, and setting appropriate image sizes, you can generate better OGP images.

Reference Articles

This Site's OGP