summaryrefslogtreecommitdiff
path: root/packages/integrations/solid/server.js
blob: 636fa50f6bc6c8ae0f6284b29af78f0326487bb0 (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
import { renderToString, ssr, createComponent } from 'solid-js/web';

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

function renderToStaticMarkup(Component, props, children) {
	const html = renderToString(() =>
		createComponent(Component, {
			...props,
			// In Solid SSR mode, `ssr` creates the expected structure for `children`.
			// In Solid client mode, `ssr` is just a stub.
			children: children != null ? ssr(`<astro-fragment>${children}</astro-fragment>`) : children,
		})
	);
	return {
		html: html,
	};
}

export default {
	check,
	renderToStaticMarkup,
};