diff --git a/src/runtime/entrypoints/main.ts b/src/runtime/entrypoints/main.ts index 30d19e40..c2fe076d 100644 --- a/src/runtime/entrypoints/main.ts +++ b/src/runtime/entrypoints/main.ts @@ -21,8 +21,8 @@ function createRootFragment( parentNode: parent, firstChild: replaceNode[0], childNodes: replaceNode, - insertBefore(node: Node, child: Node) { - parent.insertBefore(node, child); + insertBefore(node: Node, child: Node | null) { + parent.insertBefore(node, child ?? endMarker); }, appendChild(child: Node) { // We cannot blindly call `.append()` as that would add @@ -305,7 +305,14 @@ function _walkInner( _walkInner(islands, props, markerStack, vnodeStack, sib.firstChild); } - if (marker !== null && marker.kind === MarkerKind.Slot) { + // Pop vnode if current marker is a slot or we are an island marker + // that was created inside another island + if ( + marker !== null && + (marker.kind === MarkerKind.Slot || + markerStack.length > 1 && + markerStack[markerStack.length - 2].kind === MarkerKind.Island) + ) { vnodeStack.pop(); } } diff --git a/tests/fixture_island_nesting/deno.json b/tests/fixture_island_nesting/deno.json index 99152996..30d3b607 100644 --- a/tests/fixture_island_nesting/deno.json +++ b/tests/fixture_island_nesting/deno.json @@ -5,8 +5,8 @@ }, "imports": { "$fresh/": "../../", - "preact": "https://esm.sh/preact@10.15.1", - "preact/": "https://esm.sh/preact@10.15.1/", + "preact": "https://esm.sh/preact@10.16.0", + "preact/": "https://esm.sh/preact@10.16.0/", "preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.0", "@preact/signals": "https://esm.sh/*@preact/signals@1.1.3", "@preact/signals-core": "https://esm.sh/@preact/signals-core@1.2.3" diff --git a/tests/fixture_island_nesting/fresh.gen.ts b/tests/fixture_island_nesting/fresh.gen.ts index 99341850..1a50efab 100644 --- a/tests/fixture_island_nesting/fresh.gen.ts +++ b/tests/fixture_island_nesting/fresh.gen.ts @@ -4,39 +4,49 @@ import * as $0 from "./routes/index.tsx"; import * as $1 from "./routes/island_conditional.tsx"; -import * as $2 from "./routes/island_in_island.tsx"; -import * as $3 from "./routes/island_in_island_definition.tsx"; -import * as $4 from "./routes/island_jsx_child.tsx"; -import * as $5 from "./routes/island_jsx_children.tsx"; -import * as $6 from "./routes/island_jsx_island_jsx.tsx"; -import * as $7 from "./routes/island_jsx_text.tsx"; -import * as $8 from "./routes/island_nested_props.tsx"; -import * as $9 from "./routes/island_siblings.tsx"; +import * as $2 from "./routes/island_fn_child.tsx"; +import * as $3 from "./routes/island_in_island.tsx"; +import * as $4 from "./routes/island_in_island_definition.tsx"; +import * as $5 from "./routes/island_jsx_child.tsx"; +import * as $6 from "./routes/island_jsx_children.tsx"; +import * as $7 from "./routes/island_jsx_island_jsx.tsx"; +import * as $8 from "./routes/island_jsx_text.tsx"; +import * as $9 from "./routes/island_nested_props.tsx"; +import * as $10 from "./routes/island_order.tsx"; +import * as $11 from "./routes/island_siblings.tsx"; import * as $$0 from "./islands/BooleanButton.tsx"; -import * as $$1 from "./islands/Island.tsx"; -import * as $$2 from "./islands/IslandConditional.tsx"; -import * as $$3 from "./islands/IslandInsideIsland.tsx"; -import * as $$4 from "./islands/IslandWithProps.tsx"; +import * as $$1 from "./islands/FragmentIsland.tsx"; +import * as $$2 from "./islands/Island.tsx"; +import * as $$3 from "./islands/IslandCenter.tsx"; +import * as $$4 from "./islands/IslandConditional.tsx"; +import * as $$5 from "./islands/IslandFn.tsx"; +import * as $$6 from "./islands/IslandInsideIsland.tsx"; +import * as $$7 from "./islands/IslandWithProps.tsx"; const manifest = { routes: { "./routes/index.tsx": $0, "./routes/island_conditional.tsx": $1, - "./routes/island_in_island.tsx": $2, - "./routes/island_in_island_definition.tsx": $3, - "./routes/island_jsx_child.tsx": $4, - "./routes/island_jsx_children.tsx": $5, - "./routes/island_jsx_island_jsx.tsx": $6, - "./routes/island_jsx_text.tsx": $7, - "./routes/island_nested_props.tsx": $8, - "./routes/island_siblings.tsx": $9, + "./routes/island_fn_child.tsx": $2, + "./routes/island_in_island.tsx": $3, + "./routes/island_in_island_definition.tsx": $4, + "./routes/island_jsx_child.tsx": $5, + "./routes/island_jsx_children.tsx": $6, + "./routes/island_jsx_island_jsx.tsx": $7, + "./routes/island_jsx_text.tsx": $8, + "./routes/island_nested_props.tsx": $9, + "./routes/island_order.tsx": $10, + "./routes/island_siblings.tsx": $11, }, islands: { "./islands/BooleanButton.tsx": $$0, - "./islands/Island.tsx": $$1, - "./islands/IslandConditional.tsx": $$2, - "./islands/IslandInsideIsland.tsx": $$3, - "./islands/IslandWithProps.tsx": $$4, + "./islands/FragmentIsland.tsx": $$1, + "./islands/Island.tsx": $$2, + "./islands/IslandCenter.tsx": $$3, + "./islands/IslandConditional.tsx": $$4, + "./islands/IslandFn.tsx": $$5, + "./islands/IslandInsideIsland.tsx": $$6, + "./islands/IslandWithProps.tsx": $$7, }, baseUrl: import.meta.url, }; diff --git a/tests/fixture_island_nesting/islands/FragmentIsland.tsx b/tests/fixture_island_nesting/islands/FragmentIsland.tsx new file mode 100644 index 00000000..ccb9b0ee --- /dev/null +++ b/tests/fixture_island_nesting/islands/FragmentIsland.tsx @@ -0,0 +1,8 @@ +export default function FragmentIsland() { + return ( + <> +

it{" "}

+

works

+ + ); +} diff --git a/tests/fixture_island_nesting/islands/IslandCenter.tsx b/tests/fixture_island_nesting/islands/IslandCenter.tsx new file mode 100644 index 00000000..f01b83a2 --- /dev/null +++ b/tests/fixture_island_nesting/islands/IslandCenter.tsx @@ -0,0 +1,3 @@ +export default function IslandCenter() { + return

center

; +} diff --git a/tests/fixture_island_nesting/islands/IslandFn.tsx b/tests/fixture_island_nesting/islands/IslandFn.tsx new file mode 100644 index 00000000..792ba4b8 --- /dev/null +++ b/tests/fixture_island_nesting/islands/IslandFn.tsx @@ -0,0 +1,17 @@ +import { VNode } from "preact"; + +import FragmentIsland from "./FragmentIsland.tsx"; + +function Foo(props: { children: () => VNode }) { + return props.children(); +} + +export default function IslandFn() { + return ( +
+ + {() => } + +
+ ); +} diff --git a/tests/fixture_island_nesting/routes/island_fn_child.tsx b/tests/fixture_island_nesting/routes/island_fn_child.tsx new file mode 100644 index 00000000..fcf8f42a --- /dev/null +++ b/tests/fixture_island_nesting/routes/island_fn_child.tsx @@ -0,0 +1,9 @@ +import IslandFn from "../islands/IslandFn.tsx"; + +export default function Home() { + return ( +
+ +
+ ); +} diff --git a/tests/fixture_island_nesting/routes/island_order.tsx b/tests/fixture_island_nesting/routes/island_order.tsx new file mode 100644 index 00000000..34f4e231 --- /dev/null +++ b/tests/fixture_island_nesting/routes/island_order.tsx @@ -0,0 +1,11 @@ +import IslandCenter from "../islands/IslandCenter.tsx"; + +export default function IslandOrder() { + return ( +
+

left

+ +

right

+
+ ); +} diff --git a/tests/islands_test.ts b/tests/islands_test.ts index 324d6f6d..8e022f8d 100644 --- a/tests/islands_test.ts +++ b/tests/islands_test.ts @@ -442,3 +442,39 @@ Deno.test({ sanitizeOps: false, sanitizeResources: false, }); + +Deno.test({ + name: "render island inside island when passed as fn child", + + async fn(_t) { + await withPageName( + "./tests/fixture_island_nesting/main.ts", + async (page, address) => { + await page.goto(`${address}/island_fn_child`); + await page.waitForSelector(".island"); + await waitForText(page, "#page", "it works"); + }, + ); + }, + + sanitizeOps: false, + sanitizeResources: false, +}); + +Deno.test({ + name: "render nested islands in correct order", + + async fn(_t) { + await withPageName( + "./tests/fixture_island_nesting/main.ts", + async (page, address) => { + await page.goto(`${address}/island_order`); + await page.waitForSelector(".island"); + await waitForText(page, "#page", "leftcenterright"); + }, + ); + }, + + sanitizeOps: false, + sanitizeResources: false, +});