Skip to content

Server-Side Rendering

renderToString turns an avatar into HTML anywhere JavaScript runs. Its output depends only on its inputs, so server and client markup match.

import { renderToString } from "@avatar-generator/core";
renderToString(faces, { seed: user.id, size: 48 }, { alt: user.name, loading: "lazy" });
// <img src="data:image/svg+xml,..." alt="Ana Li" width="48" height="48" loading="lazy">
renderToString(faces, { seed: user.id, size: 48 }, { mode: "inline", alt: user.name });
// <svg role="img" aria-label="Ana Li" ...>...</svg>

Which mode

img (default)inline
Sandboxed from your pageYesNo
Reachable by your CSSNoYes
animation and theme: "auto" on first paintSee belowFully honour viewer settings
Safe with untrusted colorsYesNo

img mode URL-encodes the SVG, which is smaller than the base64 toDataUri() produces. Inline mode writes no data URI at all, and gives each avatar ids derived from its content, so several on a page cannot collide and renders stay stable.

A server cannot know the viewer’s settings, and <img> may not see them. For animation or theme: "auto" in server-rendered HTML, use inline. The framework wrappers render <img>, then apply the viewer’s settings once in the browser.

Importing on a server

Core and every style use no browser globals, and the web component package imports safely. Only the deprecated createAvatarElement needs a DOM.

Built by · MIT licensed