feat: allow passing JSX children to island + nested islands (#1285)
This commit is contained in:
+264
-39
@@ -1,4 +1,12 @@
|
||||
import { ComponentType, h, options, render } from "preact";
|
||||
import {
|
||||
ComponentChildren,
|
||||
ComponentType,
|
||||
Fragment,
|
||||
h,
|
||||
options,
|
||||
render,
|
||||
VNode,
|
||||
} from "preact";
|
||||
import { assetHashingHook } from "../utils.ts";
|
||||
|
||||
function createRootFragment(
|
||||
@@ -24,49 +32,266 @@ function createRootFragment(
|
||||
};
|
||||
}
|
||||
|
||||
function isCommentNode(node: Node): node is Comment {
|
||||
return node.nodeType === Node.COMMENT_NODE;
|
||||
}
|
||||
function isTextNode(node: Node): node is Text {
|
||||
return node.nodeType === Node.TEXT_NODE;
|
||||
}
|
||||
function isElementNode(node: Node): node is HTMLElement {
|
||||
return node.nodeType === Node.ELEMENT_NODE;
|
||||
}
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
export function revive(islands: Record<string, ComponentType>, props: any[]) {
|
||||
async function walk(node: Node | null) {
|
||||
const tag = node!.nodeType === 8 &&
|
||||
((node as Comment).data.match(/^\s*frsh-(.*)\s*$/) || [])[1];
|
||||
let endNode: Node | null = null;
|
||||
if (tag) {
|
||||
const startNode = node!;
|
||||
const children = [];
|
||||
const parent = node!.parentNode;
|
||||
// collect all children of the island
|
||||
while ((node = node!.nextSibling) && node.nodeType !== 8) {
|
||||
children.push(node);
|
||||
}
|
||||
startNode.parentNode!.removeChild(startNode); // remove start tag node
|
||||
_walkInner(
|
||||
islands,
|
||||
props,
|
||||
// markerstack
|
||||
[],
|
||||
// Keep a root node in the vnode stack to save a couple of checks
|
||||
// later during iteration
|
||||
[h(Fragment, null)],
|
||||
document.body,
|
||||
);
|
||||
}
|
||||
|
||||
const [id, n] = tag.split(":");
|
||||
const _render = () => {
|
||||
render(
|
||||
h(islands[id], props[Number(n)]),
|
||||
createRootFragment(
|
||||
parent! as HTMLElement,
|
||||
children,
|
||||
// deno-lint-ignore no-explicit-any
|
||||
) as any as HTMLElement,
|
||||
);
|
||||
};
|
||||
"scheduler" in window
|
||||
? await scheduler!.postTask(_render)
|
||||
: setTimeout(_render, 0);
|
||||
endNode = node;
|
||||
function ServerComponent(
|
||||
props: { children: ComponentChildren },
|
||||
): ComponentChildren {
|
||||
return props.children;
|
||||
}
|
||||
ServerComponent.displayName = "PreactServerComponent";
|
||||
|
||||
function addPropsChild(parent: VNode, vnode: ComponentChildren) {
|
||||
const props = parent.props;
|
||||
if (props.children === null) {
|
||||
props.children = vnode;
|
||||
} else {
|
||||
if (!Array.isArray(props.children)) {
|
||||
props.children = [props.children, vnode];
|
||||
} else {
|
||||
props.children.push(vnode);
|
||||
}
|
||||
|
||||
const sib = node!.nextSibling;
|
||||
const fc = node!.firstChild;
|
||||
if (endNode) {
|
||||
endNode.parentNode?.removeChild(endNode); // remove end tag node
|
||||
}
|
||||
|
||||
if (sib) walk(sib);
|
||||
if (fc) walk(fc);
|
||||
}
|
||||
walk(document.body);
|
||||
}
|
||||
|
||||
const enum MarkerKind {
|
||||
Island,
|
||||
Slot,
|
||||
}
|
||||
|
||||
interface Marker {
|
||||
kind: MarkerKind;
|
||||
// We can remove this once we drop support for RTS <6.1.0 where
|
||||
// we rendered incorrect comments leading to `!--` and `--` being
|
||||
// included in the comment text. Therefore this is a normalized
|
||||
// string representing the actual intended comment value which makes
|
||||
// a bunch of stuff easier.
|
||||
text: string;
|
||||
startNode: Comment;
|
||||
endNode: Comment | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Revive islands and stich together any server rendered content.
|
||||
*
|
||||
* Conceptually we're doing an inorder depth first search over the DOM
|
||||
* to find all our comment nodes `<!--frsh-something-->` which act as
|
||||
* a marker for islands or server rendered JSX (=slots in islands).
|
||||
* Every island or server JSX has a start and an end marker, which
|
||||
* means there is no _single_ root nodes for these elements.
|
||||
* The hierarchy we need to construct for the virtual-dom tree might
|
||||
* be rendered in a flattened manner in the DOM.
|
||||
*
|
||||
* Example:
|
||||
* <div>
|
||||
* <!--frsh-island:0-->
|
||||
* <!--frsh-slot:children-->
|
||||
* <p>server content</p>
|
||||
* <!--/frsh-slot:children-->
|
||||
* <!--/frsh-island:0-->
|
||||
* </div>
|
||||
*
|
||||
* Here we have a flat DOM structure, but from the virtual-dom
|
||||
* perspective we should render:
|
||||
* <div> -> <Island> -> ServerComponent -> <p>server content</p>
|
||||
*
|
||||
* To solve this we're keeping track of the virtual-dom hierarchy
|
||||
* in a stack-like manner, but do the actual iteration in a list-based
|
||||
* fashion over an HTMLElement's children list.
|
||||
*/
|
||||
function _walkInner(
|
||||
islands: Record<string, ComponentType>,
|
||||
// deno-lint-ignore no-explicit-any
|
||||
props: any[],
|
||||
markerStack: Marker[],
|
||||
vnodeStack: VNode[],
|
||||
node: Node | Comment,
|
||||
) {
|
||||
let sib: Node | null = node;
|
||||
while (sib !== null) {
|
||||
const marker = markerStack.length > 0
|
||||
? markerStack[markerStack.length - 1]
|
||||
: null;
|
||||
|
||||
// We use comment nodes to mark fresh islands and slots
|
||||
if (isCommentNode(sib)) {
|
||||
let comment = sib.data;
|
||||
if (comment.startsWith("!--")) {
|
||||
comment = comment.slice(3, -2);
|
||||
}
|
||||
|
||||
if (comment.startsWith("frsh-slot")) {
|
||||
// Note: Nested slots are not possible as they're flattened
|
||||
// already on the server.
|
||||
markerStack.push({
|
||||
startNode: sib,
|
||||
text: comment,
|
||||
endNode: null,
|
||||
kind: MarkerKind.Slot,
|
||||
});
|
||||
// @ts-ignore TS gets confused
|
||||
vnodeStack.push(h(ServerComponent, { key: comment }));
|
||||
} else if (
|
||||
marker !== null && (
|
||||
comment.startsWith("/frsh") ||
|
||||
// Check for old Preact RTS
|
||||
marker.text === comment
|
||||
)
|
||||
) {
|
||||
// We're closing either a slot or an island
|
||||
marker.endNode = sib;
|
||||
|
||||
markerStack.pop();
|
||||
const parent = markerStack.length > 0
|
||||
? markerStack[markerStack.length - 1]
|
||||
: null;
|
||||
|
||||
if (marker.kind === MarkerKind.Slot) {
|
||||
// If we're closing a slot than it's assumed that we're
|
||||
// inside an island
|
||||
if (parent?.kind === MarkerKind.Island) {
|
||||
const vnode = vnodeStack.pop();
|
||||
|
||||
// For now only `props.children` is supported.
|
||||
const islandParent = vnodeStack[vnodeStack.length - 1]!;
|
||||
// Overwrite serialized `{__slot: "children"}` with the
|
||||
// actual vnode child.
|
||||
islandParent.props.children = vnode;
|
||||
}
|
||||
|
||||
// Remove markers
|
||||
marker.startNode.remove();
|
||||
sib = sib.nextSibling;
|
||||
marker.endNode.remove();
|
||||
continue;
|
||||
} else if (marker.kind === MarkerKind.Island) {
|
||||
// We're ready to revive this island if it has
|
||||
// no roots of its own. Otherwise we'll treat it
|
||||
// as a standard component
|
||||
if (markerStack.length === 0) {
|
||||
const children: Node[] = [];
|
||||
|
||||
let child: Node | null = marker.startNode;
|
||||
while (
|
||||
(child = child.nextSibling) !== null && child !== marker.endNode
|
||||
) {
|
||||
children.push(child);
|
||||
}
|
||||
|
||||
const vnode = vnodeStack.pop();
|
||||
|
||||
const parentNode = sib.parentNode! as HTMLElement;
|
||||
const _render = () =>
|
||||
render(
|
||||
vnode,
|
||||
createRootFragment(
|
||||
parentNode,
|
||||
children,
|
||||
// deno-lint-ignore no-explicit-any
|
||||
) as any as HTMLElement,
|
||||
);
|
||||
|
||||
"scheduler" in window
|
||||
// `scheduler.postTask` is async but that can easily
|
||||
// fire in the background. We don't want waiting for
|
||||
// the hydration of an island block us.
|
||||
// @ts-ignore scheduler API is not in types yet
|
||||
? scheduler!.postTask(_render)
|
||||
: setTimeout(_render, 0);
|
||||
|
||||
// Remove markers
|
||||
marker.startNode.remove();
|
||||
sib = sib.nextSibling;
|
||||
marker.endNode.remove();
|
||||
continue;
|
||||
} 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 parent = vnodeStack[vnodeStack.length - 1]!;
|
||||
addPropsChild(parent, vnode);
|
||||
}
|
||||
}
|
||||
} else if (comment.startsWith("frsh")) {
|
||||
// We're opening a new island
|
||||
const [id, n] = comment.slice(5).split(":");
|
||||
const islandProps = props[Number(n)];
|
||||
|
||||
markerStack.push({
|
||||
startNode: sib,
|
||||
endNode: null,
|
||||
text: comment,
|
||||
kind: MarkerKind.Island,
|
||||
});
|
||||
const vnode = h(islands[id], islandProps);
|
||||
vnodeStack.push(vnode);
|
||||
}
|
||||
} else if (isTextNode(sib)) {
|
||||
const parentVNode = vnodeStack[vnodeStack.length - 1]!;
|
||||
if (
|
||||
marker !== null && marker.kind === MarkerKind.Slot
|
||||
) {
|
||||
addPropsChild(parentVNode, sib.data);
|
||||
}
|
||||
} else {
|
||||
const parentVNode = vnodeStack[vnodeStack.length - 1];
|
||||
if (
|
||||
marker !== null &&
|
||||
marker.kind === MarkerKind.Slot && isElementNode(sib)
|
||||
) {
|
||||
// Parse the server rendered DOM into vnodes that we can
|
||||
// attach to the virtual-dom tree. In the future, once
|
||||
// Preact supports a way to skip over subtrees, this
|
||||
// can be dropped.
|
||||
const childLen = sib.childNodes.length;
|
||||
const props: Record<string, unknown> = {
|
||||
children: childLen <= 1 ? null : [],
|
||||
};
|
||||
for (let i = 0; i < sib.attributes.length; i++) {
|
||||
const attr = sib.attributes[i];
|
||||
props[attr.nodeName] = attr.nodeValue;
|
||||
}
|
||||
const vnode = h(sib.localName, props);
|
||||
addPropsChild(parentVNode, vnode);
|
||||
vnodeStack.push(vnode);
|
||||
}
|
||||
|
||||
// TODO: What about script tags?
|
||||
if (
|
||||
sib.firstChild && (sib.nodeName !== "SCRIPT")
|
||||
) {
|
||||
_walkInner(islands, props, markerStack, vnodeStack, sib.firstChild);
|
||||
}
|
||||
|
||||
if (marker !== null && marker.kind === MarkerKind.Slot) {
|
||||
vnodeStack.pop();
|
||||
}
|
||||
}
|
||||
|
||||
sib = sib.nextSibling;
|
||||
}
|
||||
}
|
||||
|
||||
const originalHook = options.vnode;
|
||||
|
||||
+40
-22
@@ -420,6 +420,31 @@ if (!supportsUnstableComments) {
|
||||
);
|
||||
}
|
||||
|
||||
function wrapWithMarker(vnode: ComponentChildren, markerText: string) {
|
||||
// Newer versions of preact-render-to-string allow you to render comments
|
||||
if (supportsUnstableComments) {
|
||||
return h(
|
||||
Fragment,
|
||||
null,
|
||||
h(Fragment, {
|
||||
// @ts-ignore unstable property is not typed
|
||||
UNSTABLE_comment: markerText,
|
||||
}),
|
||||
vnode,
|
||||
h(Fragment, {
|
||||
// @ts-ignore unstable property is not typed
|
||||
UNSTABLE_comment: "/" + markerText,
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
return h(
|
||||
`!--${markerText}--`,
|
||||
null,
|
||||
vnode,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Set up a preact option hook to track when vnode with custom functions are
|
||||
// created.
|
||||
const ISLANDS: Island[] = [];
|
||||
@@ -440,31 +465,24 @@ options.vnode = (vnode) => {
|
||||
ENCOUNTERED_ISLANDS.add(island);
|
||||
vnode.type = (props) => {
|
||||
ignoreNext = true;
|
||||
|
||||
// Only passing children JSX to islands is supported for now
|
||||
if ("children" in props) {
|
||||
const children = props.children;
|
||||
// @ts-ignore nonono
|
||||
props.children = wrapWithMarker(
|
||||
children,
|
||||
`frsh-slot-${island.id}:children`,
|
||||
);
|
||||
}
|
||||
|
||||
const child = h(originalType, props);
|
||||
ISLAND_PROPS.push(props);
|
||||
|
||||
// Newer versions of preact-render-to-string allow you to render comments
|
||||
if (supportsUnstableComments) {
|
||||
return h(
|
||||
Fragment,
|
||||
null,
|
||||
h(Fragment, {
|
||||
// @ts-ignore unstable property is not typed
|
||||
UNSTABLE_comment: `frsh-${island.id}:${ISLAND_PROPS.length - 1}`,
|
||||
}),
|
||||
child,
|
||||
h(Fragment, {
|
||||
// @ts-ignore unstable property is not typed
|
||||
UNSTABLE_comment: `/frsh-${island.id}:${ISLAND_PROPS.length - 1}`,
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
return h(
|
||||
`!--frsh-${island.id}:${ISLAND_PROPS.length - 1}--`,
|
||||
null,
|
||||
child,
|
||||
);
|
||||
}
|
||||
return wrapWithMarker(
|
||||
child,
|
||||
`frsh-${island.id}:${ISLAND_PROPS.length - 1}`,
|
||||
);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
*
|
||||
* The corresponding deserializer is in `src/runtime/deserializer.ts`.
|
||||
*/
|
||||
import { isValidElement, VNode } from "preact";
|
||||
import { KEY } from "../runtime/deserializer.ts";
|
||||
|
||||
interface SerializeResult {
|
||||
@@ -44,6 +45,13 @@ function isSignal(x: any): x is Signal {
|
||||
);
|
||||
}
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
function isVNode(x: any): x is VNode {
|
||||
return x !== null && typeof x === "object" && "type" in x && "ref" in x &&
|
||||
"__k" in x &&
|
||||
isValidElement(x);
|
||||
}
|
||||
|
||||
export function serialize(data: unknown): SerializeResult {
|
||||
let requiresDeserializer = false;
|
||||
let hasSignals = false;
|
||||
@@ -108,6 +116,13 @@ export function serialize(data: unknown): SerializeResult {
|
||||
referenceArr.push(currentPath);
|
||||
}
|
||||
return 0;
|
||||
} else if (isVNode(value)) {
|
||||
requiresDeserializer = true;
|
||||
// No need to serialize JSX as we pick that up from
|
||||
// the rendered HTML in the browser.
|
||||
const res = null;
|
||||
parentStack.push(res);
|
||||
return res;
|
||||
} else {
|
||||
seen.set(value, currentPath);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user