@praxisjs/runtime 0.5.4 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,360 @@
1
+ // @vitest-environment jsdom
2
+ import { describe, it, expect, vi, afterEach } from "vitest";
3
+
4
+ import { StatefulComponent } from "@praxisjs/core";
5
+ import { isServerRenderPass, setServerRenderPass, signal } from "@praxisjs/core/internal";
6
+
7
+ import { mountComponent } from "../component";
8
+ import { getCurrentScope } from "../context";
9
+ import { mountElement } from "../element";
10
+ import { reconcile } from "../hydration";
11
+ import { registerReactiveNode } from "../hydration-context";
12
+ import { render, Portal } from "../index";
13
+ import { Scope } from "../scope";
14
+
15
+ const SSG_MARKER = "data-praxis-ssg";
16
+
17
+ /** Renders `factory` into a throwaway container and returns the resulting markup, simulating SSR output. */
18
+ function ssrHTML(factory: () => Node | Node[] | null): string {
19
+ const serverContainer = document.createElement("div");
20
+ const unmount = render(factory, serverContainer);
21
+ const html = serverContainer.innerHTML;
22
+ unmount();
23
+ return html;
24
+ }
25
+
26
+ function hydratable(factory: () => Node | Node[] | null): HTMLElement {
27
+ const container = document.createElement("div");
28
+ container.innerHTML = ssrHTML(factory);
29
+ container.setAttribute(SSG_MARKER, "1");
30
+ document.body.appendChild(container);
31
+ return container;
32
+ }
33
+
34
+ afterEach(() => { setServerRenderPass(false); });
35
+
36
+ describe("render() — hydration (build + reconcile)", () => {
37
+ it("adopts a flat element instead of recreating it", () => {
38
+ const factory = () => mountElement("p", { id: "greet", children: "hello" }, new Scope());
39
+ const container = hydratable(factory);
40
+ const serverEl = container.querySelector("#greet");
41
+ expect(serverEl).not.toBeNull();
42
+
43
+ render(factory, container);
44
+
45
+ expect(container.querySelector("#greet")).toBe(serverEl);
46
+ document.body.removeChild(container);
47
+ });
48
+
49
+ it("adopts nested elements at every level — the case that broke the ambient-cursor attempt", () => {
50
+ const factory = () => mountElement("div", {
51
+ id: "outer",
52
+ children: mountElement("span", { id: "inner", children: "text" }, new Scope()),
53
+ }, new Scope());
54
+ const container = hydratable(factory);
55
+ const outerBefore = container.querySelector("#outer");
56
+ const innerBefore = container.querySelector("#inner");
57
+
58
+ render(factory, container);
59
+
60
+ expect(container.querySelector("#outer")).toBe(outerBefore);
61
+ expect(container.querySelector("#inner")).toBe(innerBefore);
62
+ expect(container.textContent).toBe("text");
63
+ document.body.removeChild(container);
64
+ });
65
+
66
+ it("adopts three levels deep with siblings at each level", () => {
67
+ const factory = () => mountElement("ul", {
68
+ id: "list",
69
+ children: [
70
+ mountElement("li", { id: "a", children: mountElement("b", { children: "1" }, new Scope()) }, new Scope()),
71
+ mountElement("li", { id: "b", children: "2" }, new Scope()),
72
+ ],
73
+ }, new Scope());
74
+ const container = hydratable(factory);
75
+ const before = {
76
+ list: container.querySelector("#list"),
77
+ a: container.querySelector("#a"),
78
+ b: container.querySelector("#b"),
79
+ bold: container.querySelector("b"),
80
+ };
81
+
82
+ render(factory, container);
83
+
84
+ expect(container.querySelector("#list")).toBe(before.list);
85
+ expect(container.querySelector("#a")).toBe(before.a);
86
+ expect(container.querySelector("#b")).toBe(before.b);
87
+ expect(container.querySelector("b")).toBe(before.bold);
88
+ document.body.removeChild(container);
89
+ });
90
+
91
+ it("wires event listeners onto the adopted (real) element via replay", () => {
92
+ let clicks = 0;
93
+ const factory = () => mountElement("div", {
94
+ children: mountElement("button", { onClick: () => { clicks++; }, children: "Click" }, new Scope()),
95
+ }, new Scope());
96
+ const container = hydratable(factory);
97
+ const buttonBefore = container.querySelector("button");
98
+
99
+ render(factory, container);
100
+
101
+ expect(container.querySelector("button")).toBe(buttonBefore);
102
+ container.querySelector("button")?.dispatchEvent(new MouseEvent("click", { bubbles: true }));
103
+ expect(clicks).toBe(1);
104
+ document.body.removeChild(container);
105
+ });
106
+
107
+ it("hydrates reactive text and keeps reacting to signal changes afterwards", () => {
108
+ const text = signal("initial");
109
+ const factory = () => mountElement("span", { children: () => text() }, new Scope());
110
+ const container = hydratable(factory);
111
+ expect(container.textContent).toBe("initial");
112
+
113
+ render(factory, container);
114
+
115
+ text.set("updated");
116
+ expect(container.textContent).toBe("updated");
117
+ text.set("initial");
118
+ document.body.removeChild(container);
119
+ });
120
+
121
+ it("hydrates a reactive list of elements, keeping real nodes, and stays correct after a later update", () => {
122
+ const items = signal(["a", "b", "c"]);
123
+ const factory = () => mountElement("ul", {
124
+ id: "list",
125
+ children: () => items().map((item) => mountElement("li", { children: item }, new Scope())),
126
+ }, new Scope());
127
+ const container = hydratable(factory);
128
+ const list = container.querySelector("#list") as HTMLElement;
129
+ const firstLiBefore = list.querySelector("li");
130
+ expect(container.textContent).toBe("abc");
131
+
132
+ render(factory, container);
133
+
134
+ expect(container.querySelector("#list")?.querySelector("li")).toBe(firstLiBefore);
135
+ expect(container.textContent).toBe("abc");
136
+
137
+ // A subsequent reactive update must diff correctly — this only works if
138
+ // reconcile() rewrote mountReactive's `currentNodes` bookkeeping in place
139
+ // when it kept the real <li> elements instead of the fresh ones.
140
+ items.set(["x", "y"]);
141
+ expect(container.textContent).toBe("xy");
142
+ items.set(["z"]);
143
+ expect(container.textContent).toBe("z");
144
+ document.body.removeChild(container);
145
+ });
146
+
147
+ it("hydrates a component nested inside another component, firing onMount/onUnmount correctly", async () => {
148
+ class Inner extends StatefulComponent {
149
+ static __isComponent = true as const;
150
+ static __isStateless = false;
151
+ onMount() {}
152
+ onUnmount() {}
153
+ render() { return mountElement("em", { children: "inner" }, getCurrentScope()); }
154
+ }
155
+ class Outer extends StatefulComponent {
156
+ static __isComponent = true as const;
157
+ static __isStateless = false;
158
+ onMount() {}
159
+ render() {
160
+ return mountElement("section", {
161
+ children: mountComponent(Inner, {}, getCurrentScope()),
162
+ }, getCurrentScope());
163
+ }
164
+ }
165
+
166
+ const factory = () => mountComponent(Outer, {}, getCurrentScope());
167
+ const container = hydratable(factory);
168
+ const emBefore = container.querySelector("em");
169
+
170
+ const outerMount = vi.spyOn(Outer.prototype, "onMount");
171
+ const innerMount = vi.spyOn(Inner.prototype, "onMount");
172
+ const innerUnmount = vi.spyOn(Inner.prototype, "onUnmount");
173
+ const dispose = render(factory, container);
174
+ await Promise.resolve();
175
+
176
+ expect(outerMount).toHaveBeenCalledOnce();
177
+ expect(innerMount).toHaveBeenCalledOnce();
178
+ expect(container.querySelector("em")).toBe(emBefore);
179
+ expect(container.textContent).toBe("inner");
180
+
181
+ dispose();
182
+ expect(innerUnmount).toHaveBeenCalledOnce();
183
+
184
+ outerMount.mockRestore();
185
+ innerMount.mockRestore();
186
+ innerUnmount.mockRestore();
187
+ document.body.removeChild(container);
188
+ });
189
+
190
+ it("a tag mismatch at one node replaces only that node — siblings stay adopted", () => {
191
+ const factory = () => mountElement("div", {
192
+ id: "wrap",
193
+ children: [
194
+ mountElement("p", { id: "before", children: "before" }, new Scope()),
195
+ mountElement("span", { id: "target", children: "new" }, new Scope()),
196
+ mountElement("p", { id: "after", children: "after" }, new Scope()),
197
+ ],
198
+ }, new Scope());
199
+
200
+ const container = document.createElement("div");
201
+ container.innerHTML =
202
+ '<div id="wrap"><p id="before">before</p><strong id="target">old</strong><p id="after">after</p></div>';
203
+ container.setAttribute(SSG_MARKER, "1");
204
+ document.body.appendChild(container);
205
+
206
+ const beforeEl = container.querySelector("#before");
207
+ const afterEl = container.querySelector("#after");
208
+
209
+ render(factory, container);
210
+
211
+ expect(container.querySelector("#before")).toBe(beforeEl);
212
+ expect(container.querySelector("#after")).toBe(afterEl);
213
+ expect(container.querySelector("#target")?.tagName.toLowerCase()).toBe("span");
214
+ expect(container.textContent).toBe("beforenewafter");
215
+ document.body.removeChild(container);
216
+ });
217
+
218
+ it("appends extra fresh nodes when the real DOM has fewer children than the fresh tree", () => {
219
+ // Client factory produces three items; the server only rendered two —
220
+ // exercises reconcile()'s mismatch branch once realCursor has run out.
221
+ const factory = () => mountElement("ul", {
222
+ id: "list",
223
+ children: [
224
+ mountElement("li", { id: "a", children: "a" }, new Scope()),
225
+ mountElement("li", { id: "b", children: "b" }, new Scope()),
226
+ mountElement("li", { id: "c", children: "c" }, new Scope()),
227
+ ],
228
+ }, new Scope());
229
+
230
+ const container = document.createElement("div");
231
+ container.innerHTML = '<ul id="list"><li id="a">a</li><li id="b">b</li></ul>';
232
+ container.setAttribute(SSG_MARKER, "1");
233
+ document.body.appendChild(container);
234
+
235
+ const aBefore = container.querySelector("#a");
236
+ const bBefore = container.querySelector("#b");
237
+
238
+ render(factory, container);
239
+
240
+ expect(container.querySelector("#a")).toBe(aBefore);
241
+ expect(container.querySelector("#b")).toBe(bBefore);
242
+ expect(container.querySelector("#c")?.textContent).toBe("c");
243
+ expect(container.querySelectorAll("#list > li")).toHaveLength(3);
244
+ document.body.removeChild(container);
245
+ });
246
+
247
+ it("keeps the real element even when its fresh counterpart bypassed mountElement (nothing recorded to replay)", () => {
248
+ class RawComp extends StatefulComponent {
249
+ static __isComponent = true as const;
250
+ static __isStateless = false;
251
+ render() {
252
+ const el = document.createElement("mark");
253
+ el.id = "raw";
254
+ el.textContent = "raw content";
255
+ return el;
256
+ }
257
+ }
258
+
259
+ const factory = () => mountComponent(RawComp, {}, getCurrentScope());
260
+ const container = hydratable(factory);
261
+ const markBefore = container.querySelector("#raw");
262
+
263
+ render(factory, container);
264
+
265
+ expect(container.querySelector("#raw")).toBe(markBefore);
266
+ expect(container.textContent).toBe("raw content");
267
+ document.body.removeChild(container);
268
+ });
269
+
270
+ it("removes trailing real nodes the fresh tree no longer produces", () => {
271
+ // Server rendered three items; the client factory only produces two —
272
+ // exercises reconcile()'s cleanup loop for leftover real siblings past
273
+ // the end of freshChildren.
274
+ const factory = () => mountElement("ul", {
275
+ id: "list",
276
+ children: [
277
+ mountElement("li", { id: "a", children: "a" }, new Scope()),
278
+ mountElement("li", { id: "b", children: "b" }, new Scope()),
279
+ ],
280
+ }, new Scope());
281
+
282
+ const container = document.createElement("div");
283
+ container.innerHTML =
284
+ '<ul id="list"><li id="a">a</li><li id="b">b</li><li id="c">c</li></ul>';
285
+ container.setAttribute(SSG_MARKER, "1");
286
+ document.body.appendChild(container);
287
+
288
+ const aBefore = container.querySelector("#a");
289
+ const bBefore = container.querySelector("#b");
290
+
291
+ render(factory, container);
292
+
293
+ expect(container.querySelector("#a")).toBe(aBefore);
294
+ expect(container.querySelector("#b")).toBe(bBefore);
295
+ expect(container.querySelector("#c")).toBeNull();
296
+ expect(container.querySelectorAll("#list > li")).toHaveLength(2);
297
+ document.body.removeChild(container);
298
+ });
299
+
300
+ it("reconcile() is a safe no-op when a fresh node's registered reactive owner doesn't actually contain it", () => {
301
+ // Defensive guard in notifyReplacement(): registerReactiveNode() always
302
+ // registers a node into the exact array collectNodes() just built it
303
+ // into, so indexOf() finding nothing shouldn't happen through normal
304
+ // mounting — this exercises that branch directly, at the unit level, to
305
+ // confirm the guard degrades safely (no throw, no corruption) rather
306
+ // than silently trusting it can never occur.
307
+ const fresh = document.createElement("span");
308
+ const bogusOwner: Node[] = [];
309
+ registerReactiveNode(fresh, bogusOwner);
310
+
311
+ const container = document.createElement("div");
312
+ const real = document.createElement("span");
313
+ container.appendChild(real);
314
+
315
+ expect(() => { reconcile(container, [fresh]); }).not.toThrow();
316
+ expect(bogusOwner).toEqual([]);
317
+ expect(container.firstChild).toBe(real);
318
+ });
319
+
320
+ it("plain client render (no SSG marker) is completely unaffected", () => {
321
+ const factory = () => mountElement("p", { children: "hello" }, new Scope());
322
+ const container = document.createElement("div");
323
+ document.body.appendChild(container);
324
+
325
+ render(factory, container);
326
+
327
+ expect(container.textContent).toBe("hello");
328
+ document.body.removeChild(container);
329
+ });
330
+ });
331
+
332
+ describe("Portal — server render pass", () => {
333
+ it("renders nothing into the target during a server render pass", () => {
334
+ setServerRenderPass(true);
335
+ expect(isServerRenderPass()).toBe(true);
336
+
337
+ const target = document.createElement("div");
338
+ document.body.appendChild(target);
339
+ const scope = new Scope();
340
+ const nodes = mountComponent(Portal, { to: target, children: document.createTextNode("hi") }, scope);
341
+
342
+ expect(target.textContent).toBe("");
343
+ expect(nodes.every((n) => n.nodeType === Node.COMMENT_NODE)).toBe(true);
344
+ scope.dispose();
345
+ document.body.removeChild(target);
346
+ });
347
+
348
+ it("mounts normally on the client once the server pass is over", () => {
349
+ setServerRenderPass(false);
350
+ const target = document.createElement("div");
351
+ document.body.appendChild(target);
352
+ const scope = new Scope();
353
+
354
+ mountComponent(Portal, { to: target, children: document.createTextNode("hi") }, scope);
355
+
356
+ expect(target.textContent).toBe("hi");
357
+ scope.dispose();
358
+ document.body.removeChild(target);
359
+ });
360
+ });
package/src/element.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { mountChildren } from "./children";
2
2
  import { createElement } from "./dom/create";
