feat: support conditional island JSX children (#1355)
Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
This commit is contained in:
co-authored by
Asher Gomez
parent
dff80d3a42
commit
11077227b5
@@ -97,7 +97,7 @@ interface Marker {
|
|||||||
// string representing the actual intended comment value which makes
|
// string representing the actual intended comment value which makes
|
||||||
// a bunch of stuff easier.
|
// a bunch of stuff easier.
|
||||||
text: string;
|
text: string;
|
||||||
startNode: Comment;
|
startNode: Comment | null;
|
||||||
endNode: Comment | null;
|
endNode: Comment | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -190,7 +190,7 @@ function _walkInner(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Remove markers
|
// Remove markers
|
||||||
marker.startNode.remove();
|
marker.startNode?.remove();
|
||||||
sib = sib.nextSibling;
|
sib = sib.nextSibling;
|
||||||
marker.endNode.remove();
|
marker.endNode.remove();
|
||||||
continue;
|
continue;
|
||||||
@@ -203,12 +203,44 @@ function _walkInner(
|
|||||||
|
|
||||||
let child: Node | null = marker.startNode;
|
let child: Node | null = marker.startNode;
|
||||||
while (
|
while (
|
||||||
(child = child.nextSibling) !== null && child !== marker.endNode
|
(child = child!.nextSibling) !== null && child !== marker.endNode
|
||||||
) {
|
) {
|
||||||
children.push(child);
|
children.push(child);
|
||||||
}
|
}
|
||||||
|
|
||||||
const vnode = vnodeStack.pop();
|
const vnode = vnodeStack[vnodeStack.length - 1];
|
||||||
|
|
||||||
|
if (vnode.props.children == null) {
|
||||||
|
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: "foo",
|
||||||
|
});
|
||||||
|
|
||||||
|
const node = template.content.cloneNode(true);
|
||||||
|
_walkInner(
|
||||||
|
islands,
|
||||||
|
props,
|
||||||
|
markerStack,
|
||||||
|
vnodeStack,
|
||||||
|
node,
|
||||||
|
);
|
||||||
|
|
||||||
|
markerStack.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
vnodeStack.pop();
|
||||||
|
|
||||||
const parentNode = sib.parentNode! as HTMLElement;
|
const parentNode = sib.parentNode! as HTMLElement;
|
||||||
|
|
||||||
@@ -242,7 +274,7 @@ function _walkInner(
|
|||||||
: setTimeout(_render, 0);
|
: setTimeout(_render, 0);
|
||||||
|
|
||||||
// Remove markers
|
// Remove markers
|
||||||
marker.startNode.remove();
|
marker.startNode?.remove();
|
||||||
sib = sib.nextSibling;
|
sib = sib.nextSibling;
|
||||||
marker.endNode.remove();
|
marker.endNode.remove();
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
+32
-2
@@ -180,6 +180,8 @@ export async function render<Data>(
|
|||||||
|
|
||||||
// Clear the island props
|
// Clear the island props
|
||||||
ISLAND_PROPS = [];
|
ISLAND_PROPS = [];
|
||||||
|
// Clear previous slots
|
||||||
|
SLOTS_TRACKER.clear();
|
||||||
|
|
||||||
const ctx = new RenderContext(
|
const ctx = new RenderContext(
|
||||||
crypto.randomUUID(),
|
crypto.randomUUID(),
|
||||||
@@ -228,6 +230,13 @@ export async function render<Data>(
|
|||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
bodyHtml = renderToString(root);
|
bodyHtml = renderToString(root);
|
||||||
|
|
||||||
|
for (const [id, children] of SLOTS_TRACKER.entries()) {
|
||||||
|
const slotHtml = renderToString(h(Fragment, null, children));
|
||||||
|
const templateId = id.replace(/:/g, "-");
|
||||||
|
bodyHtml += `<template id="${templateId}">${slotHtml}</template>`;
|
||||||
|
}
|
||||||
|
|
||||||
return bodyHtml;
|
return bodyHtml;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -539,6 +548,15 @@ function wrapWithMarker(vnode: ComponentChildren, markerText: string) {
|
|||||||
const ISLANDS: Island[] = [];
|
const ISLANDS: Island[] = [];
|
||||||
const ENCOUNTERED_ISLANDS: Set<Island> = new Set([]);
|
const ENCOUNTERED_ISLANDS: Set<Island> = new Set([]);
|
||||||
let ISLAND_PROPS: unknown[] = [];
|
let ISLAND_PROPS: unknown[] = [];
|
||||||
|
// Track unused slots
|
||||||
|
const SLOTS_TRACKER = new Map<string, ComponentChildren>();
|
||||||
|
function SlotTracker(
|
||||||
|
props: { id: string; children?: ComponentChildren },
|
||||||
|
): VNode {
|
||||||
|
SLOTS_TRACKER.delete(props.id);
|
||||||
|
// deno-lint-ignore no-explicit-any
|
||||||
|
return props.children as any;
|
||||||
|
}
|
||||||
|
|
||||||
// Keep track of which component rendered which vnode. This allows us
|
// Keep track of which component rendered which vnode. This allows us
|
||||||
// to detect when an island is rendered within another instead of being
|
// to detect when an island is rendered within another instead of being
|
||||||
@@ -588,13 +606,25 @@ options.vnode = (vnode) => {
|
|||||||
vnode.type = (props) => {
|
vnode.type = (props) => {
|
||||||
ignoreNext = true;
|
ignoreNext = true;
|
||||||
|
|
||||||
|
const id = ISLAND_PROPS.length;
|
||||||
|
|
||||||
// Only passing children JSX to islands is supported for now
|
// Only passing children JSX to islands is supported for now
|
||||||
if ("children" in props) {
|
if ("children" in props) {
|
||||||
const children = props.children;
|
let children = props.children;
|
||||||
|
const markerText =
|
||||||
|
`frsh-slot-${island.id}:${island.exportName}:${id}:children`;
|
||||||
// @ts-ignore nonono
|
// @ts-ignore nonono
|
||||||
props.children = wrapWithMarker(
|
props.children = wrapWithMarker(
|
||||||
children,
|
children,
|
||||||
`frsh-slot-${island.id}:children`,
|
markerText,
|
||||||
|
);
|
||||||
|
SLOTS_TRACKER.set(markerText, children);
|
||||||
|
children = props.children;
|
||||||
|
// deno-lint-ignore no-explicit-any
|
||||||
|
(props as any).children = h(
|
||||||
|
SlotTracker,
|
||||||
|
{ id: markerText },
|
||||||
|
children,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,49 +4,55 @@
|
|||||||
|
|
||||||
import * as $0 from "./routes/index.tsx";
|
import * as $0 from "./routes/index.tsx";
|
||||||
import * as $1 from "./routes/island_conditional.tsx";
|
import * as $1 from "./routes/island_conditional.tsx";
|
||||||
import * as $2 from "./routes/island_fn_child.tsx";
|
import * as $2 from "./routes/island_conditional_lazy.tsx";
|
||||||
import * as $3 from "./routes/island_in_island.tsx";
|
import * as $3 from "./routes/island_conditional_lazy_island.tsx";
|
||||||
import * as $4 from "./routes/island_in_island_definition.tsx";
|
import * as $4 from "./routes/island_fn_child.tsx";
|
||||||
import * as $5 from "./routes/island_jsx_child.tsx";
|
import * as $5 from "./routes/island_in_island.tsx";
|
||||||
import * as $6 from "./routes/island_jsx_children.tsx";
|
import * as $6 from "./routes/island_in_island_definition.tsx";
|
||||||
import * as $7 from "./routes/island_jsx_island_jsx.tsx";
|
import * as $7 from "./routes/island_jsx_child.tsx";
|
||||||
import * as $8 from "./routes/island_jsx_text.tsx";
|
import * as $8 from "./routes/island_jsx_children.tsx";
|
||||||
import * as $9 from "./routes/island_nested_props.tsx";
|
import * as $9 from "./routes/island_jsx_island_jsx.tsx";
|
||||||
import * as $10 from "./routes/island_order.tsx";
|
import * as $10 from "./routes/island_jsx_text.tsx";
|
||||||
import * as $11 from "./routes/island_siblings.tsx";
|
import * as $11 from "./routes/island_nested_props.tsx";
|
||||||
|
import * as $12 from "./routes/island_order.tsx";
|
||||||
|
import * as $13 from "./routes/island_siblings.tsx";
|
||||||
import * as $$0 from "./islands/BooleanButton.tsx";
|
import * as $$0 from "./islands/BooleanButton.tsx";
|
||||||
import * as $$1 from "./islands/FragmentIsland.tsx";
|
import * as $$1 from "./islands/Counter.tsx";
|
||||||
import * as $$2 from "./islands/Island.tsx";
|
import * as $$2 from "./islands/FragmentIsland.tsx";
|
||||||
import * as $$3 from "./islands/IslandCenter.tsx";
|
import * as $$3 from "./islands/Island.tsx";
|
||||||
import * as $$4 from "./islands/IslandConditional.tsx";
|
import * as $$4 from "./islands/IslandCenter.tsx";
|
||||||
import * as $$5 from "./islands/IslandFn.tsx";
|
import * as $$5 from "./islands/IslandConditional.tsx";
|
||||||
import * as $$6 from "./islands/IslandInsideIsland.tsx";
|
import * as $$6 from "./islands/IslandFn.tsx";
|
||||||
import * as $$7 from "./islands/IslandWithProps.tsx";
|
import * as $$7 from "./islands/IslandInsideIsland.tsx";
|
||||||
|
import * as $$8 from "./islands/IslandWithProps.tsx";
|
||||||
|
|
||||||
const manifest = {
|
const manifest = {
|
||||||
routes: {
|
routes: {
|
||||||
"./routes/index.tsx": $0,
|
"./routes/index.tsx": $0,
|
||||||
"./routes/island_conditional.tsx": $1,
|
"./routes/island_conditional.tsx": $1,
|
||||||
"./routes/island_fn_child.tsx": $2,
|
"./routes/island_conditional_lazy.tsx": $2,
|
||||||
"./routes/island_in_island.tsx": $3,
|
"./routes/island_conditional_lazy_island.tsx": $3,
|
||||||
"./routes/island_in_island_definition.tsx": $4,
|
"./routes/island_fn_child.tsx": $4,
|
||||||
"./routes/island_jsx_child.tsx": $5,
|
"./routes/island_in_island.tsx": $5,
|
||||||
"./routes/island_jsx_children.tsx": $6,
|
"./routes/island_in_island_definition.tsx": $6,
|
||||||
"./routes/island_jsx_island_jsx.tsx": $7,
|
"./routes/island_jsx_child.tsx": $7,
|
||||||
"./routes/island_jsx_text.tsx": $8,
|
"./routes/island_jsx_children.tsx": $8,
|
||||||
"./routes/island_nested_props.tsx": $9,
|
"./routes/island_jsx_island_jsx.tsx": $9,
|
||||||
"./routes/island_order.tsx": $10,
|
"./routes/island_jsx_text.tsx": $10,
|
||||||
"./routes/island_siblings.tsx": $11,
|
"./routes/island_nested_props.tsx": $11,
|
||||||
|
"./routes/island_order.tsx": $12,
|
||||||
|
"./routes/island_siblings.tsx": $13,
|
||||||
},
|
},
|
||||||
islands: {
|
islands: {
|
||||||
"./islands/BooleanButton.tsx": $$0,
|
"./islands/BooleanButton.tsx": $$0,
|
||||||
"./islands/FragmentIsland.tsx": $$1,
|
"./islands/Counter.tsx": $$1,
|
||||||
"./islands/Island.tsx": $$2,
|
"./islands/FragmentIsland.tsx": $$2,
|
||||||
"./islands/IslandCenter.tsx": $$3,
|
"./islands/Island.tsx": $$3,
|
||||||
"./islands/IslandConditional.tsx": $$4,
|
"./islands/IslandCenter.tsx": $$4,
|
||||||
"./islands/IslandFn.tsx": $$5,
|
"./islands/IslandConditional.tsx": $$5,
|
||||||
"./islands/IslandInsideIsland.tsx": $$6,
|
"./islands/IslandFn.tsx": $$6,
|
||||||
"./islands/IslandWithProps.tsx": $$7,
|
"./islands/IslandInsideIsland.tsx": $$7,
|
||||||
|
"./islands/IslandWithProps.tsx": $$8,
|
||||||
},
|
},
|
||||||
baseUrl: import.meta.url,
|
baseUrl: import.meta.url,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
import { Signal } from "@preact/signals";
|
||||||
|
|
||||||
|
export default function Counter({ count }: { count: Signal<number> }) {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<p class="count">{count}</p>
|
||||||
|
<button class="counter" onClick={() => count.value++}>update</button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,9 +1,17 @@
|
|||||||
import { Signal } from "@preact/signals";
|
import { Signal } from "@preact/signals";
|
||||||
|
import { ComponentChildren } from "preact";
|
||||||
|
|
||||||
export interface IslandConditionalProps {
|
export interface IslandConditionalProps {
|
||||||
show: Signal<boolean>;
|
show: Signal<boolean>;
|
||||||
|
children?: ComponentChildren;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function IslandConditional({ show }: IslandConditionalProps) {
|
export default function IslandConditional(
|
||||||
return show.value ? <>it works</> : null;
|
{ show, children }: IslandConditionalProps,
|
||||||
|
) {
|
||||||
|
return (
|
||||||
|
<div class="island">
|
||||||
|
{show.value ? <p>island content</p> : <>{children}</>}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
import IslandConditional from "../islands/IslandConditional.tsx";
|
||||||
|
import BooleanButton from "../islands/BooleanButton.tsx";
|
||||||
|
import { signal } from "@preact/signals";
|
||||||
|
|
||||||
|
const show = signal(true);
|
||||||
|
|
||||||
|
export default function Page() {
|
||||||
|
return (
|
||||||
|
<div id="page">
|
||||||
|
<IslandConditional show={show}>
|
||||||
|
<p>server rendered</p>
|
||||||
|
</IslandConditional>
|
||||||
|
<BooleanButton signal={show} />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
import IslandConditional from "../islands/IslandConditional.tsx";
|
||||||
|
import BooleanButton from "../islands/BooleanButton.tsx";
|
||||||
|
import { useSignal } from "@preact/signals";
|
||||||
|
import Counter from "../islands/Counter.tsx";
|
||||||
|
|
||||||
|
export default function Page() {
|
||||||
|
const show = useSignal(true);
|
||||||
|
const count = useSignal(0);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div id="page">
|
||||||
|
<IslandConditional show={show}>
|
||||||
|
<div>
|
||||||
|
<p class="server">server rendered</p>
|
||||||
|
<Counter count={count} />
|
||||||
|
</div>
|
||||||
|
</IslandConditional>
|
||||||
|
<BooleanButton signal={show} />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
+39
-6
@@ -406,14 +406,10 @@ Deno.test({
|
|||||||
await delay(100);
|
await delay(100);
|
||||||
await page.click("button");
|
await page.click("button");
|
||||||
|
|
||||||
const text = await page.$eval(
|
|
||||||
"#page",
|
|
||||||
(el) => el.textContent,
|
|
||||||
);
|
|
||||||
// Button text is matched too, but this allows us
|
// Button text is matched too, but this allows us
|
||||||
// to assert correct ordering. The "it works" should
|
// to assert correct ordering. The "island content" should
|
||||||
// be left of "Toggle"
|
// be left of "Toggle"
|
||||||
assertEquals(text, "it worksToggle");
|
await waitForText(page, "#page", "island contentToggle");
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
@@ -478,3 +474,40 @@ Deno.test({
|
|||||||
sanitizeOps: false,
|
sanitizeOps: false,
|
||||||
sanitizeResources: false,
|
sanitizeResources: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test({
|
||||||
|
name: "render nested islands with server children conditionally",
|
||||||
|
|
||||||
|
async fn() {
|
||||||
|
await withPageName(
|
||||||
|
"./tests/fixture_island_nesting/main.ts",
|
||||||
|
async (page, address) => {
|
||||||
|
await page.goto(`${address}/island_conditional_lazy`);
|
||||||
|
await waitForText(page, ".island p", "island content");
|
||||||
|
|
||||||
|
await page.click("button");
|
||||||
|
await waitForText(page, ".island p", "server rendered");
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test({
|
||||||
|
name: "revive island in lazy server rendered children conditionally",
|
||||||
|
|
||||||
|
async fn() {
|
||||||
|
await withPageName(
|
||||||
|
"./tests/fixture_island_nesting/main.ts",
|
||||||
|
async (page, address) => {
|
||||||
|
await page.goto(`${address}/island_conditional_lazy_island`);
|
||||||
|
await waitForText(page, ".island p", "island content");
|
||||||
|
|
||||||
|
await page.click("button");
|
||||||
|
await waitForText(page, ".island .server", "server rendered");
|
||||||
|
|
||||||
|
await page.click("button.counter");
|
||||||
|
await waitForText(page, ".island .count", "1");
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user