fix: Missing error on invalid island children (#1736)

This commit is contained in:
Marvin Hagemeister
2023-09-05 15:15:48 +02:00
committed by GitHub
parent c00ae494f6
commit 7004eae7ba
7 changed files with 96 additions and 14 deletions
+15
View File
@@ -3,6 +3,7 @@ import {
type ComponentChildren,
Fragment,
h,
isValidElement,
type Options as PreactOptions,
options as preactOptions,
type VNode,
@@ -233,6 +234,20 @@ options.__b = (vnode: VNode<Record<string, unknown>>) => {
const id = islandProps.length;
if ("children" in props) {
let children = props.children;
// Guard against passing objects as children to JSX
if (
children !== null && !Array.isArray(children) &&
!isValidElement(children)
) {
const name = originalType.displayName || originalType.name ||
"Anonymous";
throw new Error(
`Invalid JSX child passed to island <${name} />. Only JSX elements can be passed as children to an island. To resolve this error, pass the data as a standard prop instead.`,
);
}
const markerText =
`frsh-slot-${island.id}:${island.exportName}:${id}:children`;
// @ts-ignore nonono