diff --git a/src/server/rendering/preact_hooks.ts b/src/server/rendering/preact_hooks.ts index 984f51b4..122f5106 100644 --- a/src/server/rendering/preact_hooks.ts +++ b/src/server/rendering/preact_hooks.ts @@ -237,14 +237,17 @@ options.__b = (vnode: VNode>) => { // Guard against passing objects as children to JSX if ( - children !== null && !Array.isArray(children) && - !isValidElement(children) + typeof children === "function" || ( + children !== null && typeof children === "object" && + !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.`, + `Invalid JSX child passed to island <${name} />. To resolve this error, pass the data as a standard prop instead.`, ); } diff --git a/tests/fixture_island_nesting/fresh.gen.ts b/tests/fixture_island_nesting/fresh.gen.ts index 21208815..ca11ebed 100644 --- a/tests/fixture_island_nesting/fresh.gen.ts +++ b/tests/fixture_island_nesting/fresh.gen.ts @@ -18,6 +18,7 @@ import * as $12 from "./routes/island_jsx_text.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 $16 from "./routes/island_valid_children.tsx"; import * as $$0 from "./islands/BooleanButton.tsx"; import * as $$1 from "./islands/Counter.tsx"; import * as $$2 from "./islands/FragmentIsland.tsx"; @@ -47,6 +48,7 @@ const manifest = { "./routes/island_nested_props.tsx": $13, "./routes/island_order.tsx": $14, "./routes/island_siblings.tsx": $15, + "./routes/island_valid_children.tsx": $16, }, islands: { "./islands/BooleanButton.tsx": $$0, diff --git a/tests/fixture_island_nesting/routes/island_valid_children.tsx b/tests/fixture_island_nesting/routes/island_valid_children.tsx new file mode 100644 index 00000000..735007a5 --- /dev/null +++ b/tests/fixture_island_nesting/routes/island_valid_children.tsx @@ -0,0 +1,24 @@ +import { PassThrough } from "../islands/PassThrough.tsx"; + +function Foo() { + return

foo

; +} + +export default function Page() { + return ( +
+ hello + {2} + {null} + {true} + {false} + {undefined} + +

hello

+
+ + + +
+ ); +} diff --git a/tests/islands_test.ts b/tests/islands_test.ts index b9ecce8d..72c3727c 100644 --- a/tests/islands_test.ts +++ b/tests/islands_test.ts @@ -6,6 +6,7 @@ import { Page, } from "./deps.ts"; import { + assertNotSelector, assertSelector, assertTextMatch, clickWhenListenerReady, @@ -564,7 +565,7 @@ Deno.test({ sanitizeResources: false, }); -Deno.test("throws when passing non-jsx children to an island", async () => { +Deno.test("throws when passing non-jsx children to an island", async (t) => { await withFresh( "./tests/fixture_island_nesting/dev.ts", async (address) => { @@ -577,6 +578,12 @@ Deno.test("throws when passing non-jsx children to an island", async () => { assertSelector(doc2, ".frsh-error-page"); assertTextMatch(doc2, "pre", /Invalid JSX child passed to island/); + + await t.step("should not throw on valid children", async () => { + const doc2 = await fetchHtml(`${address}/island_valid_children`); + + assertNotSelector(doc2, ".frsh-error-page"); + }); }, ); });