From 2b92b1c526c98a870d9cae6bdf7f876c18fe6faa Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Mon, 26 Jun 2023 13:57:05 +0200 Subject: [PATCH] fix: conditional sibling islands DOM order (#1324) --- src/runtime/entrypoints/main.ts | 19 ++++++- tests/fixture_island_nesting/fresh.gen.ts | 50 +++++++++++-------- .../islands/BooleanButton.tsx | 13 +++++ .../islands/IslandConditional.tsx | 9 ++++ .../routes/island_conditional.tsx | 14 ++++++ tests/islands_test.ts | 31 ++++++++++++ 6 files changed, 113 insertions(+), 23 deletions(-) create mode 100644 tests/fixture_island_nesting/islands/BooleanButton.tsx create mode 100644 tests/fixture_island_nesting/islands/IslandConditional.tsx create mode 100644 tests/fixture_island_nesting/routes/island_conditional.tsx diff --git a/src/runtime/entrypoints/main.ts b/src/runtime/entrypoints/main.ts index 39c545b9..0eeece30 100644 --- a/src/runtime/entrypoints/main.ts +++ b/src/runtime/entrypoints/main.ts @@ -12,6 +12,7 @@ import { assetHashingHook } from "../utils.ts"; function createRootFragment( parent: Element, replaceNode: Node | Node[], + endMarker: Text, ) { replaceNode = ([] as Node[]).concat(replaceNode); // @ts-ignore this is fine @@ -24,7 +25,11 @@ function createRootFragment( parent.insertBefore(node, child); }, appendChild(child: Node) { - parent.appendChild(child); + // We cannot blindly call `.append()` as that would add + // the new child to the very end of the parent node. This + // leads to ordering issues when the multiple islands + // share the same parent node. + parent.insertBefore(child, endMarker); }, removeChild(child: Node) { parent.removeChild(child); @@ -203,12 +208,24 @@ function _walkInner( const vnode = vnodeStack.pop(); const parentNode = sib.parentNode! as HTMLElement; + + // We need an end marker for islands because multiple + // islands can share the same parent node. Since + // islands are root-level render calls any calls to + // `.appendChild` would lead to a wrong result. + const endMarker = new Text(""); + parentNode.insertBefore( + endMarker, + marker.endNode, + ); + const _render = () => render( vnode, createRootFragment( parentNode, children, + endMarker, // deno-lint-ignore no-explicit-any ) as any as HTMLElement, ); diff --git a/tests/fixture_island_nesting/fresh.gen.ts b/tests/fixture_island_nesting/fresh.gen.ts index 070d9e8c..99341850 100644 --- a/tests/fixture_island_nesting/fresh.gen.ts +++ b/tests/fixture_island_nesting/fresh.gen.ts @@ -3,34 +3,40 @@ // This file is automatically updated during development when running `dev.ts`. import * as $0 from "./routes/index.tsx"; -import * as $1 from "./routes/island_in_island.tsx"; -import * as $2 from "./routes/island_in_island_definition.tsx"; -import * as $3 from "./routes/island_jsx_child.tsx"; -import * as $4 from "./routes/island_jsx_children.tsx"; -import * as $5 from "./routes/island_jsx_island_jsx.tsx"; -import * as $6 from "./routes/island_jsx_text.tsx"; -import * as $7 from "./routes/island_nested_props.tsx"; -import * as $8 from "./routes/island_siblings.tsx"; -import * as $$0 from "./islands/Island.tsx"; -import * as $$1 from "./islands/IslandInsideIsland.tsx"; -import * as $$2 from "./islands/IslandWithProps.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 $$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"; const manifest = { routes: { "./routes/index.tsx": $0, - "./routes/island_in_island.tsx": $1, - "./routes/island_in_island_definition.tsx": $2, - "./routes/island_jsx_child.tsx": $3, - "./routes/island_jsx_children.tsx": $4, - "./routes/island_jsx_island_jsx.tsx": $5, - "./routes/island_jsx_text.tsx": $6, - "./routes/island_nested_props.tsx": $7, - "./routes/island_siblings.tsx": $8, + "./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, }, islands: { - "./islands/Island.tsx": $$0, - "./islands/IslandInsideIsland.tsx": $$1, - "./islands/IslandWithProps.tsx": $$2, + "./islands/BooleanButton.tsx": $$0, + "./islands/Island.tsx": $$1, + "./islands/IslandConditional.tsx": $$2, + "./islands/IslandInsideIsland.tsx": $$3, + "./islands/IslandWithProps.tsx": $$4, }, baseUrl: import.meta.url, }; diff --git a/tests/fixture_island_nesting/islands/BooleanButton.tsx b/tests/fixture_island_nesting/islands/BooleanButton.tsx new file mode 100644 index 00000000..33ddd6d7 --- /dev/null +++ b/tests/fixture_island_nesting/islands/BooleanButton.tsx @@ -0,0 +1,13 @@ +import { Signal } from "@preact/signals"; + +export default function BooleanButton({ signal }: { signal: Signal }) { + return ( + + ); +} diff --git a/tests/fixture_island_nesting/islands/IslandConditional.tsx b/tests/fixture_island_nesting/islands/IslandConditional.tsx new file mode 100644 index 00000000..5f54983b --- /dev/null +++ b/tests/fixture_island_nesting/islands/IslandConditional.tsx @@ -0,0 +1,9 @@ +import { Signal } from "@preact/signals"; + +export interface IslandConditionalProps { + show: Signal; +} + +export default function IslandConditional({ show }: IslandConditionalProps) { + return show.value ? <>it works : null; +} diff --git a/tests/fixture_island_nesting/routes/island_conditional.tsx b/tests/fixture_island_nesting/routes/island_conditional.tsx new file mode 100644 index 00000000..98cb4b4f --- /dev/null +++ b/tests/fixture_island_nesting/routes/island_conditional.tsx @@ -0,0 +1,14 @@ +import IslandConditional from "../islands/IslandConditional.tsx"; +import BooleanButton from "../islands/BooleanButton.tsx"; +import { signal } from "@preact/signals"; + +const show = signal(false); + +export default function Page() { + return ( +
+ + +
+ ); +} diff --git a/tests/islands_test.ts b/tests/islands_test.ts index 271c1c3f..3ca37f83 100644 --- a/tests/islands_test.ts +++ b/tests/islands_test.ts @@ -434,6 +434,37 @@ Deno.test({ sanitizeResources: false, }); +Deno.test({ + name: "render sibling islands that render nothing initially", + + async fn(_t) { + await withPageName( + "./tests/fixture_island_nesting/main.ts", + async (page, address) => { + await page.goto(`${address}/island_conditional`, { + waitUntil: "networkidle2", + }); + await page.waitForSelector("button"); + + await delay(100); + await page.click("button"); + + const text = await page.$eval( + "#page", + (el) => el.textContent, + ); + // Button text is matched too, but this allows us + // to assert correct ordering. The "it works" should + // be left of "Toggle" + assertEquals(text, "it worksToggle"); + }, + ); + }, + + sanitizeOps: false, + sanitizeResources: false, +}); + Deno.test({ name: "serialize inner island props",