0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00
astro/packages/integrations/solid/client.js

38 lines
930 B
JavaScript
Raw Normal View History

import { sharedConfig } from 'solid-js';
import { hydrate, render, createComponent } from 'solid-js/web';
2022-05-31 11:47:13 -05:00
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;
2022-05-31 11:47:13 -05:00
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');
}
2022-05-31 11:47:13 -05:00
if (children == null) {
children = document.createElement('astro-fragment');
children.innerHTML = childHTML;
}
}
2022-05-31 11:47:13 -05:00
return children;
},
}),
element
);
};