Skip to content

@ogygia/image

Social cards

A share card is a Svelte component drawn as a PNG, one per page, for og:image, with no route to write and no list of pages to keep.

On this page

When someone shares a link, chat apps and social sites show the page’s og:image. Most sites want a card there: the page’s title on the site’s own design. Usually that means a route that draws PNGs and a list of every page to keep in sync.

With @ogygia/image, a share card is a Svelte component. import.meta.og.image.card() turns it into a function of its props that returns a descriptor, like any image. A prerendered page’s card is drawn by the build, and a page rendered on request has its card drawn by the image endpoint the first time a crawler asks.

export const ShareCard = import.meta.og.image.card<{
  title: string;
  section?: string;
}>('./ShareCard.svelte', {
  width: 1200,
  height: 630,
  fonts: ['./fonts/source-serif.woff']
});

Install

Cards are drawn by three packages your app installs, so an app with no card loads none of them: satori-html reads the component’s HTML and CSS, satori lays it out and draws it as SVG, and resvg (@resvg/resvg-js) turns that into a PNG. They load on the first card drawn.

A build with a card and any of them missing fails with image_card_engine_missing. A server that draws cards on request needs them in dependencies, not devDependencies: resvg is a native module, and bundled it can’t find its binary. The build checks that too. A fully prerendered site, or one on an edge runtime, draws cards only at build, so devDependencies is enough there.

pnpm add satori satori-html @resvg/resvg-js

Card definition

Name the card once, in a module. import.meta.og.image.card(component, options) takes the component’s path, relative to the file like an import, and three options, all required:

  • width and height: the PNG’s size in pixels, whole numbers from 1 to 99999.
  • fonts: the faces it draws text with, at least one.

Like the image macro, its arguments must be written out in place. The type parameter is the props type, so a call with the wrong props doesn’t compile. A path that isn’t a .svelte file, or a bad width or height, fails the build with image_option_invalid; a missing component with image_not_found.

Fonts are files

satori reads no system font, so a card draws text only with the fonts it lists. Each entry is a path relative to the file, or a package’s file (@fontsource/…/….woff). Each must be a .woff, .ttf or .otf file: satori reads no WOFF2.

The build reads each file’s family, weight and style from the file itself. Your CSS names the family and the weight: the name stored inside the file, not the file’s name. source-serif.woff above holds the family Source Serif 4, so the CSS says font-family: 'Source Serif 4'. A font that’s missing, in another format, or doesn’t read fails with image_card_font, and so does an empty list.

export const ShareCard = import.meta.og.image.card<{
  title: string;
  section?: string;
}>('./ShareCard.svelte', {
  width: 1200,
  height: 630,
  fonts: [
    './fonts/source-serif.woff',
    '@fontsource/jetbrains-mono/files/jetbrains-mono-latin-500-normal.woff'
  ]
});

Card component

The component renders with the card’s props, on the server, like a page. Its scoped CSS goes with it, and that of every component it imports, so write the card as you would any component. Size its root to the card’s width and height.

The CSS is satori’s, a subset:

  • What works: flexbox, sizes, margins and padding, borders and radii, colors and gradients, position: absolute, transforms, opacity, fonts and text.
  • What doesn’t: grid, floats and z-index, and calc() and CSS variables only work in some places.
  • An element with more than one child needs display: flex.

What satori can’t draw fails the build, or the request, with image_card_css, naming the property and the card.

Show an image in a card

A card can show images, but its engine never fetches them itself: satori would fetch any address the markup names, private ones included. ogygia reads every address the markup names (an <img src>, an SVG <image href>, a style’s url(), any element’s src, an SVG element’s href) and hands the bytes over. Each one may be:

  • an image of the build: the src of an import.meta.og.image() descriptor, or an asset the server imports. Not a static/ file, which the live endpoint can’t read.
  • an http(s) URL on a host image({ remote: { hosts } }) lists, fetched under its maxBytes, timeout and private-address rule, like remoteImage().
  • a data: URL.

