summaryrefslogtreecommitdiff
path: root/packages/integrations/solid/src/server.ts
blob: 445e4605e9decef2054ea760218e4d046a4f2003 (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
47
48
49
50
51
52
53
54
55
56
57
58
import { createComponent, renderToString, ssr } from 'solid-js/web';
import { getContext, incrementId } from './context.js';
import type { RendererContext } from './types.js';

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

function check(this: RendererContext, Component: any, props: Record<string, any>, children: any) {
	if (typeof Component !== 'function') return false;
	if (Component.name === 'QwikComponent') return false;
	const { html } = renderToStaticMarkup.call(this, Component, props, children);
	return typeof html === 'string';
}

function renderToStaticMarkup(
	this: RendererContext,
	Component: any,
	props: Record<string, any>,
	{ default: children, ...slotted }: any,
	metadata?: undefined | Record<string, any>
) {
	const renderId = metadata?.hydrate ? incrementId(getContext(this.result)) : '';
	const needsHydrate = metadata?.astroStaticSlot ? !!metadata.hydrate : true;
	const tagName = needsHydrate ? 'astro-slot' : 'astro-static-slot';

	const html = renderToString(
		() => {
			const slots: Record<string, any> = {};
			for (const [key, value] of Object.entries(slotted)) {
				const name = slotName(key);
				slots[name] = ssr(`<${tagName} name="${name}">${value}</${tagName}>`);
			}
			// Note: create newProps to avoid mutating `props` before they are serialized
			const newProps = {
				...props,
				...slots,
				// In Solid SSR mode, `ssr` creates the expected structure for `children`.
				children: children != null ? ssr(`<${tagName}>${children}</${tagName}>`) : children,
			};

			return createComponent(Component, newProps);
		},
		{
			renderId,
		}
	);
	return {
		attrs: {
			'data-solid-render-id': renderId,
		},
		html,
	};
}

export default {
	check,
	renderToStaticMarkup,
	supportsAstroStaticSlot: true,
};