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 page | Yes | No |
| Reachable by your CSS | No | Yes |
animation and theme: "auto" on first paint | See below | Fully honour viewer settings |
Safe with untrusted colors | Yes | No |
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 Hugo García Benjumea · MIT licensed