ssr method

void ssr(
  1. String path,
  2. BloomNode builder(
    1. BloomRequest request
    ), {
  3. HeadManager head(
    1. BloomRequest request
    )?,
  4. String layout(
    1. String bodyHtml,
    2. HeadManager? head
    )?,
  5. List<BloomMiddleware> middlewares = const [],
})

Mounts a high-performance Server-Side Rendered (SSR) endpoint using BloomNode.

Executes pure-Dart builder in <1ms without JavaScript engine overhead. Automatically wraps the resulting HTML using layout, head, or a default HTML5 template.

Each request runs in its own isolated BloomQueryScope (issue #31), disposed automatically on success or error.

Example

router.ssr('/dashboard', (req) => Div(
  classes: 'p-6 max-w-4xl mx-auto',
  children: [H1(text: 'Welcome back')],
));

Implementation

void ssr(
  String path,
  BloomNode Function(BloomRequest request) builder, {
  HeadManager Function(BloomRequest request)? head,
  String Function(String bodyHtml, HeadManager? head)? layout,
  List<BloomMiddleware> middlewares = const [],
}) {
  get(path, (request) async {
    return BloomData.withRequestScope((scope) {
      final node = BloomData.runWithScope(scope, () => builder(request));
      final bodyHtml = renderToHtmlInScope(node, scope);
      final headManager =
          head == null ? null : BloomData.runWithScope(scope, () => head(request));

      final String fullHtml;
      if (layout != null) {
        fullHtml = layout(bodyHtml, headManager);
      } else if (headManager != null) {
        fullHtml = headManager.wrapDocument(bodyHtml);
      } else {
        fullHtml =
            '<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"></head><body>$bodyHtml</body></html>';
      }

      return BloomResponse.html(fullHtml);
    }, debugLabel: 'ssr-request');
  }, middlewares: middlewares);
}