astro/packages/renderers/renderer-preact/server.js
Matthew Phillips af03c90c2b
Fix usage of arrow functions as components (#426)
* Fix usage of arrow functions as components

This fixes the React and Preact component as arrow function use-case by checking for the prototype property (arrow functions don't)

* Check the prototype
2021-06-14 13:24:37 -04:00

24 lines
759 B
JavaScript

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 (Component.prototype != null && 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,
};