summaryrefslogtreecommitdiff
path: root/packages/integrations/preact/server.js
blob: 29ae8a3b0cd9d4c41f9ea9539822c732206d0b65 (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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { h, Component as BaseComponent } from 'preact';
import render from 'preact-render-to-string';
import StaticHtml from './static-html.js';

const slotName = (str) => str.trim().replace(/[-_]([a-z])/g, (_, w) => w.toUpperCase());

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

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

	try {
		const { html } = renderToStaticMarkup(Component, props, children);
		if (typeof html !== 'string') {
			return false;
		}

		// There are edge cases (SolidJS) where Preact *might* render a string,
		// but components would be <undefined></undefined>

		return !/\<undefined\>/.test(html);
	} catch (err) {
		return false;
	}
}

function renderToStaticMarkup(Component, props, { default: children, ...slotted }) {
	const slots = {};
	for (const [key, value] of Object.entries(slotted)) {
		const name = slotName(key);
		slots[name] = h(StaticHtml, { value, name });
	}
	// Note: create newProps to avoid mutating `props` before they are serialized
	const newProps = { ...props, ...slots };
	const html = render(
		h(Component, newProps, children != null ? h(StaticHtml, { value: children }) : children)
	);
	return { html };
}

export default {
	check,
	renderToStaticMarkup,
};