summaryrefslogtreecommitdiff
path: root/packages/renderers/renderer-preact/server.js
blob: 989e2d385ee0a674eb87082b0bd0c514cf73d8ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { h, Component as BaseComponent } from 'preact';
import { renderToString } from 'preact-render-to-string';
import StaticHtml from './static-html.js';

function check(Component, props, children) {
  if(typeof Component !== 'function') return false;

  if(typeof Component.prototype.render === 'function') {
    return BaseComponent.isPrototypeOf(Component);
  }

  const { html } = renderToStaticMarkup(Component, props, children);
  return Boolean(html);
}

function renderToStaticMarkup(Component, props, children) {
  const html = renderToString(h(Component, { ...props, children: h(StaticHtml, { value: children }), innerHTML: children }));
  return { html };
}

export default {
  check,
  renderToStaticMarkup,
};