Cookie Consent by Free Privacy Policy Generator Aktuallisiere deine Cookie Einstellungen ๐Ÿ“Œ Auto Generate Open Graph Images in NextJS


๐Ÿ“š Auto Generate Open Graph Images in NextJS


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

Next.js has a feature that allows you to generate Open Graph images using code. This feature is useful when you want to generate images for your blog posts, social media posts, or any other content that requires an image.

In a previous article we looked at Adding Metadata to NextJS in this article, we will look at how to generate Open Graph images using code in Next.js.

Install @vercel/og

The first thing you need to do is install the @vercel/og package. This package is a utility for generating Open Graph images. You can install it using npm or yarn.

npm install @vercel/og
# or
yarn add @vercel/og

Generate Images Using Code

Once you have installed the @vercel/og package, you can start generating images using code. The @vercel/og package provides a generate function that you can use to generate images.

Here is an example of how you can generate an Open Graph image using code:

import { generate } from "@vercel/og";

const image = await generate({
  title: "Hello, World!",
  description: "This is an example Open Graph image",
  theme: "light",
  md: true,
  fontSize: 64,
  images: [
    {
      src: "https://example.com/image.jpg",
      width: 800,
      height: 600,
    },
  ],
});

console.log(image);

In this example, we are generating an Open Graph image with the title "Hello, World!" and the description "This is an example Open Graph image". We are using the light theme, rendering the description in markdown, setting the font size to 64, and using an image as the background.

The generate function returns a base64-encoded image that you can use in your application.

Creating A Image Route

To take advantage of this in a Next.js blog you can create a new route that will generate the Open Graph image for a specific blog post. This route will take the blog post content and generate an image that can be used in the Open Graph meta tags.

In this example we're going to use the ImageResponse to generate the image and return it as a response. Create a new file in app/api/og of route.tsx and add the following code:

import { ImageResponse } from 'next/og';

export async function GET() {
  return new ImageResponse(
    (
      <div
        style={{
          fontSize: 40,
          color: 'black',
          background: 'white',
          width: '100%',
          height: '100%',
          padding: '50px 200px',
          textAlign: 'center',
          justifyContent: 'center',
          alignItems: 'center',
        }}
      >
        ๐Ÿ‘‹ Hello, World!
      </div>
    ),
    {
      width: 1200,
      height: 630,
    },
  );
}

This code will generate an Open Graph image with the text "๐Ÿ‘‹ Hello, World!" in the center of the image. The image will have a width of 1200 pixels and a height of 630 pixels.

Navigate to http://localhost:3000/api/og/route to see the generated image.

Blog Posts Open Graph Images

You can use this approach to generate the open graph images for your blog posts.

We will do this by passing in a querystring of the blog post title and use this content to generate the image.

import { ImageResponse } from 'next/og'

export async function GET(request: Request) {
    const { searchParams } = new URL(request.url);

    return new ImageResponse(
        (
            // ImageResponse JSX element
            <div
                style={{
                    background: 'black',
                    width: '100%',
                    height: '100%',
                    display: 'flex',
                    flexDirection: 'column',
                    alignItems: 'center',
                    justifyContent: 'center',
                    padding: '0 100px',
                }}
            >
                <h1 style={{fontSize: 64, color: 'white', display: 'block' }}>
                    { searchParams.get('title') }
                </h1>

            </div>
        ),
        {
            width: 1200,
            height: 630,
        }
    )
}

Now you can navigate to http://localhost:3000/api/og/route?title=Hello%20World to see the generated image with the title "Hello World".

Within you generateMetadata function you can now use this route to generate the Open Graph image for your blog post.

    openGraph: {
            title: post.title,
            description: post.description,
            images: [
                {
                    url: `/api/op?title=${post.title}`,
                    width: 1200,
                    height: 630,
                    alt: post.title,
                    type: "image/png",
                }
            ],
            type: "article"
        },
    }

This will generate the Open Graph image for the blog post with the title of the post.

Now when you share the blog post on social media the image will be generated using the content of the blog post.

This is a great way to generate Open Graph images for your blog posts using code in Next.js.

Technical Details

  • Recommended OG image size: 1200x630 pixels
  • @vercel/og uses Satori and Resvg to convert HTML and CSS into PNG
  • @vercel/og API reference
...



๐Ÿ“Œ Auto Generate Open Graph Images in NextJS


๐Ÿ“ˆ 56.01 Punkte

๐Ÿ“Œ Auto Generate Open Graph Images With Laravel


๐Ÿ“ˆ 40.43 Punkte

๐Ÿ“Œ How to create alias for only one half of the command? Like "git graph", where graph="log --all --graph"?


๐Ÿ“ˆ 37.53 Punkte

๐Ÿ“Œ What is NextJs?ย  Why Should You Use it in 2023?ย  NextJs Pros and Cons Guide


