Void
Return Open Graph images from Void routes written in Hono JSX, on Cloudflare or on the Node.js target.
Void routes run on Hono, and Takumi renders Hono JSX as it renders React, so a route returns an ImageResponse with no adapter package. On Cloudflare, Takumi runs on its WebAssembly backend. On the Node.js target, it runs on its native addon.
Install Takumi and Hono
npm i takumi-js honoUse Hono's JSX in routes
Skip this step if your app already sets importSource to hono/jsx. An app on React can keep React's JSX, which Takumi renders as well.
import { defineConfig } from "vite-plus";
import { voidPlugin } from "void";
export default defineConfig({
oxc: { jsx: { runtime: "automatic", importSource: "hono/jsx" } },
plugins: [voidPlugin()],
});Create the route
A handler can return a Response, so return the ImageResponse directly.
import { ImageResponse } from "takumi-js/response";
import { defineHandler } from "void";
export const GET = defineHandler((c) => {
const title = c.req.query("title") ?? "Hello from Void";
return new ImageResponse(
<div
style={{
display: "flex",
alignItems: "center",
justifyContent: "center",
width: "100%",
height: "100%",
background: "#4f46e5",
}}
>
<h1 style={{ fontSize: 72, color: "white" }}>{title}</h1>
</div>,
{ width: 1200, height: 630 },
);
});JSX escapes title as text, so a request cannot inject markup through it.
Request the image
Run npm run dev, then open /og?title=Hello.
Load data while rendering
An async component can fetch or query what the image shows. Takumi waits for it and draws what Hono's c.html() would send as the page, so a Suspense boundary shows its children, not its fallback:
import { Suspense } from "hono/jsx/streaming";
import { ImageResponse } from "takumi-js/response";
import { defineHandler } from "void";
const Title = async ({ slug }: { slug: string }) => {
const post = await fetch(`https://api.example.com/posts/${slug}`).then((res) => res.json());
return <h1 style={{ fontSize: 72 }}>{post.title}</h1>;
};
export const GET = defineHandler((c) => {
const slug = c.req.param("slug");
if (!slug) {
return c.notFound();
}
return new ImageResponse(
<div style={{ display: "flex", width: "100%", height: "100%", padding: 64 }}>
<Suspense fallback={<h1>Loading</h1>}>
<Title slug={slug} />
</Suspense>
</div>,
{ width: 1200, height: 630 },
);
});Hono's html template from hono/html works too, with its values escaped and awaited.
Draw text in other scripts
The built-in Geist font covers Latin text. Characters no font covers draw as boxes, and Takumi prints a warning that names them. To draw them, load a font only when the image needs it with loadFallbackFonts. This route downloads the Japanese characters of its title from Google Fonts:
import { googleFonts } from "takumi-js/helpers";
import { ImageResponse } from "takumi-js/response";
import { defineHandler } from "void";
export const GET = defineHandler((c) => {
const title = c.req.query("title") ?? "こんにちは、Void";
return new ImageResponse(<h1 style={{ fontSize: 72 }}>{title}</h1>, {
width: 1200,
height: 630,
loadFallbackFonts: (characters) =>
googleFonts({ families: ["Noto Sans JP"], text: characters.join("") }),
});
});Takumi keeps the files it downloads in a store that later renders share, so a later render that needs the same file skips the download. See Fonts for choosing families and handling fonts that fail to download.
Deploy
On Cloudflare, void deploy bundles Takumi's WebAssembly binary into the Worker, where it makes up most of the size. See Cloudflare Workers for its size and the limits to check.
On the Node.js target, keep takumi-js in your app's dependencies. The build then leaves takumi-js out of dist/ssr, and Node loads it from node_modules at runtime, with the native addon for the server's platform. Run npm install where the server runs, so it installs the addon for that platform rather than the one you built on.
Next steps
Style the image with Styles, load fonts with Fonts, and see ImageResponse for every option.
Last updated on