fix: Missing error on invalid island children (#1736)
This commit is contained in:
@@ -3,6 +3,7 @@ import {
|
|||||||
type ComponentChildren,
|
type ComponentChildren,
|
||||||
Fragment,
|
Fragment,
|
||||||
h,
|
h,
|
||||||
|
isValidElement,
|
||||||
type Options as PreactOptions,
|
type Options as PreactOptions,
|
||||||
options as preactOptions,
|
options as preactOptions,
|
||||||
type VNode,
|
type VNode,
|
||||||
@@ -233,6 +234,20 @@ options.__b = (vnode: VNode<Record<string, unknown>>) => {
|
|||||||
const id = islandProps.length;
|
const id = islandProps.length;
|
||||||
if ("children" in props) {
|
if ("children" in props) {
|
||||||
let children = props.children;
|
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 =
|
const markerText =
|
||||||
`frsh-slot-${island.id}:${island.exportName}:${id}:children`;
|
`frsh-slot-${island.id}:${island.exportName}:${id}:children`;
|
||||||
// @ts-ignore nonono
|
// @ts-ignore nonono
|
||||||
|
|||||||
@@ -9,13 +9,15 @@ import * as $3 from "./routes/island_conditional_lazy_island.tsx";
|
|||||||
import * as $4 from "./routes/island_fn_child.tsx";
|
import * as $4 from "./routes/island_fn_child.tsx";
|
||||||
import * as $5 from "./routes/island_in_island.tsx";
|
import * as $5 from "./routes/island_in_island.tsx";
|
||||||
import * as $6 from "./routes/island_in_island_definition.tsx";
|
import * as $6 from "./routes/island_in_island_definition.tsx";
|
||||||
import * as $7 from "./routes/island_jsx_child.tsx";
|
import * as $7 from "./routes/island_invalid_children.tsx";
|
||||||
import * as $8 from "./routes/island_jsx_children.tsx";
|
import * as $8 from "./routes/island_invalid_children_fn.tsx";
|
||||||
import * as $9 from "./routes/island_jsx_island_jsx.tsx";
|
import * as $9 from "./routes/island_jsx_child.tsx";
|
||||||
import * as $10 from "./routes/island_jsx_text.tsx";
|
import * as $10 from "./routes/island_jsx_children.tsx";
|
||||||
import * as $11 from "./routes/island_nested_props.tsx";
|
import * as $11 from "./routes/island_jsx_island_jsx.tsx";
|
||||||
import * as $12 from "./routes/island_order.tsx";
|
import * as $12 from "./routes/island_jsx_text.tsx";
|
||||||
import * as $13 from "./routes/island_siblings.tsx";
|
import * as $13 from "./routes/island_nested_props.tsx";
|
||||||
|
import * as $14 from "./routes/island_order.tsx";
|
||||||
|
import * as $15 from "./routes/island_siblings.tsx";
|
||||||
import * as $$0 from "./islands/BooleanButton.tsx";
|
import * as $$0 from "./islands/BooleanButton.tsx";
|
||||||
import * as $$1 from "./islands/Counter.tsx";
|
import * as $$1 from "./islands/Counter.tsx";
|
||||||
import * as $$2 from "./islands/FragmentIsland.tsx";
|
import * as $$2 from "./islands/FragmentIsland.tsx";
|
||||||
@@ -25,6 +27,7 @@ import * as $$5 from "./islands/IslandConditional.tsx";
|
|||||||
import * as $$6 from "./islands/IslandFn.tsx";
|
import * as $$6 from "./islands/IslandFn.tsx";
|
||||||
import * as $$7 from "./islands/IslandInsideIsland.tsx";
|
import * as $$7 from "./islands/IslandInsideIsland.tsx";
|
||||||
import * as $$8 from "./islands/IslandWithProps.tsx";
|
import * as $$8 from "./islands/IslandWithProps.tsx";
|
||||||
|
import * as $$9 from "./islands/PassThrough.tsx";
|
||||||
|
|
||||||
const manifest = {
|
const manifest = {
|
||||||
routes: {
|
routes: {
|
||||||
@@ -35,13 +38,15 @@ const manifest = {
|
|||||||
"./routes/island_fn_child.tsx": $4,
|
"./routes/island_fn_child.tsx": $4,
|
||||||
"./routes/island_in_island.tsx": $5,
|
"./routes/island_in_island.tsx": $5,
|
||||||
"./routes/island_in_island_definition.tsx": $6,
|
"./routes/island_in_island_definition.tsx": $6,
|
||||||
"./routes/island_jsx_child.tsx": $7,
|
"./routes/island_invalid_children.tsx": $7,
|
||||||
"./routes/island_jsx_children.tsx": $8,
|
"./routes/island_invalid_children_fn.tsx": $8,
|
||||||
"./routes/island_jsx_island_jsx.tsx": $9,
|
"./routes/island_jsx_child.tsx": $9,
|
||||||
"./routes/island_jsx_text.tsx": $10,
|
"./routes/island_jsx_children.tsx": $10,
|
||||||
"./routes/island_nested_props.tsx": $11,
|
"./routes/island_jsx_island_jsx.tsx": $11,
|
||||||
"./routes/island_order.tsx": $12,
|
"./routes/island_jsx_text.tsx": $12,
|
||||||
"./routes/island_siblings.tsx": $13,
|
"./routes/island_nested_props.tsx": $13,
|
||||||
|
"./routes/island_order.tsx": $14,
|
||||||
|
"./routes/island_siblings.tsx": $15,
|
||||||
},
|
},
|
||||||
islands: {
|
islands: {
|
||||||
"./islands/BooleanButton.tsx": $$0,
|
"./islands/BooleanButton.tsx": $$0,
|
||||||
@@ -53,6 +58,7 @@ const manifest = {
|
|||||||
"./islands/IslandFn.tsx": $$6,
|
"./islands/IslandFn.tsx": $$6,
|
||||||
"./islands/IslandInsideIsland.tsx": $$7,
|
"./islands/IslandInsideIsland.tsx": $$7,
|
||||||
"./islands/IslandWithProps.tsx": $$8,
|
"./islands/IslandWithProps.tsx": $$8,
|
||||||
|
"./islands/PassThrough.tsx": $$9,
|
||||||
},
|
},
|
||||||
baseUrl: import.meta.url,
|
baseUrl: import.meta.url,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
import { ComponentChildren } from "preact";
|
||||||
|
|
||||||
|
export function PassThrough(props: { children: ComponentChildren }) {
|
||||||
|
return <div>{props.children}</div>;
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
import { PassThrough } from "../islands/PassThrough.tsx";
|
||||||
|
|
||||||
|
export default function Page() {
|
||||||
|
return (
|
||||||
|
<div id="page">
|
||||||
|
<PassThrough>{{ foo: 123 }}</PassThrough>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
import { PassThrough } from "../islands/PassThrough.tsx";
|
||||||
|
|
||||||
|
export default function Page() {
|
||||||
|
return (
|
||||||
|
<div id="page">
|
||||||
|
<PassThrough>{() => {}}</PassThrough>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -6,8 +6,12 @@ import {
|
|||||||
Page,
|
Page,
|
||||||
} from "./deps.ts";
|
} from "./deps.ts";
|
||||||
import {
|
import {
|
||||||
|
assertSelector,
|
||||||
|
assertTextMatch,
|
||||||
clickWhenListenerReady,
|
clickWhenListenerReady,
|
||||||
|
fetchHtml,
|
||||||
waitForText,
|
waitForText,
|
||||||
|
withFresh,
|
||||||
withPageName,
|
withPageName,
|
||||||
} from "./test_utils.ts";
|
} from "./test_utils.ts";
|
||||||
|
|
||||||
@@ -559,3 +563,20 @@ Deno.test({
|
|||||||
sanitizeOps: false,
|
sanitizeOps: false,
|
||||||
sanitizeResources: false,
|
sanitizeResources: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test("throws when passing non-jsx children to an island", async () => {
|
||||||
|
await withFresh(
|
||||||
|
"./tests/fixture_island_nesting/dev.ts",
|
||||||
|
async (address) => {
|
||||||
|
const doc = await fetchHtml(`${address}/island_invalid_children`);
|
||||||
|
|
||||||
|
assertSelector(doc, ".frsh-error-page");
|
||||||
|
assertTextMatch(doc, "pre", /Invalid JSX child passed to island/);
|
||||||
|
|
||||||
|
const doc2 = await fetchHtml(`${address}/island_invalid_children_fn`);
|
||||||
|
|
||||||
|
assertSelector(doc2, ".frsh-error-page");
|
||||||
|
assertTextMatch(doc2, "pre", /Invalid JSX child passed to island/);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|||||||
@@ -66,6 +66,23 @@ export function assertTextMany(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function assertTextMatch(
|
||||||
|
doc: Document,
|
||||||
|
selector: string,
|
||||||
|
regex: RegExp,
|
||||||
|
) {
|
||||||
|
const texts = Array.from(doc.querySelectorAll(selector)).map((el) =>
|
||||||
|
el.textContent
|
||||||
|
).filter(Boolean) as string[];
|
||||||
|
|
||||||
|
if (!texts.some((text) => regex.test(text))) {
|
||||||
|
const html = "\n\n" + prettyDom(doc);
|
||||||
|
throw new Error(
|
||||||
|
`Regex ${regex} did not match any text elements in HTML.\n\n${html}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const VOID_ELEMENTS =
|
export const VOID_ELEMENTS =
|
||||||
/^(?:area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/;
|
/^(?:area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/;
|
||||||
function prettyDom(doc: Document) {
|
function prettyDom(doc: Document) {
|
||||||
|
|||||||
Reference in New Issue
Block a user