Skip to content

SVG on the server

The core of ireko and toSVG need no DOM. A layout and its SVG are pure functions of the input. Thus you can make diagrams on a server, in a build step, in a worker or in a test.

// diagram.ts: node diagram.ts > diagram.svg
import { layout } from "ireko";
import { toSVG } from "ireko/svg";
import { parse } from "ireko/text";
const parsed = parse(`
build "CI" nested grid(columns: 4) { checkout lint test deploy }
checkout -> lint
lint -> test
test -> deploy
`);
if (!parsed.ok) throw new Error(parsed.error.message);
process.stdout.write(toSVG(layout(parsed.input), { title: "The CI pipeline" }));
build "CI" nested grid(columns: 4) {
  checkout lint test deploy
}
checkout -> lint
lint -> test
test -> deploy
The output of the script, as this site draws it.CIcheckoutlinttestdeploy
The output of the script, as this site draws it.Open in the playground →

LayoutSvg from ireko/react has no state and no effects, so React renders it on the server. This site uses it for each figure: the HTML has the diagram before any script runs.

// An Astro page, a Next.js server component or renderToString
import { layout } from "ireko";
import { LayoutSvg } from "ireko/react";
export function Architecture({ input }) {
return <LayoutSvg layout={layout(input)} title="The architecture" className="diagram" />;
}

Without a size, each leaf gets the nodeSize of the layout (120 by 48). For labels of different lengths, give a measure function. It gets each node, and gives its size or undefined. fitText makes one: each box is as wide as its label, plus the padding.

import { fitText, layout } from "ireko";
const result = layout(input, {
measure: fitText({ fontSize: 13, padding: { x: 12, y: 8 }, minWidth: 60 }),
});

The default width of a text is an estimate from the widths of the characters of a common sans-serif font. It needs no font engine, so the same input gives the same layout on a server and in a browser.

In a browser, widthOfCanvas from ireko/render gives the exact width in the font of the page. The playground of this site uses it.

import { fitText } from "ireko";
import { widthOfCanvas } from "ireko/render";
const measure = fitText({ widthOf: widthOfCanvas("Inter, sans-serif") });

Measure after the web fonts load (document.fonts.ready), because a fallback font has other widths. A node with a size in its spec keeps that size. For a container, the measured size is the smallest size, so its label fits.

toSVG writes the colors as attributes, so the file looks correct alone. It also writes classes, so a page can change the colors with CSS:

toSVG(result, {
theme: { background: "transparent", fontFamily: "Inter, sans-serif", accent: "#d43c3c" },
padding: 24,
labels: true,
background: false,
});
OptionDefaultMeaning
themelightThemeThe colors and fonts. A partial theme replaces only its values.
padding16The space around the layout, in pixels.
titlenoneThe title of the image, for assistive technology. Give one.
labelstrueWith false, the output has no text.
backgroundtrueWith false, the output has no background rectangle.

The same input and options give the same SVG, byte for byte. The engine uses no random numbers and no clock, and toSVG writes each number with three decimals or fewer. Thus a snapshot test of a diagram is stable:

import { expect, it } from "vitest";
it("draws the architecture", async () => {
await expect(toSVG(layout(input))).toMatchFileSnapshot("./architecture.svg");
});

The tests of ireko do the same for each example of the gallery.

  • PNG: draw the SVG with a headless browser, or with an SVG library, for example resvg.
  • JSON: the layout is JSON. Store it, and draw it later in any renderer.
  • Canvas on a server: drawFrame from ireko/canvas draws on any 2D context with the methods of Context2D, for example the canvas of a Node.js library.