renderToStream function
Synchronously renders node to an HTML string and streams the output in fixed 4KB chunks.
Allows HTTP servers to begin flushing initial bytes to the network early. The concatenation
of all emitted chunks is identical to calling renderToHtml directly on node.
For asynchronous streaming with out-of-order Suspense resolution, use renderToStreamWithSuspense.
await for (final chunk in renderToStream(App())) {
httpResponse.write(chunk);
}
Implementation
Stream<String> renderToStream(BloomNode node) {
// Rendered eagerly so keyframe deduplication is scoped to this call's own
// zone even when multiple streams are listened to concurrently; only the
// 4KB chunking below is lazy.
final html = renderToHtml(node);
const chunkSize = 4096;
Iterable<String> chunks() sync* {
for (var i = 0; i < html.length; i += chunkSize) {
yield html.substring(
i, i + chunkSize > html.length ? html.length : i + chunkSize);
}
}
return Stream.fromIterable(chunks());
}