---
url: https://void.cloud/guide/og-images.md
description: Give each post its own social preview image.
---

# Open Graph Images

When someone shares a post, the social app reads the page’s `og:image` tag and fetches that URL. Give each post an image route so its preview shows the right title and author.

## Create the image route {#usage}

Return an `OgImageResponse` from `routes/og/[slug].ts`. It renders the same [HTML templates](../reference/api/html.md) and [Hono JSX](./image-generation.md#jsx-types) as `ImageResponse`, with a default size of 1200 × 630.

```ts
// routes/og/[slug].ts
import { defineHandler } from 'void';
import { html } from 'void/html';
import { OgImageResponse } from 'void/image';
import { getPost } from '../../src/posts.ts';

export const GET = defineHandler(async (c) => {
  const slug = c.req.param('slug');
  const post = slug && (await getPost(slug));
  if (!post) {
    return c.notFound();
  }

  return new OgImageResponse(
    html`<article style="height: 100%; padding: 80px; background: #0f172a; color: white">
      <h1 style="font-size: 72px">${post.title}</h1>
      <p style="font-size: 36px">${post.author}</p>
    </article>`,
  );
});
```

Open `/og/hello-world` to see the PNG for the post with slug `hello-world`. The image URL must be accessible without signing in so social apps can fetch it. See [where images render](./image-generation.md#setup) for local development and deployment.

## Add the image to a page

If your loader returns a `post`, use it in [`head()`](./pages-routing/head.md) to give the page and its preview the same title. Set `og:image` to the route you just created:

```ts
// pages/posts/[slug].server.ts
import { defineHead } from 'void';

interface Props {
  post: { title: string; slug: string; description: string; author: string };
}

export const head = defineHead<Props>((c, { post }) => ({
  title: post.title,
  meta: [
    { property: 'og:type', content: 'article' },
    { property: 'og:title', content: post.title },
    { property: 'og:description', content: post.description },
    { property: 'og:url', content: c.req.url },
    { property: 'og:image', content: `/og/${encodeURIComponent(post.slug)}` },
    { property: 'og:image:width', content: '1200' },
    { property: 'og:image:height', content: '630' },
    { property: 'og:image:alt', content: `${post.title} by ${post.author}` },
    { name: 'twitter:card', content: 'summary_large_image' },
  ],
}));
```

Void expands the image path to an absolute URL using the incoming request. For a [prerendered page](./pages-routing/head.md#absolute-crawler-urls), supply the full public URL because there’s no request at build time.

## Image sizes

The default size works for most landscape previews. To make a square thumbnail, pass `{ width: 600, height: 600 }` as the second argument. Update the dimensions in the page metadata too. For a square X card, use `twitter:card="summary"`.

If one service needs a different crop, give it its own route and point its image tag there. For example, `twitter:image` can point to a 1200 × 600 card while `og:image` points to the 1200 × 630 version for other services.

## Caching

In production, `OgImageResponse` sets `Cache-Control: public, max-age=86400`. A browser can reuse the same URL for 24 hours. Social apps may keep a preview longer. When a cover changes and you need readers to see it right away, give it a new URL and update `og:image`. Purging Void’s cache won’t remove a social app’s copy.

Override `headers` to choose a different lifetime. Development has no default cache header. To avoid rendering again when another visitor requests the same cover, [cache the route at the edge](./image-generation.md#cache-generated-images).

## Await rendering

Use `renderOgImage(content, options?)` when you need bytes instead of an HTTP response. It takes the same [rendering options](./image-generation.md#options) and defaults to 1200 × 630. Await it to [handle a rendering error](./image-generation.md#await-rendering) before sending headers. If you return the bytes in a `Response`, set its content type and cache policy.
