fix: conditional sibling islands DOM order (#1324)

This commit is contained in:
Marvin Hagemeister
2023-06-26 13:57:05 +02:00
committed by GitHub
parent 62aaffa314
commit 2b92b1c526
6 changed files with 113 additions and 23 deletions
+18 -1
View File
@@ -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,
);
+28 -22
View File
@@ -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,
};
@@ -0,0 +1,13 @@
import { Signal } from "@preact/signals";
export default function BooleanButton({ signal }: { signal: Signal }) {
return (
<button
onClick={() => {
signal.value = !signal.value;
}}
>
Toggle
</button>
);
}
@@ -0,0 +1,9 @@
import { Signal } from "@preact/signals";
export interface IslandConditionalProps {
show: Signal<boolean>;
}
export default function IslandConditional({ show }: IslandConditionalProps) {
return show.value ? <>it works</> : null;
}
@@ -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 (
<div id="page">
<IslandConditional show={show} />
<BooleanButton signal={show} />
</div>
);
}
+31
View File
@@ -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",