Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/andresilva-cc/grafex/llms.txt

Use this file to discover all available pages before exploring further.

close() terminates the headless browser process that Grafex maintains across render calls. Because the browser is kept alive to amortize launch cost, it holds the Node.js event loop open — your script will not exit on its own until close() is called. Calling it is mandatory at the end of any script that uses render() or renderAll(), and should always be placed in a finally block so that it runs even when an error is thrown during rendering.

Signature

async function close(): Promise<void>

Behavior

Frees the browser process. Grafex uses Playwright internally to launch and manage a headless WebKit (or Chromium) browser. close() calls the underlying BrowserManager.close() method, which closes the Playwright browser context and terminates the OS-level browser process. Clears the shared instance. After close() completes, the internal BrowserManager reference is set to null. The next call to render() or renderAll() detects this and automatically launches a fresh browser instance — there is no manual re-initialization step. Safe to call multiple times. If currentManager is already null (because close() was already called), the function returns immediately without doing anything. This makes it safe to call unconditionally in finally blocks or cleanup handlers.
If you do not call close(), the browser process will keep the Node.js event loop open indefinitely. Your script will appear to hang after all rendering is complete. Always use try/finally or an equivalent pattern to guarantee close() is reached.

Examples

import { render, close } from 'grafex';
import { writeFile } from 'node:fs/promises';

const compositions = ['./hero.tsx', './card.tsx', './thumbnail.tsx'];

try {
  for (const file of compositions) {
    const result = await render(file, { scale: 2 });
    const out = file.replace('.tsx', '@2x.png');
    await writeFile(out, result.buffer);
    console.log(`Rendered ${out}${result.width}×${result.height}`);
  }
} finally {
  // Runs whether rendering succeeds or throws mid-batch
  await close();
}
After close() returns, the browser is fully stopped. If you call render() again, Grafex automatically creates a new browser instance — there is no need to call an “open” function explicitly.
In long-running processes or servers that generate images on demand, you may choose to keep the browser alive across requests rather than closing after every render. In that case, call close() only on process shutdown — for example in a SIGTERM handler — rather than after each individual render.

Build docs developers (and LLMs) love