Skip to content

OG images

@hamur/og-image draws the image that a link preview shows (og:image and twitter:image) from an HTML template, once per page. You write the template in app/og/, a page calls defineOgImage, and the module writes the tags.

sh
bun add @hamur/og-image
ts
import type { OgImageOptions } from "@hamur/og-image"
import { defineConfig } from "hamur"
import ogImage2 from "@hamur/og-image"

const ogImage = {
  template: "Post",
  fonts: ["Inter:400", "Inter:700"],
  runtime: { bun: true },
} satisfies OgImageOptions

export default defineConfig({
  modules: [ogImage2],
  site: { url: "https://example.com", name: "Example" },
  ogImage,
})

site.url is needed: the tags hold absolute URLs. The options are template (the name used when a call names none, Default), width and height (1200 × 630), prerender (true), runtime, fonts and emoji. Every key, default and error message is in the reference.

Templates

A file in app/og/ whose name starts with an upper-case letter is a template, and the name is the file's. Two kinds exist:

  • a .ts file whose default export is defineOgTemplate(render): render(props, { site }) returns the HTML of the image's <body>, with inline CSS;
  • a component, in the library of the app: .tsx for React, Preact and Solid, .vue or .svelte. Its default export gets the props as component props. The html adapter has .ts templates only.
ts
import { defineOgTemplate, html } from "@hamur/og-image/template"

export default defineOgTemplate(
  (props: { title: string; author?: string }, { site }) => html`
  <div style="display:flex;flex-direction:column;justify-content:space-between;width:100%;height:100%;padding:72px;background:#0b1020;color:#fff;font-family:Inter,sans-serif">
    <h1 style="font-size:72px;margin:0">${props.title}</h1>
    <p style="font-size:32px;margin:0">${props.author ? html`${props.author} · ` : ""}${site.name}</p>
  </div>`,
)

The html tag escapes every value you interpolate; a nested html result is kept as written. Props reach a template as the page passed them, so write each value through html (or escapeHtml in a plain string). A layer's app/og/ adds templates, the app's file wins by name, and a package-provided Default shows the title, the description and the site name.

A component template is rendered by your library's own server render, with its escaping, and no script or client code ships:

tsx
export default function Post(props: { title: string; author?: string }) {
  return (
    <div
      style={{
        display: "flex",
        flexDirection: "column",
        justifyContent: "center",
        width: "100%",
        height: "100%",
        padding: 72,
        background: "#0b1020",
        color: "#fff",
        fontFamily: "Inter, sans-serif",
      }}
    >
      <h1 style={{ fontSize: 72, margin: 0 }}>{props.title}</h1>
      {props.author ? <p style={{ fontSize: 32 }}>{props.author}</p> : null}
    </div>
  )
}

A component gets no site; read useSiteConfig() where you build the props. Typegen writes .hamur/types/og-image.d.ts, so template: "Post" and its props are checked against the template's parameter.

Calling defineOgImage

A page or a layout calls defineOgImage of its adapter (@hamur/og-image/react, /vue, /preact, /svelte, /solid or /html) while it renders.

ts
import { defineOgImage } from "@hamur/og-image/html"

export default function Post(): string {
  defineOgImage({ props: { title: "Hello", description: "A first post" }, alt: "Hello, a first post" })
  return "<article><h1>Hello</h1></article>"
}

It writes og:image, its width, height and type, twitter:image and twitter:card summary_large_image, plus og:image:alt and twitter:image:alt when you pass alt. The URL is <site.url><app.baseURL>_hamur/og/<page>.<v>.png, where <v> changes with the template, its file, the props and the size, so a cache never serves an old image. A later call on the page replaces an earlier one. An opengraph-image file for the route wins over the call.

Prerendered images

On a page the build prerenders, the image is a file: .output/public/_hamur/og/<page>.<v>.png, drawn with Chrome through Bun.WebView. The template's document runs no script and may fetch nothing but files of .output/public.

The build needs Chrome. It looks in BUN_CHROME_PATH, $PATH, the usual install folders and Playwright's cache. A build with such a page and no Chrome fails with:

text
og-image needs Chrome for Bun.WebView; set BUN_CHROME_PATH or ogImage.prerender: false
sh
export BUN_CHROME_PATH=/usr/bin/chromium
hamur generate

ogImage.prerender: false keeps the build free of Chrome; the pages then get an image only through runtime. An image that takes more than 10 seconds, or a template that throws, fails the build with the page and the template's name.

Images at run time

A page that is rendered per request has no file. With ogImage.runtime set, its tags carry a signed URL, and the server draws the image when a crawler asks, then keeps it in the cache storage mount.

PresetogImage.runtimeImage
bun, bun-compile{ bun: true }PNG, drawn by Bun.WebView; SVG when no Chrome is found
cloudflare, cloudflare-pages{ cloudflare: "BROWSER" }PNG, drawn by the Browser Rendering binding; the binding must be in wrangler.json
any other server preset{}SVG
staticnone; the key has no effect

Facebook, X and LinkedIn do not show SVG images, so on a preset without a browser prefer prerendered pages. The build says when run-time images will be SVG.

Signing

A run-time URL ends in ?p=<props>&s=<signature>. The server checks the signature in constant time before it reads anything else and answers 403 for a missing or wrong one, so nobody can make it draw images you did not ask for. The key is HAMUR_OG_SECRET, at least 32 characters; the server stops at load with a message when it is unset and runtime is on.

sh
export HAMUR_OG_SECRET="$(openssl rand -hex 32)"

hamur dev signs nothing and needs no secret. A runtimeConfig key that reads HAMUR_OG_SECRET fails at load, so the secret never reaches a page.

Fonts and emoji

A screenshot never waits on the network, so fonts are written into the template's HTML. fonts lists "<family>:<weight>" entries; each is looked up in the providers of @hamur/fonts, when it is installed, then in public/fonts/<family>-<weight>.(woff2|woff|ttf|otf). A font found nowhere fails the build:

text
og-image: font "Inter:400" not found in @hamur/fonts providers or public/fonts/

With emoji: "noto" (the default), a template whose text holds an emoji also gets Noto Color Emoji faces, so font-family: Inter, sans-serif shows them. emoji: false leaves them out. A server that renders at run time carries the font data in its bundle.

Trying a template

In hamur dev, <page>.<v>.html answers the template's document, so you can open the image in a tab; the .png URL renders it per request, unsigned and uncached.