Function: renderToStream()

Estimated reading time: less than 1 minute
function renderToStream(html, rid?): Readable

Transforms a component tree who may contain Suspense components into a stream of HTML.

Parameters

html

The component tree to render or a function that returns the component tree.

Element | (rid) => Element

rid?

The request id to identify the request, if not provided, a new incrementing id will be used.

string | number

Returns

Readable

Example

import { text } from 'node:stream/consumers'

// Prints out the rendered stream (2nd example shows with a custom id)
const stream = renderToStream((r) => <AppWithSuspense rid={r} />)
const stream = renderToStream(<AppWithSuspense rid={myCustomId} />, myCustomId)

// You can consume it as a stream
for await (const html of stream) {
  console.log(html.toString())
}

// Or join it all together (Wastes ALL Suspense benefits, but useful for testing)
console.log(await text(stream))

See

Suspense