Skip to content

installDom

Stands up a headless DOM and mirrors its globals onto globalThis so d3plus can render. Returns a DomEnv whose teardown() restores the previous global state exactly — nothing is left mutated after a render.

Prefer withDom unless you need to manage the lifecycle yourself.

The lower-level half of server rendering: installs a headless window and document (jsdom by default, or a window you supply) on globalThis, plus the small set of browser APIs d3plus needs that those DOMs lack, and returns a teardown() that puts every global back. renderToStaticSVG and renderToStaticPNG do this for you; use it directly when other DOM-dependent code has to run in the same environment. Node only, so the snippet is not executed here.

import {installDom} from "@d3plus/ssr";
const env = await installDom();
// window, document, and the other browser globals d3plus touches now
// exist on globalThis, backed by jsdom.
try {
const list = env.document.createElement("ul");
list.innerHTML = "<li>built headlessly</li>";
console.log(list.outerHTML);
} finally {
env.teardown(); // restores every global it replaced, even after an error
}
// Bring your own DOM instead of jsdom (for example linkedom):
const custom = await installDom({window: myWindow});

Parameters

import {installDom} from "@d3plus/ssr";
optsDomEnvOptionsDefault {}