SEO
Social card templates
Design your own VitNode social card with an OgImageTemplate, register it for a content type or the whole site, and draw cards from your own API route.
VitNode's built-in social images cover most sites with a colour and a logo. When you want your own layout, write a template: a function that returns JSX, drawn to a 1200 Ć 630 PNG by the API.
Theme colours, the logo and fonts set in ogImage reach every template through its props, so set those first.
Write your own template
A template is a function that takes the card's props and returns JSX. VitNode calls it as a plain function, so it cannot use hooks or context. The renderer understands inline style objects and flexbox. Give every element with more than one child display: "flex".
import type { OgImageTemplate } from "@vitnode/api/lib/og-image";
export const StoryCard: OgImageTemplate = ({ siteName, theme, title }) => (
<div
style={{
background: theme.background,
color: theme.foreground,
display: "flex",
flexDirection: "column",
fontFamily: theme.fontFamily,
height: "100%",
justifyContent: "center",
padding: 80,
width: "100%",
}}
>
<span style={{ color: theme.accent, fontSize: 32 }}>{siteName} Ā· Story</span>
<span style={{ fontSize: 72, fontWeight: 700 }}>{title}</span>
</div>
);The props are title, description, siteName, domain, locale, theme, logo and kind (content, profile or site), plus contentTypeId and pluginId on a content card and avatar on a profile card. logo is { src, width, height, wordmark }, ready for an <img>. A template that puts the logo on a background other than theme.background recolors its currentColor with ogImageLogoSrc(logo, "#18181b") from @vitnode/api/lib/og-image.
A plugin registers templates for its own content types in buildApiPlugin:
import { StoryCard } from "./api/og/story-card";
export const newsroomApiPlugin = () =>
buildApiPlugin({
manifest,
ogImageTemplates: { "newsroom.story": StoryCard },
// ...
});An app replaces any card in buildApiConfig. ogImage.templates overrides a plugin's template for a content type, and ogImage.template draws everything no other template covers, profiles and the site card included. A template registered for an id no plugin defines stops the API at startup.
Draw a card from your own route
For a page outside the Content Engine, answer from an API route with ogImageResponse and point the page head at it. Check that the data is public first, because the card is shown to everyone:
import {
ogImageBaseProps,
ogImageResponse,
ogImageVersion,
} from "@vitnode/api/lib/og-image";
import { buildRoute } from "@/api/builders";
export const eventOgRoute = buildRoute({
route: { method: "get", path: "/og/{slug}" /* params, responses */ },
handler: async c => {
const event = await findPublicEvent(c, c.req.param("slug"));
if (!event) return c.json({ error: "Not found" }, 404);
return await ogImageResponse(
c,
{
...ogImageBaseProps(c),
description: event.summary,
kind: "content",
locale: null,
title: event.title,
},
{ version: ogImageVersion(c, [event.title, event.summary]) },
);
},
});buildRoute is your plugin's bound builder from src/api/builders.ts, and findPublicEvent stands for your own query. In the page's head, return openGraph: { image: { url, width: 1200, height: 630, type: "image/png" } }, with url built from the route and the same v. See head arguments and result.
Check the result
Restart the API, then open a card URL in the browser, such as a story's og:image from the social images check. A 200 with Content-Type: image/png showing your layout means the template is used. A 503 means the renderer failed, and the reason is in AdminCP ā Debug Panel ā Log explorer (/admin/core/debug).
Raise ogImage.version in the API config after changing a template, so crawlers and CDNs fetch the new cards instead of cached ones.
Related
- Social images: every
ogImageoption. - Route module: the
openGraphfield of a page'shead.