mirror of
https://github.com/withastro/astro.git
synced 2024-12-23 21:53:55 -05:00
44a69f7dcb
If using Astro <= 0.17.0 there is no `metadata` being passed. Even though no one should be using that version with this, adding some extra protection just in case.
51 lines
1.3 KiB
JavaScript
51 lines
1.3 KiB
JavaScript
import { Component as BaseComponent, createElement as h } from 'react';
|
|
import { renderToStaticMarkup as reactRenderToStaticMarkup, renderToString } from 'react-dom/server.js';
|
|
import StaticHtml from './static-html.js';
|
|
|
|
const reactTypeof = Symbol.for('react.element');
|
|
|
|
function check(Component, props, children) {
|
|
if (typeof Component !== 'function') return false;
|
|
|
|
if (Component.prototype != null && typeof Component.prototype.render === 'function') {
|
|
return BaseComponent.isPrototypeOf(Component);
|
|
}
|
|
|
|
let error = null;
|
|
let isReactComponent = false;
|
|
function Tester(...args) {
|
|
try {
|
|
const vnode = Component(...args);
|
|
if (vnode && vnode['$$typeof'] === reactTypeof) {
|
|
isReactComponent = true;
|
|
}
|
|
} catch (err) {
|
|
error = err;
|
|
}
|
|
|
|
return h('div');
|
|
}
|
|
|
|
renderToStaticMarkup(Tester, props, children, {});
|
|
|
|
if (error) {
|
|
throw error;
|
|
}
|
|
return isReactComponent;
|
|
}
|
|
|
|
function renderToStaticMarkup(Component, props, children, metadata) {
|
|
const vnode = h(Component, { ...props, children: h(StaticHtml, { value: children }), innerHTML: children });
|
|
let html;
|
|
if(metadata && metadata.hydrate) {
|
|
html = renderToString(vnode);
|
|
} else {
|
|
html = reactRenderToStaticMarkup(vnode);
|
|
}
|
|
return { html };
|
|
}
|
|
|
|
export default {
|
|
check,
|
|
renderToStaticMarkup,
|
|
};
|