fix: conditional sibling islands DOM order (#1324)
This commit is contained in:
@@ -12,6 +12,7 @@ import { assetHashingHook } from "../utils.ts";
|
|||||||
function createRootFragment(
|
function createRootFragment(
|
||||||
parent: Element,
|
parent: Element,
|
||||||
replaceNode: Node | Node[],
|
replaceNode: Node | Node[],
|
||||||
|
endMarker: Text,
|
||||||
) {
|
) {
|
||||||
replaceNode = ([] as Node[]).concat(replaceNode);
|
replaceNode = ([] as Node[]).concat(replaceNode);
|
||||||
// @ts-ignore this is fine
|
// @ts-ignore this is fine
|
||||||
@@ -24,7 +25,11 @@ function createRootFragment(
|
|||||||
parent.insertBefore(node, child);
|
parent.insertBefore(node, child);
|
||||||
},
|
},
|
||||||
appendChild(child: Node) {
|
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) {
|
removeChild(child: Node) {
|
||||||
parent.removeChild(child);
|
parent.removeChild(child);
|
||||||
@@ -203,12 +208,24 @@ function _walkInner(
|
|||||||
const vnode = vnodeStack.pop();
|
const vnode = vnodeStack.pop();
|
||||||
|
|
||||||
const parentNode = sib.parentNode! as HTMLElement;
|
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 = () =>
|
const _render = () =>
|
||||||
render(
|
render(
|
||||||
vnode,
|
vnode,
|
||||||
createRootFragment(
|
createRootFragment(
|
||||||
parentNode,
|
parentNode,
|
||||||
children,
|
children,
|
||||||
|
endMarker,
|
||||||
// deno-lint-ignore no-explicit-any
|
// deno-lint-ignore no-explicit-any
|
||||||
) as any as HTMLElement,
|
) as any as HTMLElement,
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -3,34 +3,40 @@
|
|||||||
// This file is automatically updated during development when running `dev.ts`.
|
// This file is automatically updated during development when running `dev.ts`.
|
||||||
|
|
||||||
import * as $0 from "./routes/index.tsx";
|
import * as $0 from "./routes/index.tsx";
|
||||||
import * as $1 from "./routes/island_in_island.tsx";
|
import * as $1 from "./routes/island_conditional.tsx";
|
||||||
import * as $2 from "./routes/island_in_island_definition.tsx";
|
import * as $2 from "./routes/island_in_island.tsx";
|
||||||
import * as $3 from "./routes/island_jsx_child.tsx";
|
import * as $3 from "./routes/island_in_island_definition.tsx";
|
||||||
import * as $4 from "./routes/island_jsx_children.tsx";
|
import * as $4 from "./routes/island_jsx_child.tsx";
|
||||||
import * as $5 from "./routes/island_jsx_island_jsx.tsx";
|
import * as $5 from "./routes/island_jsx_children.tsx";
|
||||||
import * as $6 from "./routes/island_jsx_text.tsx";
|
import * as $6 from "./routes/island_jsx_island_jsx.tsx";
|
||||||
import * as $7 from "./routes/island_nested_props.tsx";
|
import * as $7 from "./routes/island_jsx_text.tsx";
|
||||||
import * as $8 from "./routes/island_siblings.tsx";
|
import * as $8 from "./routes/island_nested_props.tsx";
|
||||||
import * as $$0 from "./islands/Island.tsx";
|
import * as $9 from "./routes/island_siblings.tsx";
|
||||||
import * as $$1 from "./islands/IslandInsideIsland.tsx";
|
import * as $$0 from "./islands/BooleanButton.tsx";
|
||||||
import * as $$2 from "./islands/IslandWithProps.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 = {
|
const manifest = {
|
||||||
routes: {
|
routes: {
|
||||||
"./routes/index.tsx": $0,
|
"./routes/index.tsx": $0,
|
||||||
"./routes/island_in_island.tsx": $1,
|
"./routes/island_conditional.tsx": $1,
|
||||||
"./routes/island_in_island_definition.tsx": $2,
|
"./routes/island_in_island.tsx": $2,
|
||||||
"./routes/island_jsx_child.tsx": $3,
|
"./routes/island_in_island_definition.tsx": $3,
|
||||||
"./routes/island_jsx_children.tsx": $4,
|
"./routes/island_jsx_child.tsx": $4,
|
||||||
"./routes/island_jsx_island_jsx.tsx": $5,
|
"./routes/island_jsx_children.tsx": $5,
|
||||||
"./routes/island_jsx_text.tsx": $6,
|
"./routes/island_jsx_island_jsx.tsx": $6,
|
||||||
"./routes/island_nested_props.tsx": $7,
|
"./routes/island_jsx_text.tsx": $7,
|
||||||
"./routes/island_siblings.tsx": $8,
|
"./routes/island_nested_props.tsx": $8,
|
||||||
|
"./routes/island_siblings.tsx": $9,
|
||||||
},
|
},
|
||||||
islands: {
|
islands: {
|
||||||
"./islands/Island.tsx": $$0,
|
"./islands/BooleanButton.tsx": $$0,
|
||||||
"./islands/IslandInsideIsland.tsx": $$1,
|
"./islands/Island.tsx": $$1,
|
||||||
"./islands/IslandWithProps.tsx": $$2,
|
"./islands/IslandConditional.tsx": $$2,
|
||||||
|
"./islands/IslandInsideIsland.tsx": $$3,
|
||||||
|
"./islands/IslandWithProps.tsx": $$4,
|
||||||
},
|
},
|
||||||
baseUrl: import.meta.url,
|
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>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -434,6 +434,37 @@ Deno.test({
|
|||||||
sanitizeResources: false,
|
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({
|
Deno.test({
|
||||||
name: "serialize inner island props",
|
name: "serialize inner island props",
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user