[]previous and next page

renderToString

Renders an app to an HTML string synchronously: the simplest server render, for pages whose data is already at hand.

Renders an app to an HTML string synchronously: the simplest server render, for pages whose data is already at hand. Suspense boundaries whose resources are still pending render their fallback (use renderToStringAsync or renderToStream to wait for data). Effects never run. The root is disposed before this returns, and an error from code propagates after that cleanup.

Import#

ts
import { renderToString } from "@shadow-js/web/server";

Signature#

ts
function renderToString(code: () => unknown, options?: RenderOptions): string

Parameters#

Name Description
code Builds the app: returns a component result, an SsrNode, text or an array.
options Hydration-key prefix, head and portal collectors, and error reporters. Undefined uses the defaults of RenderOptions.

Returns#

The escaped markup of the app.

Throws#

  • TypeError for an invalid renderId, for portals not created by createServerPortals, or for a value that cannot be rendered (see escape). An error no boundary caught propagates unless onUncaughtError is set.

Examples#

ts
import { createComponent, renderToString } from "@shadow-js/web/server";

function App() {
  return <h1>Hello</h1>;
}
const html = renderToString(() => createComponent(App, {}));