Its bytes must be PNG, JPEG, GIF, WebP, AVIF or TIFF. Anything else fails with image_card_source: a relative path, a static/ file, an SVG data: URL, <Img>’s signed file URLs, a URL with no remote set, a url( with no closing paren. A #… reference inside the card’s own SVG isn’t an address, and a link’s href names no image, so both are left alone.

Each image is fitted to the card with sharp before the draw. One past 268,402,689 pixels (16383 × 16383) is refused from its header. One larger than the card needs is drawn from a copy at the card’s size, so a small file of huge dimensions can’t take the draw’s memory. A WebP, AVIF or TIFF, a CMYK or 12-bit JPEG, a 16-bit PNG and a file with a color profile are always drawn from an sRGB PNG or JPEG copy, since resvg draws the first ones as nothing and reads no color profile. A server with no sharp encoder (vercel() alone) refuses a card’s image.

<script lang="ts">
  let {
    title,
    section = ''
  }: { title: string; section?: string } = $props();
</script>

<div class="card">
  {#if section}<span class="chip">{section}</span>{/if}
  <div class="title">{title}</div>
</div>

<style>
  .card {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    width: 1200px;
    height: 630px;
    padding: 64px;
    background: #102a43;
    font-family: 'Source Serif 4';
  }
  .chip {
    display: flex;
    font-family: 'JetBrains Mono';
    font-size: 24px;
    color: #f0b429;
  }
  .title {
    display: flex;
    font-size: 72px;
    color: #f0f4f8;
  }
</style>
<script lang="ts">
  let { title }: { title: string } = $props();

  const avatar = import.meta.og.image('./images/avatar.jpg');
</script>

<div class="card">
  <img class="avatar" src={avatar.src} alt="" />
  <div class="title">{title}</div>
</div>

<style>
  .card {
    display: flex;
    align-items: center;
    gap: 48px;
    width: 1200px;
    height: 630px;
    padding: 64px;
    background: #102a43;
    font-family: 'Source Serif 4';
  }
  .avatar {
    width: 200px;
    height: 200px;
    border-radius: 100px;
  }
  .title {
    display: flex;
    font-size: 64px;
    color: #f0f4f8;
  }
</style>

Page cards

ShareCard(props) returns the card’s descriptor. Call it on the server: in a load, or in the render of a csr = false page’s component. The descriptor’s URL is minted by the server, so a card maker called in the browser fails with image_no_server. On a page Kit hydrates (csr = true), make the card in the page’s server load and pass the descriptor down.

The props must be JSON and small: one plain object of strings, finite numbers, booleans, null, arrays and plain objects, at most 2048 characters once written in a URL. Anything else fails with image_card_props, a key set to undefined included: leave an optional prop out instead. The props ride in each file’s URL, signed with the rest, so a server draws a card on request from its URL alone, and nobody can ask it for props the page didn’t name. Pass the text the card shows, not the record it came from, and never a query string or a visitor’s free text: each distinct set of props is another PNG to draw and keep.

Put it in the head

The descriptor’s src is the card’s own PNG, at the card’s size. Make it absolute for og:image (Make a URL absolute). It’s an image like any other, so <Img> and getImage() take it too, but those ask for resized AVIF and WebP files of it. On a live page that needs an encoder that can resize it: sharp(), or [vercel(), sharp()]. With vercel() alone or on Workers, use the card’s src as it is, or prerender the page (#2290).

This site draws a card for every docs page the same way. Below is the one for this page, made while the page rendered with the same props its head passes, so it’s the PNG its og:image names.

import { error } from '@sveltejs/kit';
import { ShareCard } from '$lib/share';
import { blog } from '$lib/blog.server';

export async function load({ params }) {
  const post = await blog.get(params.slug);
  if (!post) error(404, 'Not found');
  return {
    title: post.data.title,
     share: ShareCard({
      title: post.data.title,
      section: 'Blog'
    }) 
  };
}
<script lang="ts">
  import { page } from '$app/state';
  import { Img } from '@ogygia/image';

  let { data } = $props();
</script>

<svelte:head>
  <meta
    property="og:image"
    content={new URL(data.share.src, page.url).href}
  />
  <meta property="og:image:width" content="1200" />
  <meta property="og:image:height" content="630" />
</svelte:head>

<Img of={data.share} preset="thumb" alt="" />

Output files

A card is a function of its props and the images it shows: the same props and the same image bytes draw the same PNG. Its id holds a digest of every module and font the card is made from and the engine’s versions, so changing a child component, its CSS or a font names new files.

PageMade by
prerenderedthe build, under _app/immutable/og/ (c…-1200x630.png)
rendered on request, Node or Vercelthe image endpoint, on the first ask, kept forever (or remote.maxAge, see below)
rendered on request, Workers or edgethe build’s file, when a prerendered page named the same card and props
devthe dev server, again after each edit

A card that shows a remote image is a remote file: the host can change the picture behind a URL. A server that draws it keeps it, and sends it to be kept, until the end of the current remote.maxAge window, never immutable, and draws it again after. The build names its file by the bytes it got, which it asks the host for again on every build. A card that shows only the build’s images is kept forever. A remote host that refuses a card’s image answers the card’s file with the same status as the image itself (404, 413, 502, 504, as listed under When the host fails), and a full fetch line 503.

On Workers or another edge runtime, satori and resvg can’t run. A page rendered on request names its card’s file as always, and the Worker serves it from the build’s static files when a prerendered page made the same card with the same props and it shows no remote image (the build names one that does by the bytes it got, which the Worker can’t know). Otherwise it answers 404 and says image_no_encoder. A build for such an adapter with a card says so once. Prerender the pages that show cards, or have a prerendered page name the cards your live pages show. The same goes for an <Img> of a card: its resized files are served only when a prerendered page laid out the same card the same way, and otherwise answer 404.

A Vercel function runs Node, so it draws cards like any Node server. The build names satori’s text shaper (a WebAssembly file it reads by a path Vercel’s file tracing can’t see) in the card’s server chunk, so the function bundle keeps it.