summaryrefslogtreecommitdiff
path: root/packages/integrations/solid/client.js
blob: 867d951c6f16c1a56625026f6e93d25c67b8435d (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
import { sharedConfig } from 'solid-js';
import { hydrate, render, createComponent } from 'solid-js/web';

export default (element) =>
	(Component, props, childHTML, { client }) => {
		// Prepare global object expected by Solid's hydration logic
		if (!window._$HY) {
			window._$HY = { events: [], completed: new WeakSet(), r: {} };
		}
		if (!element.hasAttribute('ssr')) return;

		const fn = client === 'only' ? render : hydrate;

		// Perform actual hydration
		let children;
		fn(
			() =>
				createComponent(Component, {
					...props,
					get children() {
						if (childHTML != null) {
							// hydrating
							if (sharedConfig.context) {
								children = element.querySelector('astro-fragment');
							}

							if (children == null) {
								children = document.createElement('astro-fragment');
								children.innerHTML = childHTML;
							}
						}
						return children;
					},
				}),
			element
		);
	};