buildPrerenderBootstrapHtml method

String buildPrerenderBootstrapHtml({
  1. required String templateHtml,
  2. required WebMetadata metadata,
  3. required String semanticHtml,
  4. required BuildMode mode,
  5. RenderingStrategy renderingStrategy = RenderingStrategy.flutterCanvas,
  6. String baseHref = '/',
  7. MetadataRenderer metadataRenderer = const MetadataRenderer(),
  8. MetaMantleConfig config = const MetaMantleConfig(),
})

Synthesizes complete prerender HTML by injecting head metadata, prerender CSS, semantic content container, and client coordination scripts into templateHtml.

Implementation

String buildPrerenderBootstrapHtml({
  required String templateHtml,
  required WebMetadata metadata,
  required String semanticHtml,
  required BuildMode mode,
  RenderingStrategy renderingStrategy = RenderingStrategy.flutterCanvas,
  String baseHref = '/',
  MetadataRenderer metadataRenderer = const MetadataRenderer(),
  MetaMantleConfig config = const MetaMantleConfig(),
}) {
  final tags = metadataRenderer.render(metadata, config: config);
  final document = html_parser.parse(templateHtml);
  final head = document.querySelector('head');
  final body = document.querySelector('body');
  final htmlEl = document.querySelector('html');

  if (htmlEl != null) {
    htmlEl.attributes['data-mantle-strategy'] = renderingStrategy.id;
    if (renderingStrategy == RenderingStrategy.semanticDom) {
      htmlEl.attributes['data-mantle-runtime'] = 'ready';
    }
  }

  if (head != null) {
    for (final tag in tags) {
      if (tag.tagName == 'title') {
        final existing = head.querySelector('title');
        if (existing != null) {
          existing.text = tag.textContent ?? '';
        } else {
          final t = document.createElement('title')
            ..text = tag.textContent ?? '';
          head.append(t);
        }
        continue;
      }

      final el = document.createElement(tag.tagName);
      for (final attr in tag.attributes.entries) {
        el.attributes[attr.key] = attr.value;
      }
      if (tag.textContent != null) {
        el.text = tag.textContent!;
      }
      head.append(el);
    }

    if (mode != BuildMode.metadata &&
        renderingStrategy != RenderingStrategy.semanticDom) {
      final existingStyle = head.querySelector('#mantle-prerender-style');
      if (existingStyle != null) {
        existingStyle.remove();
      }
      final styleEl = html_parser.parseFragment(generatePrerenderStyles());
      head.append(styleEl);
    }
  }

  if (body != null && mode != BuildMode.metadata && semanticHtml.isNotEmpty) {
    final existingRoot = body.querySelector('#mantle-prerender-root');
    if (existingRoot != null) {
      existingRoot.remove();
    }
    final existingApp = body.querySelector('#app');
    if (existingApp != null) {
      existingApp.remove();
    }
    final existingBridge = body.querySelector('#mantle-startup-bridge');
    if (existingBridge != null) {
      existingBridge.remove();
    }
    final existingIsland = body.querySelector('#mantle-island-initializer');
    if (existingIsland != null) {
      existingIsland.remove();
    }
    final existingDomClient = body.querySelector('#mantle-dom-client');
    if (existingDomClient != null) {
      existingDomClient.remove();
    }

    final wrapped = wrapPrerenderContent(
      semanticHtml,
      mode: mode,
      renderingStrategy: renderingStrategy,
    );
    final contentFragment = html_parser.parseFragment(wrapped);
    if (contentFragment.children.isNotEmpty) {
      body.children.insert(0, contentFragment.children.first);
    }

    if (renderingStrategy == RenderingStrategy.semanticDom) {
      // Strip full-page Flutter bootstrap script so Flutter Canvas never takes over the document
      final scriptsToRemove =
          [
                ...body.querySelectorAll('script'),
                if (head != null) ...head.querySelectorAll('script'),
              ]
              .where(
                (s) =>
                    s.attributes['src']?.contains('flutter_bootstrap.js') ??
                    false,
              )
              .toList();
      for (final s in scriptsToRemove) {
        s.remove();
      }

      final domClientFragment = html_parser.parseFragment(
        generateSemanticDomClientScript(),
      );
      body.append(domClientFragment);
    } else if (renderingStrategy == RenderingStrategy.domWithFlutterIslands) {
      final islandInitFragment = html_parser.parseFragment(
        generateIslandBootstrapScript(),
      );
      body.children.insert(0, islandInitFragment.children.first);
    } else if (mode == BuildMode.hybrid) {
      final bridgeFragment = html_parser.parseFragment(
        generateClientHandshakeScript(),
      );
      body.append(bridgeFragment);
    }
  }

  return document.outerHtml;
}