JSXImage
← All frameworks

Add an Open Graph image to Next.js

The Open Graph image is the picture that shows when a link is shared on LinkedIn, Slack, X, Discord or iMessage.With the snippet below, every Next.js page gets its own, with its own title, made from a URL.No image files, no build step and no account.

An Open Graph image with the title How we cut build times in half

The snippet

Put it in app/blog/[slug]/page.tsx (App Router).

tsx
export async function generateMetadata({ params }) {
  const post = await getPost((await params).slug);
  const og = new URL("https://jsximg.com/tools/og-image-generator.png");
  og.searchParams.set("title", post.title);
  og.searchParams.set("description", post.excerpt);
  og.searchParams.set("site", "acme.dev");
  return {
    title: post.title,
    openGraph: { images: [{ url: og.href, width: 1200, height: 630 }] },
    twitter: { card: "summary_large_image", images: [og.href] },
  };
}
  • getPost stands for however you load the page's data.
  • For a page without data, build the URL the same way in export const metadata.
  • No opengraph-image.tsx or ImageResponse needed, so nothing counts toward the edge function size limit.

What the URL takes

  • title, required: the page's title. Long titles get a smaller size so they fit.
  • description, optional: a line under the title, such as the page's description.
  • site: your domain, shown under the title.
  • bgColor and fgColor: your brand's background and text colors, as hex such as ff5a1f.
  • googleFont: any Google Font by name, such as Space+Grotesk. Without it, the image uses the built-in Geist.

Try values in the Open Graph image generator. Check a page with opengraph.xyz after you deploy.

Your own design, without the mark

The free image uses one design with a small jsximage.com mark in the corner. With an account, the template is yours: a React component styled with Tailwind, in your fonts and colors, with any props you want. You sign its URLs in your code, the same one-line change.

Other frameworks