@hamur/og-image
The first-party Open Graph image module. Add it to modules and set its options under the ogImage config key. A page calls defineOgImage, and the module writes the og:image and twitter:image tags of an image drawn from an HTML template in app/og/.
// hamur.config.ts
import ogImage from "@hamur/og-image"
export default defineConfig({
modules: [ogImage],
site: { url: "https://example.com", name: "Acme" },
ogImage: { runtime: { bun: true } },
})
default
The module, as defineModule({ meta: { name: "og-image", configKey: "ogImage" } }). Its setup checks ogImage, reads the templates of app/og/ and of each layer's app/og/ (the file is scanned, not run), adds the server-side app plugin that writes the tags once the page finished rendering, and adds the route GET <app.baseURL>_hamur/og/**. Typegen writes .hamur/types/og-image.d.ts, which gives OgTemplates the props of each template.
Build errors: og-image: app/og/post.ts: a template name starts with an upper-case letter, og-image: app/og/Post.ts must export default defineOgTemplate(…), og-image: app/og/Post.tsx must export a default component, og-image: two templates named "Post": app/og/Post.ts, app/og/Post.vue (one folder holds a .ts template and a component of one name) and ogImage.template "Post" is not a template in app/og/ (have: Default). A runtimeConfig key that reads HAMUR_OG_SECRET fails at load: runtimeConfig.ogSecret reads HAMUR_OG_SECRET, which signs the og-image URLs; rename the key.
The build rasterises the image of each prerendered page that calls defineOgImage with Chrome through Bun.WebView (a fresh headless Chrome, one tab per image, at most 4 at once, closed after the last image), and writes .output/public/_hamur/og/<page>.<v>.png. Chrome is looked for at the first image, in BUN_CHROME_PATH, $PATH, the standard install locations and Playwright's cache; a build with such a page and no Chrome fails with og-image needs Chrome for Bun.WebView; set BUN_CHROME_PATH or ogImage.prerender: false. The template's document runs no script, and every request it makes is failed before it leaves Chrome except a GET of a file of .output/public, which the build answers itself; data: URLs load. An image that does not finish loading in 10 seconds fails the page: og-image: /blog/a: template "Post" did not finish loading in 10 s, followed by what it was waiting for, such as (waiting for the load event; unanswered requests: https://og.hamur.invalid/logo.svg), and a template that throws fails it with og-image: /blog/a: template "Post" threw: <message>.
With ogImage.runtime.cloudflare on the cloudflare and cloudflare-pages presets, the build checks the merged .output/wrangler.json: og-image: ogImage.runtime.cloudflare is "BROWSER" but wrangler.json has no "browser": { "binding": "BROWSER" }, and og-image: Browser Rendering quick actions need compatibility_date 2026-03-24 or later (wrangler.json has 2026-01-10). It logs once [og-image] preset "node" has no browser at run time; pages rendered there get SVG images, which Facebook, X and LinkedIn do not show where run-time images are SVG, [og-image] ogImage.runtime.cloudflare has no effect on preset "node" (and ogImage.runtime.bun) for a key of another preset, and [og-image] ogImage.runtime has no effect on the static preset.
OgImageOptions
| Key | Type | Default |
|---|---|---|
template | string | "Default": the template of a call that names none |
width | number | 1200: an integer from 1 to 16384 |
height | number | 630: an integer from 1 to 16384 |
prerender | boolean | true: a prerendered page gets the static image URL |
runtime | false | { cloudflare?: string; bun?: boolean } | false: a page rendered at run time gets no tags; set, it gets a signed URL. cloudflare names the Browser Rendering binding that renders PNG images on the cloudflare presets, bun: true renders them with Bun.WebView on bun and bun-compile; elsewhere, {} included, the images are SVG |
fonts | string[] | []: "<family>:<weight>" such as "Inter:700"; each is resolved at build and written into the template's HTML as an @font-face rule with the file as a data: URL (see Fonts and emoji) |
emoji | "noto" | false | "noto": adds Noto Color Emoji the same way when the template's text holds an emoji |
Font errors: og-image: font "Inter:400" not found in @hamur/fonts providers or public/fonts/.
Load errors: ogImage.<key> is not an option, ogImage.width must be an integer from 1 to 16384 (got 0) (and height), ogImage.prerender must be a boolean (got "yes"), ogImage.runtime must be false or { cloudflare?, bun? } (got true), ogImage.runtime.cloudflare must be a binding name (got ""), ogImage.runtime.bun must be a boolean (got "yes"), ogImage.fonts[0] "Inter" must be "<family>:<weight>" such as "Inter:400" and ogImage.emoji must be "noto" or false (got true).
With ogImage.runtime set, the server reads HAMUR_OG_SECRET when it loads and stops with og-image: set HAMUR_OG_SECRET to render images at run time when it is unset, or og-image: HAMUR_OG_SECRET must be at least 32 characters. hamur dev signs nothing and needs no secret.
Fonts and emoji
Each ogImage.fonts entry is looked up when the build starts, first in the providers of @hamur/fonts (remoteProviders, in the order of fonts.priority, default google, bunny, fontshare, fontsource) when that package is installed, then in public/fonts/<family>-<weight>.(woff2|woff|ttf|otf) of the app and its layers (case, spaces, dashes and underscores of the family are ignored). Files a provider names are downloaded once into <cacheDir>/og-image/fonts/ and never written into the repository. The template's HTML is written with a <style> in front that holds one @font-face rule per face, so the family is used by its name (font-family: Inter) and a screenshot never waits on the network, which the build blocks anyway. A font found nowhere fails the build, also when no page calls defineOgImage:
og-image: font "Inter:400" not found in @hamur/fonts providers or public/fonts/
A file a provider names that cannot be downloaded fails it with og-image: font "Inter:400": could not download <url>: <status>.
With emoji: "noto" the text of a template that holds a character of \p{Extended_Pictographic} (or a flag) also gets the faces of Noto Color Emoji that cover its emoji, from the Google provider, declared as Noto Color Emoji and under each family of ogImage.fonts, so font-family: Inter, sans-serif shows them; a template with other families lists "Noto Color Emoji" in its font-family. A server that renders images at run time (ogImage.runtime on a preset with a browser) carries the font data in its bundle, every emoji face included, and fetches nothing; hamur dev carries the emoji faces only when a template file holds an emoji.
OgTemplates
The props of each template by name. The package declares Default: { title?: string; description?: string }; typegen adds app/og/<Name>.ts as <Name>: <the props parameter of its template>.
OgImageInput
What defineOgImage takes: { template?: N; props?: OgTemplates[N]; alt?: string; width?: number; height?: number }, N a key of OgTemplates.
@hamur/og-image/template
// app/og/Post.ts
import { defineOgTemplate, html } from "@hamur/og-image/template"
export default defineOgTemplate(
(props: { title: string; author?: string }) =>
html` <h1 style="font:700 72px sans-serif">${props.title}</h1>
${props.author ? html`<p style="font:400 32px sans-serif">${props.author}</p>` : ""}`,
)
Props reach a template as the page passed them, unescaped. A template is HTML a browser renders, so every value it writes must be escaped: write it with html, or pass each value through escapeHtml.
defineOgTemplate
defineOgTemplate<P>(render: (props: P, context: OgTemplateContext) => string | OgHtml): OgTemplate<P>. The function returns the HTML of the image's <body> with inline CSS; the module wraps it in a document with no script (a Content-Security-Policy of default-src 'none', inline styles, data: fonts and images of the site) sized width × height. context.site holds the request's site.url, site.name and site.description. A template name matches /^[A-Z][A-Za-z0-9]*$/; the app's file wins by name over a layer's, and both over the package's Default, which shows the title (else site.name), the description (else site.description) and site.name in the corner.
html
html`…` is a tagged template that returns an OgHtml. Each interpolated value is escaped as escapeHtml does, except an OgHtml (a nested html`…`), which is kept as written; an array is joined, and null, undefined and false write nothing. Default is written with it.
OgHtml
What html returns; String(value) is its HTML.
escapeHtml
escapeHtml(value: unknown): string escapes &, <, >, " and ', for a template written as a plain string.
OgTemplate, OgTemplateContext, OgTemplateProps, OgComponentProps
The type of a template, of its second parameter, OgTemplateProps<T>, the props of a template type, and OgComponentProps<T>, the props of a component template (a Vue component's $props, the second parameter of a Svelte component, else the first parameter of a function component), as typegen writes OgTemplates.
Component templates
app/og/<Name>.<ext> in the app's library (.tsx for React, Preact and Solid, .vue, .svelte) is a template too: its default export is a component, which gets the props as component props.
// app/og/Post.tsx
export default function Post(props: { title: string }) {
return (
<div style={{ display: "flex", padding: 80, background: "#0b1020", color: "#fff" }}>
<h1>{props.title}</h1>
</div>
)
}
The adapter's server render turns it into HTML as a root of its own, for the path of the page the image belongs to, with the library's own escaping (no raw-HTML escape hatch is needed or used for props) and with its hydration markers (comments and Solid's _hk) removed. No script or client code is shipped, and the component gets no site (a .ts template reads it from its second parameter). The html adapter has .ts templates only. A template's version, and so its image URL, changes with the component's file and every file it imports by a relative path.
@hamur/og-image/server
The module's door. Its route GET <app.baseURL>_hamur/og/<page>.<v>.<ext> reads the URL, which carries ?p=<p>&s=<s> in the run-time form:
- Outside
hamur devit answers only withogImage.runtimeset, and checks the signaturesbefore anything else, in constant time; a missing or wrongsanswers403,text/plain,og-image: bad signature. A URL over 16384 characters answers414; a<page>.<v>whose version does not match itsp, or an unknown template, answers404. - A signed
.pngURL answersimage/png: on bun withogImage.runtime.bun, rendered byBun.WebViewas the build does, its requests answered from the public files; on cloudflare withogImage.runtime.cloudflare, byenv[<binding>].quickAction("screenshot", { html, viewport }), whose browser loads the template's URLs from the site. Each render has the 10 second limit and at most 4 run at once. Without Chrome on bun, or without the binding on cloudflare, the route answers SVG and the server logs once[og-image] no Chrome found for Bun.WebView (set BUN_CHROME_PATH); run-time images are SVGor[og-image] env.BROWSER is not a Browser Rendering binding; run-time images are SVG. - A signed
.svgURL, the run-time form on every other preset, answersimage/svg+xml: the template's document as XHTML in a<foreignObject>of awidth×height<svg>, with the document'sContent-Security-Policyalso sent as a header. Facebook, X and LinkedIn do not show SVG images. - An image answers
cache-control: public, max-age=31536000, immutable, since its URL names its content, and is kept in thecachestorage mount underog:<s>; a later request is answered from there without running the template. An SVG answered in place of a PNG is sent withcache-control: no-storeand not kept. - In
hamur dev,.pngURLs are rendered byBun.WebViewper request (SVG without Chrome) and answered withcache-control: no-store. - In
hamur dev,<page>.<v>.htmlanswers the template's document for that page, so the image can be previewed in a tab;hamur devaccepts its URLs unsigned.
@hamur/og-image/react, @hamur/og-image/vue, @hamur/og-image/preact, @hamur/og-image/svelte, @hamur/og-image/solid, @hamur/og-image/html
defineOgImage
function defineOgImage<N extends keyof OgTemplates = "Default">(input?: OgImageInput<N>): void
Called in a page or a layout during its render, it writes og:image (absolute, from site.url), og:image:width, og:image:height, og:image:type (image/png, or image/svg+xml for an .svg URL), og:image:alt when alt is given, twitter:image, twitter:image:alt and twitter:card summary_large_image. A later call on the page replaces an earlier one. The URL is <site.url><app.baseURL>_hamur/og/<page>.<v>.<ext>: <page> the path below app.baseURL (index for /), <v> 8 hex digits that change whenever the template, its file, the props or the size change. A prerendered page gets that URL as is; at run time with ogImage.runtime it carries ?p=<props>&s=<signature>; in hamur dev, ?p=<props>. A page that is neither gets no tags and the server logs once per route [og-image] /dash calls defineOgImage but is not prerendered and ogImage.runtime is off; no og:image is written. The run-time URL ends .png where a run-time rasteriser serves the preset (ogImage.runtime.bun on bun, ogImage.runtime.cloudflare on cloudflare) and .svg elsewhere.
An opengraph-image file that applies to the route wins: the call writes nothing there, and hamur dev logs [og-image] /blog/a has app/pages/blog/opengraph-image.png; defineOgImage writes no og:image there. The server's tags reach the client in the payload state hamur:og-image; after a client navigation the page has none from the call.
Errors at the call: defineOgImage() must be called inside a Hamur app, og-image: no template "X" in app/og/ (have: Default, Post), og-image: props of "Post" must be JSON values (got a function at props.render) and og-image: width must be an integer from 1 to 16384 (got 0) (and height).