fix: conditional island children not rendered (#1822)
This commit is contained in:
@@ -90,7 +90,7 @@ ServerComponent.displayName = "PreactServerComponent";
|
||||
|
||||
function addPropsChild(parent: VNode, vnode: ComponentChildren) {
|
||||
const props = parent.props;
|
||||
if (props.children === null) {
|
||||
if (props.children == null) {
|
||||
props.children = vnode;
|
||||
} else {
|
||||
if (!Array.isArray(props.children)) {
|
||||
@@ -118,7 +118,7 @@ interface Marker {
|
||||
endNode: Text | Comment | null;
|
||||
}
|
||||
|
||||
const HIDE_MARKERS = true;
|
||||
const SHOW_MARKERS = false;
|
||||
|
||||
/**
|
||||
* Replace comment markers with empty text nodes to hide them
|
||||
@@ -129,7 +129,7 @@ function hideMarker(marker: Marker) {
|
||||
const parent = endNode!.parentNode!;
|
||||
|
||||
if (
|
||||
!HIDE_MARKERS && startNode !== null &&
|
||||
SHOW_MARKERS && startNode !== null &&
|
||||
startNode.nodeType === Node.COMMENT_NODE
|
||||
) {
|
||||
const text = new Text("");
|
||||
@@ -139,7 +139,7 @@ function hideMarker(marker: Marker) {
|
||||
}
|
||||
|
||||
if (
|
||||
!HIDE_MARKERS && endNode !== null && endNode.nodeType === Node.COMMENT_NODE
|
||||
SHOW_MARKERS && endNode !== null && endNode.nodeType === Node.COMMENT_NODE
|
||||
) {
|
||||
const text = new Text("");
|
||||
marker.endNode = text;
|
||||
@@ -148,6 +148,50 @@ function hideMarker(marker: Marker) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If an islands children are `null` then it might be a conditionally
|
||||
* rendered one which was initially not visible. In these cases we
|
||||
* send a `<template>` tag with the "would be rendered" children to
|
||||
* the client. This function checks for that
|
||||
*/
|
||||
function addChildrenFromTemplate(
|
||||
islands: Record<string, Record<string, ComponentType>>,
|
||||
// deno-lint-ignore no-explicit-any
|
||||
props: any[],
|
||||
markerStack: Marker[],
|
||||
vnodeStack: VNode[],
|
||||
comment: string,
|
||||
) {
|
||||
const [id, exportName, n] = comment.slice("/frsh-".length).split(
|
||||
":",
|
||||
);
|
||||
|
||||
const sel = `#frsh-slot-${id}-${exportName}-${n}-children`;
|
||||
const template = document.querySelector(sel) as
|
||||
| HTMLTemplateElement
|
||||
| null;
|
||||
|
||||
if (template !== null) {
|
||||
markerStack.push({
|
||||
kind: MarkerKind.Slot,
|
||||
endNode: null,
|
||||
startNode: null,
|
||||
text: comment.slice(1),
|
||||
});
|
||||
|
||||
const node = template.content.cloneNode(true);
|
||||
_walkInner(
|
||||
islands,
|
||||
props,
|
||||
markerStack,
|
||||
vnodeStack,
|
||||
node,
|
||||
);
|
||||
|
||||
markerStack.pop();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Revive islands and stich together any server rendered content.
|
||||
*
|
||||
@@ -247,34 +291,13 @@ function _walkInner(
|
||||
const vnode = vnodeStack[vnodeStack.length - 1];
|
||||
|
||||
if (vnode.props.children == null) {
|
||||
const [id, exportName, n] = comment.slice("/frsh-".length).split(
|
||||
":",
|
||||
addChildrenFromTemplate(
|
||||
islands,
|
||||
props,
|
||||
markerStack,
|
||||
vnodeStack,
|
||||
comment,
|
||||
);
|
||||
|
||||
const sel = `#frsh-slot-${id}-${exportName}-${n}-children`;
|
||||
const template = document.querySelector(sel) as
|
||||
| HTMLTemplateElement
|
||||
| null;
|
||||
|
||||
if (template !== null) {
|
||||
markerStack.push({
|
||||
kind: MarkerKind.Slot,
|
||||
endNode: null,
|
||||
startNode: null,
|
||||
text: "foo",
|
||||
});
|
||||
|
||||
const node = template.content.cloneNode(true);
|
||||
_walkInner(
|
||||
islands,
|
||||
props,
|
||||
markerStack,
|
||||
vnodeStack,
|
||||
node,
|
||||
);
|
||||
|
||||
markerStack.pop();
|
||||
}
|
||||
}
|
||||
vnodeStack.pop();
|
||||
|
||||
@@ -306,9 +329,31 @@ function _walkInner(
|
||||
} else if (parent?.kind === MarkerKind.Slot) {
|
||||
// Treat the island as a standard component when it
|
||||
// has an island parent or a slot parent
|
||||
const vnode = vnodeStack.pop();
|
||||
const vnode = vnodeStack[vnodeStack.length - 1];
|
||||
if (vnode && vnode.props.children == null) {
|
||||
addChildrenFromTemplate(
|
||||
islands,
|
||||
props,
|
||||
markerStack,
|
||||
vnodeStack,
|
||||
comment,
|
||||
);
|
||||
|
||||
// Didn't find any template tag, proceed as usual
|
||||
if (vnode.props.children == null) {
|
||||
vnodeStack.pop();
|
||||
}
|
||||
} else {
|
||||
vnodeStack.pop();
|
||||
}
|
||||
|
||||
marker.endNode = sib;
|
||||
hideMarker(marker);
|
||||
|
||||
const parent = vnodeStack[vnodeStack.length - 1]!;
|
||||
addPropsChild(parent, vnode);
|
||||
sib = marker.endNode.nextSibling;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
} else if (comment.startsWith("frsh")) {
|
||||
|
||||
@@ -2,65 +2,69 @@
|
||||
// This file SHOULD be checked into source version control.
|
||||
// This file is automatically updated during development when running `dev.ts`.
|
||||
|
||||
import * as $0 from "./routes/index.tsx";
|
||||
import * as $1 from "./routes/island_conditional.tsx";
|
||||
import * as $2 from "./routes/island_conditional_lazy.tsx";
|
||||
import * as $3 from "./routes/island_conditional_lazy_island.tsx";
|
||||
import * as $4 from "./routes/island_fn_child.tsx";
|
||||
import * as $5 from "./routes/island_in_island.tsx";
|
||||
import * as $6 from "./routes/island_in_island_definition.tsx";
|
||||
import * as $7 from "./routes/island_invalid_children.tsx";
|
||||
import * as $8 from "./routes/island_invalid_children_fn.tsx";
|
||||
import * as $9 from "./routes/island_jsx_child.tsx";
|
||||
import * as $10 from "./routes/island_jsx_children.tsx";
|
||||
import * as $11 from "./routes/island_jsx_island_jsx.tsx";
|
||||
import * as $12 from "./routes/island_jsx_text.tsx";
|
||||
import * as $13 from "./routes/island_nested_props.tsx";
|
||||
import * as $14 from "./routes/island_order.tsx";
|
||||
import * as $15 from "./routes/island_siblings.tsx";
|
||||
import * as $16 from "./routes/island_valid_children.tsx";
|
||||
import * as $0 from "./routes/dropdown.tsx";
|
||||
import * as $1 from "./routes/index.tsx";
|
||||
import * as $2 from "./routes/island_conditional.tsx";
|
||||
import * as $3 from "./routes/island_conditional_lazy.tsx";
|
||||
import * as $4 from "./routes/island_conditional_lazy_island.tsx";
|
||||
import * as $5 from "./routes/island_fn_child.tsx";
|
||||
import * as $6 from "./routes/island_in_island.tsx";
|
||||
import * as $7 from "./routes/island_in_island_definition.tsx";
|
||||
import * as $8 from "./routes/island_invalid_children.tsx";
|
||||
import * as $9 from "./routes/island_invalid_children_fn.tsx";
|
||||
import * as $10 from "./routes/island_jsx_child.tsx";
|
||||
import * as $11 from "./routes/island_jsx_children.tsx";
|
||||
import * as $12 from "./routes/island_jsx_island_jsx.tsx";
|
||||
import * as $13 from "./routes/island_jsx_text.tsx";
|
||||
import * as $14 from "./routes/island_nested_props.tsx";
|
||||
import * as $15 from "./routes/island_order.tsx";
|
||||
import * as $16 from "./routes/island_siblings.tsx";
|
||||
import * as $17 from "./routes/island_valid_children.tsx";
|
||||
import * as $$0 from "./islands/BooleanButton.tsx";
|
||||
import * as $$1 from "./islands/Counter.tsx";
|
||||
import * as $$2 from "./islands/FragmentIsland.tsx";
|
||||
import * as $$3 from "./islands/Island.tsx";
|
||||
import * as $$4 from "./islands/IslandCenter.tsx";
|
||||
import * as $$5 from "./islands/IslandConditional.tsx";
|
||||
import * as $$6 from "./islands/IslandFn.tsx";
|
||||
import * as $$7 from "./islands/IslandInsideIsland.tsx";
|
||||
import * as $$8 from "./islands/IslandWithProps.tsx";
|
||||
import * as $$9 from "./islands/PassThrough.tsx";
|
||||
import * as $$2 from "./islands/Dropdown.tsx";
|
||||
import * as $$3 from "./islands/FragmentIsland.tsx";
|
||||
import * as $$4 from "./islands/Island.tsx";
|
||||
import * as $$5 from "./islands/IslandCenter.tsx";
|
||||
import * as $$6 from "./islands/IslandConditional.tsx";
|
||||
import * as $$7 from "./islands/IslandFn.tsx";
|
||||
import * as $$8 from "./islands/IslandInsideIsland.tsx";
|
||||
import * as $$9 from "./islands/IslandWithProps.tsx";
|
||||
import * as $$10 from "./islands/PassThrough.tsx";
|
||||
|
||||
const manifest = {
|
||||
routes: {
|
||||
"./routes/index.tsx": $0,
|
||||
"./routes/island_conditional.tsx": $1,
|
||||
"./routes/island_conditional_lazy.tsx": $2,
|
||||
"./routes/island_conditional_lazy_island.tsx": $3,
|
||||
"./routes/island_fn_child.tsx": $4,
|
||||
"./routes/island_in_island.tsx": $5,
|
||||
"./routes/island_in_island_definition.tsx": $6,
|
||||
"./routes/island_invalid_children.tsx": $7,
|
||||
"./routes/island_invalid_children_fn.tsx": $8,
|
||||
"./routes/island_jsx_child.tsx": $9,
|
||||
"./routes/island_jsx_children.tsx": $10,
|
||||
"./routes/island_jsx_island_jsx.tsx": $11,
|
||||
"./routes/island_jsx_text.tsx": $12,
|
||||
"./routes/island_nested_props.tsx": $13,
|
||||
"./routes/island_order.tsx": $14,
|
||||
"./routes/island_siblings.tsx": $15,
|
||||
"./routes/island_valid_children.tsx": $16,
|
||||
"./routes/dropdown.tsx": $0,
|
||||
"./routes/index.tsx": $1,
|
||||
"./routes/island_conditional.tsx": $2,
|
||||
"./routes/island_conditional_lazy.tsx": $3,
|
||||
"./routes/island_conditional_lazy_island.tsx": $4,
|
||||
"./routes/island_fn_child.tsx": $5,
|
||||
"./routes/island_in_island.tsx": $6,
|
||||
"./routes/island_in_island_definition.tsx": $7,
|
||||
"./routes/island_invalid_children.tsx": $8,
|
||||
"./routes/island_invalid_children_fn.tsx": $9,
|
||||
"./routes/island_jsx_child.tsx": $10,
|
||||
"./routes/island_jsx_children.tsx": $11,
|
||||
"./routes/island_jsx_island_jsx.tsx": $12,
|
||||
"./routes/island_jsx_text.tsx": $13,
|
||||
"./routes/island_nested_props.tsx": $14,
|
||||
"./routes/island_order.tsx": $15,
|
||||
"./routes/island_siblings.tsx": $16,
|
||||
"./routes/island_valid_children.tsx": $17,
|
||||
},
|
||||
islands: {
|
||||
"./islands/BooleanButton.tsx": $$0,
|
||||
"./islands/Counter.tsx": $$1,
|
||||
"./islands/FragmentIsland.tsx": $$2,
|
||||
"./islands/Island.tsx": $$3,
|
||||
"./islands/IslandCenter.tsx": $$4,
|
||||
"./islands/IslandConditional.tsx": $$5,
|
||||
"./islands/IslandFn.tsx": $$6,
|
||||
"./islands/IslandInsideIsland.tsx": $$7,
|
||||
"./islands/IslandWithProps.tsx": $$8,
|
||||
"./islands/PassThrough.tsx": $$9,
|
||||
"./islands/Dropdown.tsx": $$2,
|
||||
"./islands/FragmentIsland.tsx": $$3,
|
||||
"./islands/Island.tsx": $$4,
|
||||
"./islands/IslandCenter.tsx": $$5,
|
||||
"./islands/IslandConditional.tsx": $$6,
|
||||
"./islands/IslandFn.tsx": $$7,
|
||||
"./islands/IslandInsideIsland.tsx": $$8,
|
||||
"./islands/IslandWithProps.tsx": $$9,
|
||||
"./islands/PassThrough.tsx": $$10,
|
||||
},
|
||||
baseUrl: import.meta.url,
|
||||
};
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
// islands/Dropdown.tsx
|
||||
import { ComponentChildren, createContext } from "preact";
|
||||
import { StateUpdater, useState } from "preact/hooks";
|
||||
|
||||
const DropdownContext = createContext<[boolean, StateUpdater<boolean>]>(
|
||||
[false, () => {}],
|
||||
);
|
||||
|
||||
export default function Dropdown(
|
||||
{ children }: { children: ComponentChildren },
|
||||
) {
|
||||
return (
|
||||
<DropdownContext.Provider value={useState(false)}>
|
||||
{children}
|
||||
</DropdownContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function DropdownHandle(
|
||||
{ children }: { children: ComponentChildren },
|
||||
) {
|
||||
return (
|
||||
<DropdownContext.Consumer>
|
||||
{([isMenuOpen, setIsMenuOpen]) => {
|
||||
return (
|
||||
<button
|
||||
onClick={() => {
|
||||
setIsMenuOpen(!isMenuOpen);
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</button>
|
||||
);
|
||||
}}
|
||||
</DropdownContext.Consumer>
|
||||
);
|
||||
}
|
||||
|
||||
export function DropdownMenu({ children }: { children: ComponentChildren }) {
|
||||
return (
|
||||
<DropdownContext.Consumer>
|
||||
{([isMenuOpen]) => {
|
||||
if (isMenuOpen) {
|
||||
return children;
|
||||
}
|
||||
return null;
|
||||
}}
|
||||
</DropdownContext.Consumer>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import Dropdown, {
|
||||
DropdownHandle,
|
||||
DropdownMenu,
|
||||
} from "../islands/Dropdown.tsx";
|
||||
|
||||
export default function Page() {
|
||||
return (
|
||||
<div>
|
||||
<h1>Dropdown</h1>
|
||||
<Dropdown>
|
||||
<DropdownHandle>Click me!</DropdownHandle>
|
||||
<DropdownMenu>
|
||||
<p class="result">Hello Menu!</p>
|
||||
</DropdownMenu>
|
||||
</Dropdown>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
+20
-9
@@ -10,6 +10,7 @@ import {
|
||||
assertSelector,
|
||||
assertTextMatch,
|
||||
clickWhenListenerReady,
|
||||
parseHtml,
|
||||
waitForText,
|
||||
withFakeServe,
|
||||
withPageName,
|
||||
@@ -308,6 +309,22 @@ Deno.test({
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test("render island inside island with conditional children", async () => {
|
||||
await withPageName(
|
||||
"./tests/fixture_island_nesting/main.ts",
|
||||
async (page, address) => {
|
||||
await page.goto(`${address}/dropdown`);
|
||||
await page.waitForSelector("button");
|
||||
|
||||
const doc = parseHtml(await page.content());
|
||||
assertNotSelector(doc, ".result");
|
||||
|
||||
await page.click("button");
|
||||
await page.waitForSelector(".result");
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name:
|
||||
"render island with JSX children that render another island with JSX children",
|
||||
@@ -357,12 +374,9 @@ Deno.test({
|
||||
await withPageName(
|
||||
"./tests/fixture_island_nesting/main.ts",
|
||||
async (page, address) => {
|
||||
await page.goto(`${address}/island_conditional`, {
|
||||
waitUntil: "networkidle2",
|
||||
});
|
||||
await page.waitForSelector("button");
|
||||
await page.goto(`${address}/island_conditional`);
|
||||
await page.waitForSelector(".island");
|
||||
|
||||
await delay(100);
|
||||
await page.click("button");
|
||||
|
||||
// Button text is matched too, but this allows us
|
||||
@@ -376,14 +390,11 @@ Deno.test({
|
||||
|
||||
Deno.test({
|
||||
name: "serialize inner island props",
|
||||
|
||||
async fn(_t) {
|
||||
await withPageName(
|
||||
"./tests/fixture_island_nesting/main.ts",
|
||||
async (page, address) => {
|
||||
await page.goto(`${address}/island_nested_props`, {
|
||||
waitUntil: "networkidle2",
|
||||
});
|
||||
await page.goto(`${address}/island_nested_props`);
|
||||
await page.waitForSelector(".island");
|
||||
|
||||
await waitForText(page, ".island .island p", "it works");
|
||||
|
||||
Reference in New Issue
Block a user