///
///
import type { ComponentType } from "react";
import { hydrateRoot } from "react-dom/client";
function readProps
(host: HTMLElement): P | null {
const script = host.querySelector("script[data-island-props]");
if (!script?.textContent) {
return null;
}
try {
return JSON.parse(script.textContent) as P;
} catch {
return null;
}
}
export function hydrateIsland(name: string, Component: ComponentType
) {
const hosts = document.querySelectorAll(`[data-island="${name}"]`);
for (const host of Array.from(hosts)) {
if (host.dataset.islandHydrated === "true") {
continue;
}
const root = host.querySelector("[data-island-root]");
const props = readProps(host);
if (!root || !props) {
continue;
}
hydrateRoot(root, , {
onRecoverableError(error, errorInfo) {
console.error(`Recoverable hydration error in island "${name}"`, {
error,
componentStack: errorInfo.componentStack,
});
},
});
host.dataset.islandHydrated = "true";
}
}