import "../polyfills.ts"; import { Component, ComponentChildren, ComponentType, Fragment, h, options, render, VNode, } from "preact"; import { assetHashingHook } from "../utils.ts"; import { type SerializedState } from "../../server/rendering/fresh_tags.tsx"; import type { Signal } from "@preact/signals"; import { CLIENT_NAV_ATTR, DATA_ANCESTOR, DATA_CURRENT, DATA_KEY_ATTR, LOADING_ATTR, PARTIAL_ATTR, PARTIAL_SEARCH_PARAM, PartialMode, } from "../../constants.ts"; import { matchesUrl, setActiveUrl, UrlMatchKind } from "../active_url.ts"; function createRootFragment( parent: Element, startMarker: Text | Comment, // 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. endMarker: Text | Comment, ) { // @ts-ignore this is fine return parent.__k = { _frshRootFrag: true, nodeType: 1, parentNode: parent, nextSibling: null, get firstChild() { const child = startMarker.nextSibling; if (child === endMarker) return null; return child; }, get childNodes() { const children: ChildNode[] = []; let child = startMarker.nextSibling; while (child !== null && child !== endMarker) { children.push(child); child = child.nextSibling; } return children; }, insertBefore(node: Node, child: Node | null) { parent.insertBefore(node, child ?? endMarker); }, appendChild(child: Node) { // 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); }, }; } 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 && !("_frshRootFrag" in node); } type IslandRegistry = Record>; export function revive( islands: IslandRegistry, // deno-lint-ignore no-explicit-any props: any[], ) { const result: RenderRequest[] = []; _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, result, ); for (let i = 0; i < result.length; i++) { const { vnode, marker, rootFragment } = result[i]; const _render = () => { render( vnode, rootFragment, ); if (marker.kind === MarkerKind.Partial) { // deno-lint-ignore no-explicit-any partials.set(marker.text, (vnode as any).__c); } }; "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); } } function ServerComponent( props: { children: ComponentChildren; id: string }, ): 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); } } } class PartialComp extends Component< { children?: ComponentChildren; mode: number; name: string } > { render() { return this.props.children; } } const enum MarkerKind { Island, Slot, Partial, } interface Marker { kind: MarkerKind; text: string; startNode: Text | Comment | null; endNode: Text | Comment | null; } export interface RenderRequest { vnode: VNode; marker: Marker; rootFragment: HTMLElement; } // Useful for debugging const SHOW_MARKERS = false; const partials = new Map(); /** * Replace comment markers with empty text nodes to hide them * in DevTools. This is done to avoid user confusion. */ function hideMarker(marker: Marker) { const { startNode, endNode } = marker; const parent = endNode!.parentNode!; if ( !SHOW_MARKERS && startNode !== null && startNode.nodeType === Node.COMMENT_NODE ) { const text = new Text(""); marker.startNode = text; parent.insertBefore(text, startNode); startNode.remove(); } if ( !SHOW_MARKERS && endNode !== null && endNode.nodeType === Node.COMMENT_NODE ) { const text = new Text(""); marker.endNode = text; parent.insertBefore(text, endNode); endNode.remove(); } } /** * 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 `