3
3
  import { applyProp } from "./dom/props";
4
+ import { isRecording, recordProps } from "./hydration-context";
4
5
 
5
6
  import type { Scope } from "./scope";
6
7
 
@@ -11,6 +12,10 @@ export function mountElement(
11
12
  ): HTMLElement | SVGElement {
12
13
  const el = createElement(tag);
13
14
 
15
+ // During a hydration build pass, retain (props, scope) so reconcile() can
16
+ // replay them onto whichever real element it ends up keeping instead.
17
+ if (isRecording()) recordProps(el, props, scope);
18
+
14
19
  for (const key in props) {
15
20
  if (key === "children") continue;
16
21
  applyProp(el, key, props[key], scope);
@@ -0,0 +1,53 @@
1
+ import type { Scope } from "./scope";
2
+
3
+ let _recording = false;
4
+
5
+ /**
6
+ * Marks the synchronous "build" pass of a hydration attempt: `factory()` runs
7
+ * completely normally (same create-mode code as a plain client render), but
8
+ * every element additionally records its (props, scope) for reconcile()'s
9
+ * replay step, and every mountReactive() region records which of its output
10
+ * nodes may need to be swapped in-place if reconcile() decides to keep a real
11
+ * element instead of the fresh one. No-op / zero cost outside a build pass.
12
+ */
13
+ export function runInRecording<T>(fn: () => T): T {
14
+ const prev = _recording;
15
+ _recording = true;
16
+ try {
17
+ return fn();
18
+ } finally {
19
+ _recording = prev;
20
+ }
21
+ }
22
+
23
+ export function isRecording(): boolean {
24
+ return _recording;
25
+ }
26
+
27
+ interface PropRecord {
28
+ props: Record<string, unknown>;
29
+ scope: Scope;
30
+ }
31
+
32
+ const _propRecords = new WeakMap<Element, PropRecord>();
33
+
34
+ export function recordProps(el: Element, props: Record<string, unknown>, scope: Scope): void {
35
+ _propRecords.set(el, { props, scope });
36
+ }
37
+
38
+ export function getRecordedProps(el: Element): PropRecord | undefined {
39
+ return _propRecords.get(el);
40
+ }
41
+
42
+ // mountReactive() keeps a `currentNodes` array (by reference) for diffing its
43
+ // *next* update. If reconcile() replaces one of those fresh nodes with a real
44
+ // one, it needs to overwrite that array in place so the closure sees the swap.
45
+ const _reactiveOwners = new WeakMap<Node, Node[]>();
46
+
47
+ export function registerReactiveNode(node: Node, owner: Node[]): void {
48
+ _reactiveOwners.set(node, owner);
49
+ }
50
+
51
+ export function getReactiveOwner(node: Node): Node[] | undefined {
52
+ return _reactiveOwners.get(node);
53
+ }
@@ -0,0 +1,70 @@
1
+ import { applyProp } from "./dom/props";
2
+ import { getReactiveOwner, getRecordedProps } from "./hydration-context";
3
+
4
+ function replayProps(real: Element, fresh: Element): void {
5
+ const record = getRecordedProps(fresh);
6
+ if (!record) return;
7
+ for (const key in record.props) {
8
+ if (key === "children") continue;
9
+ applyProp(real, key, record.props[key], record.scope);
10
+ }
11
+ }
12
+
13
+ // mountReactive()'s `currentNodes` array holds fresh node references for its
14
+ // *next* diff. If we're keeping `real` instead of `fresh` here, that array
15
+ // needs to see the swap too — it's registered by reference, so this mutation
16
+ // is visible to whichever effect closure owns it.
17
+ function notifyReplacement(fresh: Node, real: Node): void {
18
+ const owner = getReactiveOwner(fresh);
19
+ if (!owner) return;
20
+ const idx = owner.indexOf(fresh);
21
+ if (idx !== -1) owner[idx] = real;
22
+ }
23
+
24
+ /**
25
+ * Walks `freshChildren` (the complete, correctly-nested output of a normal
26
+ * create-mode mount into a detached scratch node) against `realParent`'s
27
+ * existing children (the server-rendered DOM already attached to the page),
28
+ * in document order — safe now that both trees are fully built, unlike trying
29
+ * to adopt while `freshChildren` was still being constructed.
30
+ *
31
+ * Matching elements keep the real node (props/listeners replayed onto it,
32
+ * via `applyProp` — the same function normal mounting uses) and recurse.
33
+ * Text/comment nodes are always taken from the fresh side — they carry no
34
+ * listeners or state, so recreating them is free and avoids having to
35
+ * re-point mountComponent/mountReactive's anchor comments. Anything that
36
+ * doesn't line up (different tag, or one side ran out) is a local mismatch:
37
+ * the stale real node is dropped and the fresh one takes its place — no
38
+ * lookahead, no reordering, and no whole-page fallback.
39
+ */
40
+ export function reconcile(realParent: Node, freshChildren: Node[]): void {
41
+ let realCursor: ChildNode | null = realParent.firstChild;
42
+
43
+ for (const fresh of freshChildren) {
44
+ if (
45
+ fresh.nodeType === Node.ELEMENT_NODE &&
46
+ realCursor?.nodeType === Node.ELEMENT_NODE &&
47
+ (realCursor as Element).localName === (fresh as Element).localName
48
+ ) {
49
+ const real = realCursor as Element;
50
+ realCursor = real.nextSibling;
51
+ replayProps(real, fresh as Element);
52
+ reconcile(real, Array.from((fresh as Element).childNodes));
53
+ notifyReplacement(fresh, real);
54
+ continue;
55
+ }
56
+
57
+ if (realCursor) {
58
+ const stale = realCursor;
59
+ realCursor = realCursor.nextSibling;
60
+ stale.remove();
61
+ }
62
+ realParent.insertBefore(fresh, realCursor);
63
+ }
64
+
65
+ while (realCursor) {
66
+ const stale = realCursor;
67
+ realCursor = realCursor.nextSibling;
68
+ stale.remove();
69
+ }
70
+ }
package/src/index.ts CHANGED
@@ -1,22 +1,17 @@
1
1
  import { mountChildren } from "./children";
2
2
  import { runInScope } from "./context";
3
+ import { reconcile } from "./hydration";
4
+ import { runInRecording } from "./hydration-context";
3
5
  import { Scope } from "./scope";
4
6
 
5
- /**
6
- * Renders a component tree into a container element.
7
- *
8
- * The factory function is called once inside the root scope,
9
- * so `jsx()` can access the current scope via `getCurrentScope()`.
10
- *
11
- * @example
12
- * render(() => <App />, document.getElementById('app'));
13
- */
14
- export function render(
7
+ /** Written by `@praxisjs/ssg` onto the mount container in its prerendered HTML. */
8
+ const SSG_MARKER = "data-praxis-ssg";
9
+
10
+ function mountCreate(
15
11
  factory: () => Node | Node[] | null,
16
12
  container: HTMLElement,
17
13
  ): () => void {
18
14
  const rootScope = new Scope();
19
-
20
15
  container.innerHTML = "";
21
16
 
22
17
  runInScope(rootScope, () => {
@@ -30,6 +25,57 @@ export function render(
30
25
  };
31
26
  }
32
27
 
28
+ function hydrate(
29
+ factory: () => Node | Node[] | null,
30
+ container: HTMLElement,
31
+ ): () => void {
32
+ const rootScope = new Scope();
33
+ const scratch = document.createDocumentFragment();
34
+
35
+ // Build normally — same code as mountCreate, just into a detached scratch
36
+ // node instead of the real container, so it can be walked in document
37
+ // order afterwards (its own construction order is bottom-up, not useful
38
+ // for matching against the server-rendered DOM directly).
39
+ runInRecording(() => {
40
+ runInScope(rootScope, () => {
41
+ const result = factory();
42
+ mountChildren(scratch, result, rootScope);
43
+ });
44
+ });
45
+
46
+ reconcile(container, Array.from(scratch.childNodes));
47
+
48
+ return () => {
49
+ rootScope.dispose();
50
+ container.innerHTML = "";
51
+ };
52
+ }
53
+
54
+ /**
55
+ * Renders a component tree into a container element.
56
+ *
57
+ * The factory function is called once inside the root scope,
58
+ * so `jsx()` can access the current scope via `getCurrentScope()`.
59
+ *
60
+ * If `container` carries the marker `@praxisjs/ssg` stamps onto prerendered
61
+ * HTML, the tree is built normally into a detached scratch node and then
62
+ * reconciled against the existing DOM (real elements are kept and have their
63
+ * props/listeners replayed onto them; text/comments and any mismatched nodes
64
+ * are recreated) instead of clearing the container and mounting fresh.
65
+ *
66
+ * @example
67
+ * render(() => <App />, document.getElementById('app'));
68
+ */
69
+ export function render(
70
+ factory: () => Node | Node[] | null,
71
+ container: HTMLElement,
72
+ ): () => void {
73
+ if (container.hasAttribute(SSG_MARKER)) {
74
+ return hydrate(factory, container);
75
+ }
76
+ return mountCreate(factory, container);
77
+ }
78
+
33
79
  export { Scope } from "./scope";
34
80
  export { runInScope, getCurrentScope } from "./context";
35
81
  export { mountElement } from "./element";
package/src/portal.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { StatelessComponent } from "@praxisjs/core";
2
+ import { isServerRenderPass } from "@praxisjs/core/internal";
2
3
 
3
4
  import { mountChildren } from "./children";
4
5
  import { getCurrentScope } from "./context";
@@ -12,6 +13,13 @@ export class Portal extends StatelessComponent<PortalProps> {
12
13
  static readonly __isStateless = true;
13
14
 
14
15
  render() {
16
+ // Portals escape to an arbitrary external target (default document.body),
17
+ // which would need a hydration cursor shared across every portal mounted at
18
+ // that same target — out of scope for v1. Skip during the server render pass
19
+ // (target content isn't part of the static HTML); the client mounts normally,
20
+ // create-mode, right after hydration completes — no duplication.
21
+ if (isServerRenderPass()) return null;
22
+
15
23
  const scope = getCurrentScope();
16
24
  const target = resolvePortalTarget(this.props.to);
17
25
  if (!target) return null;
package/src/reactive.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { runInScope } from "./context";
2
+ import { isRecording, registerReactiveNode } from "./hydration-context";
2
3
  import { Scope } from "./scope";
3
4
 
4
5
  function collectNodes(value: unknown, out: Node[]): void {
@@ -37,6 +38,15 @@ export function mountReactive(
37
38
  const newNodes: Node[] = [];
38
39
  collectNodes(result, newNodes);
39
40
 
41
+ // During a hydration build pass, remember that each of these fresh nodes
42
+ // belongs to *this* array — reconcile() mutates `newNodes` in place (by
43
+ // reference, same array `currentNodes` ends up pointing at) if it decides
44
+ // to keep a real element instead of one of them, so future diffs here
45
+ // stay correct.
46
+ if (isRecording()) {
47
+ for (const n of newNodes) registerReactiveNode(n, newNodes);
48
+ }
49
+
40
50
  const anchor = end.parentNode ?? parent;
41
51
 
42
52
  if (currentNodes.length > 0) {