fix: incorrect island child validation (#1740)
This commit is contained in:
@@ -237,14 +237,17 @@ options.__b = (vnode: VNode<Record<string, unknown>>) => {
|
|||||||
|
|
||||||
// Guard against passing objects as children to JSX
|
// Guard against passing objects as children to JSX
|
||||||
if (
|
if (
|
||||||
children !== null && !Array.isArray(children) &&
|
typeof children === "function" || (
|
||||||
!isValidElement(children)
|
children !== null && typeof children === "object" &&
|
||||||
|
!Array.isArray(children) &&
|
||||||
|
!isValidElement(children)
|
||||||
|
)
|
||||||
) {
|
) {
|
||||||
const name = originalType.displayName || originalType.name ||
|
const name = originalType.displayName || originalType.name ||
|
||||||
"Anonymous";
|
"Anonymous";
|
||||||
|
|
||||||
throw new Error(
|
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.`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import * as $12 from "./routes/island_jsx_text.tsx";
|
|||||||
import * as $13 from "./routes/island_nested_props.tsx";
|
import * as $13 from "./routes/island_nested_props.tsx";
|
||||||
import * as $14 from "./routes/island_order.tsx";
|
import * as $14 from "./routes/island_order.tsx";
|
||||||
import * as $15 from "./routes/island_siblings.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 $$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";
|
||||||
@@ -47,6 +48,7 @@ const manifest = {
|
|||||||
"./routes/island_nested_props.tsx": $13,
|
"./routes/island_nested_props.tsx": $13,
|
||||||
"./routes/island_order.tsx": $14,
|
"./routes/island_order.tsx": $14,
|
||||||
"./routes/island_siblings.tsx": $15,
|
"./routes/island_siblings.tsx": $15,
|
||||||
|
"./routes/island_valid_children.tsx": $16,
|
||||||
},
|
},
|
||||||
islands: {
|
islands: {
|
||||||
"./islands/BooleanButton.tsx": $$0,
|
"./islands/BooleanButton.tsx": $$0,
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
import { PassThrough } from "../islands/PassThrough.tsx";
|
||||||
|
|
||||||
|
function Foo() {
|
||||||
|
return <h1>foo</h1>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function Page() {
|
||||||
|
return (
|
||||||
|
<div id="page">
|
||||||
|
<PassThrough>hello</PassThrough>
|
||||||
|
<PassThrough>{2}</PassThrough>
|
||||||
|
<PassThrough>{null}</PassThrough>
|
||||||
|
<PassThrough>{true}</PassThrough>
|
||||||
|
<PassThrough>{false}</PassThrough>
|
||||||
|
<PassThrough>{undefined}</PassThrough>
|
||||||
|
<PassThrough>
|
||||||
|
<h1>hello</h1>
|
||||||
|
</PassThrough>
|
||||||
|
<PassThrough>
|
||||||
|
<Foo />
|
||||||
|
</PassThrough>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ import {
|
|||||||
Page,
|
Page,
|
||||||
} from "./deps.ts";
|
} from "./deps.ts";
|
||||||
import {
|
import {
|
||||||
|
assertNotSelector,
|
||||||
assertSelector,
|
assertSelector,
|
||||||
assertTextMatch,
|
assertTextMatch,
|
||||||
clickWhenListenerReady,
|
clickWhenListenerReady,
|
||||||
@@ -564,7 +565,7 @@ Deno.test({
|
|||||||
sanitizeResources: false,
|
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(
|
await withFresh(
|
||||||
"./tests/fixture_island_nesting/dev.ts",
|
"./tests/fixture_island_nesting/dev.ts",
|
||||||
async (address) => {
|
async (address) => {
|
||||||
@@ -577,6 +578,12 @@ Deno.test("throws when passing non-jsx children to an island", async () => {
|
|||||||
|
|
||||||
assertSelector(doc2, ".frsh-error-page");
|
assertSelector(doc2, ".frsh-error-page");
|
||||||
assertTextMatch(doc2, "pre", /Invalid JSX child passed to island/);
|
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");
|
||||||
|
});
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user