๐Ÿ“ˆ 31.14 Punkte

๐Ÿ“Œ NextJS examples: 60 popular websites built with NextJS and headless CMS


๐Ÿ“ˆ 31.14 Punkte

๐Ÿ“Œ 3 Exciting Improvements Between NextJS 14 And NextJS 13


๐Ÿ“ˆ 31.14 Punkte

๐Ÿ“Œ Enhancing Graph Data Embeddings with Machine Learning: The Deep Manifold Graph Auto-Encoder (DMVGAE/DMGAE) Approach


๐Ÿ“ˆ 30.59 Punkte

๐Ÿ“Œ Generate sitemap.xml and robots.txt in Nextjs


๐Ÿ“ˆ 27.5 Punkte

๐Ÿ“Œ How to generate dynamic OG image using new NextJs with App directory


๐Ÿ“ˆ 27.5 Punkte

๐Ÿ“Œ Graph-Datenbanken: Graph Query Language soll ISO-Standard werden


๐Ÿ“ˆ 25.02 Punkte

๐Ÿ“Œ MISP 2.4.111 event-graph View event-graph.js cross site scripting


๐Ÿ“ˆ 25.02 Punkte

๐Ÿ“Œ Oracle Big Data Graph up to 2.x Spatial/Graph Remote Code Execution


๐Ÿ“ˆ 25.02 Punkte

๐Ÿ“Œ Developer's guide to Microsoft Graph | Learn Together: Building Apps with Microsoft Graph


๐Ÿ“ˆ 25.02 Punkte

๐Ÿ“Œ CVE-2020-36518 | Oracle Big Data Spatial and Graph Big Data Graph denial of service


๐Ÿ“ˆ 25.02 Punkte

๐Ÿ“Œ CVE-2021-42340 | Oracle Big Data Spatial and Graph Big Data Graph denial of service


๐Ÿ“ˆ 25.02 Punkte

๐Ÿ“Œ CVE-2021-41184 | Oracle Big Data Spatial and Graph Big Data Graph cross site scripting


๐Ÿ“ˆ 25.02 Punkte

๐Ÿ“Œ A graph extension written in C: quick and easy deployment of graph model on relational database


๐Ÿ“ˆ 25.02 Punkte

๐Ÿ“Œ Build a GNN-based real-time fraud detection solution using the Deep Graph Library without using external graph storage


๐Ÿ“ˆ 25.02 Punkte

๐Ÿ“Œ Intro to Graph and Native Graph Databases


๐Ÿ“ˆ 25.02 Punkte

๐Ÿ“Œ CVE-2023-28606 | MISP prior 2.4.169 Event-Graph Node Tooltip js/event-graph.js cross site scripting


๐Ÿ“ˆ 25.02 Punkte

๐Ÿ“Œ CVE-2023-46589 | Oracle Big Data Spatial and Graph 3.0.4 Big Data Graph unknown vulnerability


๐Ÿ“ˆ 25.02 Punkte

๐Ÿ“Œ CVE-2019-10798 | rdf-graph-array up to 0.3.0-rc6 rdf.Graph.prototype.add privileges management


๐Ÿ“ˆ 25.02 Punkte

๐Ÿ“Œ Text to Knowledge Graph Made Easy with Graph Maker


๐Ÿ“ˆ 25.02 Punkte

๐Ÿ“Œ Meet Graph-Mamba: A Novel Graph Model that Leverages State Space Models SSM for Efficient Data-Dependent Context Selection


๐Ÿ“ˆ 25.02 Punkte

๐Ÿ“Œ How to Test Graph Quality to Improve Graph Machine Learning Performance


๐Ÿ“ˆ 25.02 Punkte

๐Ÿ“Œ This AI Paper from Siemens Explores the Integration of the Graph Modality in LLM for General Graph Instruction Following Tasks


๐Ÿ“ˆ 25.02 Punkte

๐Ÿ“Œ Large Generative Graph Models (LGGMs): A New Class of Graph Generative Model Trained on a Large Corpus of Graphs


๐Ÿ“ˆ 25.02 Punkte

๐Ÿ“Œ What is a Call Graph? And How to Generate them Automatically


๐Ÿ“ˆ 24.44 Punkte

๐Ÿ“Œ How to generate graph in Pdf


๐Ÿ“ˆ 24.44 Punkte

๐Ÿ“Œ How to Generate a Battery Usage Graph on Windows 10


๐Ÿ“ˆ 24.44 Punkte

๐Ÿ“Œ How to Generate and Evaluate the Performance of Knowledge Graph Embeddings?


๐Ÿ“ˆ 24.44 Punkte

๐Ÿ“Œ Open Graph images


๐Ÿ“ˆ 22.93 Punkte











matomo