@tschk/moonshine-dom 0.4.4 → 0.4.49
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.
- package/README.md +79 -10
- package/package.json +9 -4
- package/src/attributes.ts +38 -2
- package/src/binding.ts +65 -15
- package/src/component-instance.ts +3 -2
- package/src/component.ts +11 -0
- package/src/control.ts +121 -19
- package/src/dom.ts +24 -5
- package/src/framework-native.ts +18 -11
- package/src/head-ssr.ts +108 -0
- package/src/head.ts +34 -0
- package/src/platform.d.ts +4 -0
- package/src/platform.js +4 -0
- package/src/reactive.ts +22 -2
- package/src/resource-ssr.ts +73 -0
- package/src/resource.ts +7 -1
- package/src/ssr-attempt.ts +36 -1
- package/src/ssr-owner.ts +7 -0
- package/src/ssr.ts +403 -146
- package/src/store.ts +31 -0
package/src/dom.ts
CHANGED
|
@@ -578,7 +578,7 @@ export function h<P extends Record<string, unknown>>(
|
|
|
578
578
|
const elementNamespace =
|
|
579
579
|
props?.xmlns ??
|
|
580
580
|
(tag === "svg" || namespace === SVG || SVG_TAGS.has(tag) ? SVG : HTML);
|
|
581
|
-
return createIntrinsic(tag, props, children, elementNamespace);
|
|
581
|
+
return createIntrinsic(tag, props, children, String(elementNamespace));
|
|
582
582
|
}
|
|
583
583
|
|
|
584
584
|
/** Compiler-owned lexical namespace; authored xmlns remains an ordinary attribute. */
|
|
@@ -594,30 +594,49 @@ export function hNS<K extends keyof HTMLElementTagNameMap>(
|
|
|
594
594
|
props?: Record<string, unknown> | null,
|
|
595
595
|
...children: DOMChild[]
|
|
596
596
|
): HTMLElementTagNameMap[K];
|
|
597
|
+
export function hNS<K extends keyof MathMLElementTagNameMap>(
|
|
598
|
+
elementNamespace: "http://www.w3.org/1998/Math/MathML",
|
|
599
|
+
tag: K,
|
|
600
|
+
props?: Record<string, unknown> | null,
|
|
601
|
+
...children: DOMChild[]
|
|
602
|
+
): MathMLElementTagNameMap[K];
|
|
597
603
|
export function hNS(
|
|
598
|
-
elementNamespace:
|
|
604
|
+
elementNamespace: string,
|
|
599
605
|
tag: string,
|
|
600
606
|
props?: Record<string, unknown> | null,
|
|
601
607
|
...children: DOMChild[]
|
|
602
608
|
): Element;
|
|
603
609
|
export function hNS(
|
|
604
|
-
elementNamespace:
|
|
610
|
+
elementNamespace: string,
|
|
605
611
|
tag: string,
|
|
606
612
|
props: Record<string, unknown> | null = null,
|
|
607
613
|
...children: DOMChild[]
|
|
608
614
|
): Element {
|
|
609
|
-
if (
|
|
615
|
+
if (typeof elementNamespace !== "string")
|
|
610
616
|
throw new TypeError("Unsupported element namespace");
|
|
611
617
|
return createIntrinsic(tag, props, children, elementNamespace);
|
|
612
618
|
}
|
|
613
619
|
|
|
620
|
+
/** Compiler-owned namespace values retain browser coercion and errors. */
|
|
621
|
+
export function hSourceNS(
|
|
622
|
+
elementNamespace: unknown,
|
|
623
|
+
tag: string,
|
|
624
|
+
props: Record<string, unknown> | null = null,
|
|
625
|
+
...children: DOMChild[]
|
|
626
|
+
): Element {
|
|
627
|
+
return createIntrinsic(tag, props, children, elementNamespace ?? HTML);
|
|
628
|
+
}
|
|
629
|
+
|
|
614
630
|
function createIntrinsic(
|
|
615
631
|
tag: string,
|
|
616
632
|
props: Record<string, unknown> | null,
|
|
617
633
|
children: DOMChild[],
|
|
618
634
|
elementNamespace: unknown,
|
|
619
635
|
): Element {
|
|
620
|
-
const element = document.createElementNS(
|
|
636
|
+
const element = document.createElementNS(
|
|
637
|
+
elementNamespace as string | null,
|
|
638
|
+
tag,
|
|
639
|
+
);
|
|
621
640
|
const select = element instanceof HTMLSelectElement;
|
|
622
641
|
const input = element instanceof HTMLInputElement;
|
|
623
642
|
const textarea = element instanceof HTMLTextAreaElement;
|
package/src/framework-native.ts
CHANGED
|
@@ -5,7 +5,8 @@ import {
|
|
|
5
5
|
type DocumentMetadata,
|
|
6
6
|
type RenderContext,
|
|
7
7
|
} from "@tschk/moonshine-framework";
|
|
8
|
-
import { h, defer,
|
|
8
|
+
import { h, defer, escapeHtml, type SSRChild } from "./ssr";
|
|
9
|
+
import { head, title, renderWithHead } from "./head-ssr";
|
|
9
10
|
|
|
10
11
|
type PageProps = {
|
|
11
12
|
params: Record<string, string>;
|
|
@@ -71,11 +72,24 @@ export function createNativeRenderer(
|
|
|
71
72
|
}
|
|
72
73
|
}
|
|
73
74
|
let body: string;
|
|
75
|
+
let headHTML = "";
|
|
76
|
+
const renderBody = (read: () => SSRChild): string => {
|
|
77
|
+
const result = renderWithHead(() => {
|
|
78
|
+
if (metadata.description != null)
|
|
79
|
+
head(() =>
|
|
80
|
+
h("meta", { name: "description", content: metadata.description }),
|
|
81
|
+
);
|
|
82
|
+
if (metadata.title != null) title(() => metadata.title);
|
|
83
|
+
return read();
|
|
84
|
+
});
|
|
85
|
+
headHTML = result.head;
|
|
86
|
+
return result.body;
|
|
87
|
+
};
|
|
74
88
|
let scripts = "";
|
|
75
89
|
if (route.mode === "spa") {
|
|
76
90
|
if (!route.clientEntries.length)
|
|
77
91
|
throw new Error("Native SPA route requires a client entry");
|
|
78
|
-
body =
|
|
92
|
+
body = renderBody(() =>
|
|
79
93
|
h("div", { id: "moonshine-app", "data-moonshine-app": route.id }),
|
|
80
94
|
);
|
|
81
95
|
scripts = `<script type="application/json" data-moonshine-props>${serializeData({ params, data })}</script>`;
|
|
@@ -85,7 +99,7 @@ export function createNativeRenderer(
|
|
|
85
99
|
} else {
|
|
86
100
|
const Page = component(pageKey);
|
|
87
101
|
const layouts = layoutKeys.map(component);
|
|
88
|
-
body =
|
|
102
|
+
body = renderBody(() => {
|
|
89
103
|
let child: () => SSRChild = () => h(Page, { params, data });
|
|
90
104
|
for (let index = layouts.length - 1; index >= 0; index--) {
|
|
91
105
|
const Layout = layouts[index]!;
|
|
@@ -100,14 +114,7 @@ export function createNativeRenderer(
|
|
|
100
114
|
.join("");
|
|
101
115
|
const lang =
|
|
102
116
|
metadata.lang == null ? "" : ` lang="${escapeHtml(metadata.lang)}"`;
|
|
103
|
-
|
|
104
|
-
(metadata.title == null
|
|
105
|
-
? ""
|
|
106
|
-
: `<title>${escapeHtml(metadata.title)}</title>`) +
|
|
107
|
-
(metadata.description == null
|
|
108
|
-
? ""
|
|
109
|
-
: `<meta name="description" content="${escapeHtml(metadata.description)}">`);
|
|
110
|
-
return `<!doctype html><html${lang}><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">${head}${styles}</head><body>${body}${scripts}</body></html>`;
|
|
117
|
+
return `<!doctype html><html${lang}><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">${headHTML}${styles}</head><body>${body}${scripts}</body></html>`;
|
|
111
118
|
}
|
|
112
119
|
|
|
113
120
|
return {
|
package/src/head-ssr.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/** Optional synchronous SSR head collector; no browser, host or async-SSR support. */
|
|
2
|
+
import {
|
|
3
|
+
h,
|
|
4
|
+
getOwner,
|
|
5
|
+
renderToString,
|
|
6
|
+
serializeSSRChild,
|
|
7
|
+
type SSRChild,
|
|
8
|
+
} from "./ssr";
|
|
9
|
+
import { isSSRRendering, getSSRRenderScope } from "./ssr-owner";
|
|
10
|
+
import {
|
|
11
|
+
captureActiveSSRRenderAttempt,
|
|
12
|
+
isSSRRenderAccepted,
|
|
13
|
+
type SSRRenderAttempt,
|
|
14
|
+
} from "./ssr-attempt";
|
|
15
|
+
|
|
16
|
+
type Entry = {
|
|
17
|
+
html: string;
|
|
18
|
+
title: boolean;
|
|
19
|
+
scope?: Entry;
|
|
20
|
+
attempt: SSRRenderAttempt | undefined;
|
|
21
|
+
};
|
|
22
|
+
type Collector = { entries: Entry[]; scope?: Entry; request?: object };
|
|
23
|
+
|
|
24
|
+
const collectorKey = /* @__PURE__ */ Symbol("moonshine.ssr.head");
|
|
25
|
+
|
|
26
|
+
function activeCollector(name: string): Collector {
|
|
27
|
+
const owner = getOwner();
|
|
28
|
+
const collector = owner?.context?.get(collectorKey) as Collector | undefined;
|
|
29
|
+
if (
|
|
30
|
+
!isSSRRendering() ||
|
|
31
|
+
!owner ||
|
|
32
|
+
owner.disposed ||
|
|
33
|
+
!collector ||
|
|
34
|
+
collector.request !== getSSRRenderScope()
|
|
35
|
+
)
|
|
36
|
+
throw new Error(
|
|
37
|
+
`${name}() requires an active renderWithHead() server scope`,
|
|
38
|
+
);
|
|
39
|
+
return collector;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function reserve(collector: Collector, isTitle: boolean): Entry {
|
|
43
|
+
const entry: Entry = {
|
|
44
|
+
html: "",
|
|
45
|
+
title: isTitle,
|
|
46
|
+
attempt: captureActiveSSRRenderAttempt(),
|
|
47
|
+
};
|
|
48
|
+
collector.entries.push(entry);
|
|
49
|
+
return entry;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function release(collector: Collector, entry: Entry): void {
|
|
53
|
+
const index = collector.entries.indexOf(entry);
|
|
54
|
+
if (index >= 0) collector.entries.splice(index);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function renderWithHead(fn: () => SSRChild): {
|
|
58
|
+
body: string;
|
|
59
|
+
head: string;
|
|
60
|
+
} {
|
|
61
|
+
const collector: Collector = { entries: [] };
|
|
62
|
+
const body = renderToString(() => {
|
|
63
|
+
collector.request = getSSRRenderScope();
|
|
64
|
+
const owner = getOwner()!;
|
|
65
|
+
owner.context = new Map(owner.context);
|
|
66
|
+
owner.context.set(collectorKey, collector);
|
|
67
|
+
return fn();
|
|
68
|
+
});
|
|
69
|
+
let headHTML = "";
|
|
70
|
+
let titleHTML = "";
|
|
71
|
+
const titled = new Set<Entry>();
|
|
72
|
+
for (const entry of collector.entries) {
|
|
73
|
+
if (!isSSRRenderAccepted(entry.attempt)) continue;
|
|
74
|
+
if (entry.title) {
|
|
75
|
+
if (entry.scope && titled.has(entry.scope)) continue;
|
|
76
|
+
if (entry.scope) titled.add(entry.scope);
|
|
77
|
+
titleHTML = entry.html;
|
|
78
|
+
} else headHTML += entry.html;
|
|
79
|
+
}
|
|
80
|
+
return { body, head: headHTML + titleHTML };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function head(render: () => SSRChild): undefined {
|
|
84
|
+
const collector = activeCollector("head");
|
|
85
|
+
const entry = reserve(collector, false);
|
|
86
|
+
const previous = collector.scope;
|
|
87
|
+
collector.scope = entry;
|
|
88
|
+
try {
|
|
89
|
+
entry.html = serializeSSRChild(render());
|
|
90
|
+
} catch (error) {
|
|
91
|
+
release(collector, entry);
|
|
92
|
+
throw error;
|
|
93
|
+
} finally {
|
|
94
|
+
collector.scope = previous;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function title(read: () => SSRChild): undefined {
|
|
99
|
+
const collector = activeCollector("title");
|
|
100
|
+
const entry = reserve(collector, true);
|
|
101
|
+
entry.scope = collector.scope;
|
|
102
|
+
try {
|
|
103
|
+
entry.html = serializeSSRChild(h("title", null, read()));
|
|
104
|
+
} catch (error) {
|
|
105
|
+
release(collector, entry);
|
|
106
|
+
throw error;
|
|
107
|
+
}
|
|
108
|
+
}
|
package/src/head.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/** Opt-in document head declarations; title writes persist after owner disposal. */
|
|
2
|
+
import { render, type DOMChild } from "./dom";
|
|
3
|
+
import { createRenderEffect, getOwner, onCleanup } from "./reactive";
|
|
4
|
+
import { isSSRRendering } from "./ssr-owner";
|
|
5
|
+
import { peekComponentContext, withComponentConsumer } from "./component";
|
|
6
|
+
|
|
7
|
+
function requireOwner(): void {
|
|
8
|
+
const owner = getOwner();
|
|
9
|
+
if (!owner || owner.disposed || isSSRRendering())
|
|
10
|
+
throw new Error("DOM head declarations require a live browser owner");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Mount only this owner's head nodes, preserving unrelated document content. */
|
|
14
|
+
export function head(children: () => DOMChild): undefined {
|
|
15
|
+
requireOwner();
|
|
16
|
+
let stop: (() => void) | undefined;
|
|
17
|
+
let disposed = false;
|
|
18
|
+
onCleanup(() => {
|
|
19
|
+
disposed = true;
|
|
20
|
+
stop?.();
|
|
21
|
+
});
|
|
22
|
+
stop = render(children, document.head);
|
|
23
|
+
// Child setup may dispose the containing component before render returns.
|
|
24
|
+
if (disposed) stop();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Update the document title; removing a declaration does not restore an older title. */
|
|
28
|
+
export function title(read: () => string): undefined {
|
|
29
|
+
requireOwner();
|
|
30
|
+
const consumer = peekComponentContext();
|
|
31
|
+
createRenderEffect(() => {
|
|
32
|
+
document.title = consumer ? withComponentConsumer(consumer, read) : read();
|
|
33
|
+
});
|
|
34
|
+
}
|
package/src/platform.js
ADDED
package/src/reactive.ts
CHANGED
|
@@ -371,6 +371,24 @@ export function runWithOwner<T>(scope: Owner | null, fn: () => T): T {
|
|
|
371
371
|
}
|
|
372
372
|
}
|
|
373
373
|
|
|
374
|
+
/** Untracked callback scope; newly created effects start after the callback. */
|
|
375
|
+
export function runWithOwnerCallback<T>(
|
|
376
|
+
scope: Owner | null,
|
|
377
|
+
fn: () => T,
|
|
378
|
+
): T | undefined {
|
|
379
|
+
return runWithOwner(scope, () =>
|
|
380
|
+
untrack(() =>
|
|
381
|
+
runUpdates(() => {
|
|
382
|
+
try {
|
|
383
|
+
return fn();
|
|
384
|
+
} catch (error) {
|
|
385
|
+
if (!scope || !routeHandler(scope)?.(error)) throw error;
|
|
386
|
+
}
|
|
387
|
+
}),
|
|
388
|
+
),
|
|
389
|
+
);
|
|
390
|
+
}
|
|
391
|
+
|
|
374
392
|
/** Attached component setup: isolate dependencies and dispose failed owned work. */
|
|
375
393
|
export function createComponentScope<T>(fn: () => T): T {
|
|
376
394
|
return invokeCallback(() => {
|
|
@@ -480,7 +498,8 @@ function computation<T>(
|
|
|
480
498
|
const tracked = moonshineMemo(() =>
|
|
481
499
|
invokeCallback(() => {
|
|
482
500
|
if (scope.disposed) return;
|
|
483
|
-
|
|
501
|
+
if (scope.children.size || scope.cleanups.length)
|
|
502
|
+
untrack(() => clean(scope));
|
|
484
503
|
try {
|
|
485
504
|
previous = runWithOwner(scope, () => fn(previous));
|
|
486
505
|
} catch (error) {
|
|
@@ -572,7 +591,8 @@ export function createMemo<T>(
|
|
|
572
591
|
const tracked = moonshineMemo(() =>
|
|
573
592
|
invokeCallback(() => {
|
|
574
593
|
if (!active() || scope.disposed) return current;
|
|
575
|
-
|
|
594
|
+
if (scope.children.size || scope.cleanups.length)
|
|
595
|
+
untrack(() => clean(scope));
|
|
576
596
|
let next: T;
|
|
577
597
|
try {
|
|
578
598
|
next = runWithOwner(scope, () => fn(current.value));
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/** Synchronous server resources: sources are read, fetchers never run. */
|
|
2
|
+
import { peekComponentContext } from "./component";
|
|
3
|
+
import { untrack, type Accessor } from "./reactive";
|
|
4
|
+
import type {
|
|
5
|
+
Resource,
|
|
6
|
+
ResourceFetcher,
|
|
7
|
+
ResourceOptions,
|
|
8
|
+
ResourceSource,
|
|
9
|
+
ResourceState,
|
|
10
|
+
} from "./resource";
|
|
11
|
+
import { isSSRRendering } from "./ssr-owner";
|
|
12
|
+
|
|
13
|
+
export type ServerResourceActions<T, R = unknown> = {
|
|
14
|
+
mutate: (value: T | undefined) => T | undefined;
|
|
15
|
+
refetch: (info?: R) => boolean;
|
|
16
|
+
};
|
|
17
|
+
export type ServerResourceReturn<T, R = unknown> = [
|
|
18
|
+
Resource<T>,
|
|
19
|
+
ServerResourceActions<T, R>,
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
export function createResource<T, R = unknown>(
|
|
23
|
+
fetcher: ResourceFetcher<true, T, R>,
|
|
24
|
+
options?: ResourceOptions<T>,
|
|
25
|
+
): ServerResourceReturn<T, R>;
|
|
26
|
+
export function createResource<T, S, R = unknown>(
|
|
27
|
+
source: ResourceSource<S>,
|
|
28
|
+
fetcher: ResourceFetcher<S, T, R>,
|
|
29
|
+
options?: ResourceOptions<T>,
|
|
30
|
+
): ServerResourceReturn<T, R>;
|
|
31
|
+
export function createResource<T, S, R = unknown>(
|
|
32
|
+
sourceOrFetcher: ResourceSource<S> | ResourceFetcher<true, T, R>,
|
|
33
|
+
fetcherOrOptions?: ResourceFetcher<S, T, R> | ResourceOptions<T>,
|
|
34
|
+
suppliedOptions?: ResourceOptions<T>,
|
|
35
|
+
): ServerResourceReturn<T, R> {
|
|
36
|
+
if (!isSSRRendering() && peekComponentContext()?.mode !== "ssr")
|
|
37
|
+
throw new Error(
|
|
38
|
+
"Server createResource requires an active SSR render or SSR component context",
|
|
39
|
+
);
|
|
40
|
+
const hasSource = typeof fetcherOrOptions === "function";
|
|
41
|
+
const options = (hasSource ? suppliedOptions : fetcherOrOptions) as
|
|
42
|
+
ResourceOptions<T> | undefined;
|
|
43
|
+
for (const key of Object.keys(options ?? {})) {
|
|
44
|
+
if (key !== "initialValue" && key !== "name")
|
|
45
|
+
throw new TypeError(`Unsupported native resource option: ${key}`);
|
|
46
|
+
}
|
|
47
|
+
const source = hasSource ? sourceOrFetcher : true;
|
|
48
|
+
const seeded = options != null && "initialValue" in options;
|
|
49
|
+
const state: ResourceState = seeded ? "ready" : "unresolved";
|
|
50
|
+
let value = seeded ? options?.initialValue : undefined;
|
|
51
|
+
let loading = false;
|
|
52
|
+
|
|
53
|
+
function load(): boolean {
|
|
54
|
+
return (loading = !!(typeof source === "function"
|
|
55
|
+
? untrack(source as Accessor<unknown>)
|
|
56
|
+
: source));
|
|
57
|
+
}
|
|
58
|
+
load();
|
|
59
|
+
const read = (() => value) as Resource<T>;
|
|
60
|
+
Object.defineProperties(read, {
|
|
61
|
+
state: { value: state },
|
|
62
|
+
loading: { get: () => loading },
|
|
63
|
+
error: { value: undefined },
|
|
64
|
+
latest: { get: read },
|
|
65
|
+
});
|
|
66
|
+
return [
|
|
67
|
+
read,
|
|
68
|
+
{
|
|
69
|
+
mutate: (next) => (value = next),
|
|
70
|
+
refetch: load,
|
|
71
|
+
},
|
|
72
|
+
];
|
|
73
|
+
}
|
package/src/resource.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/** Client resources without Suspense, hydration, or asynchronous SSR. */
|
|
2
|
+
import { peekComponentContext } from "./component";
|
|
2
3
|
import {
|
|
3
4
|
batch,
|
|
4
5
|
createComputed,
|
|
@@ -11,6 +12,7 @@ import {
|
|
|
11
12
|
type Accessor,
|
|
12
13
|
type Setter,
|
|
13
14
|
} from "./reactive";
|
|
15
|
+
import { isSSRRendering } from "./ssr-owner";
|
|
14
16
|
|
|
15
17
|
export type ResourceState =
|
|
16
18
|
"unresolved" | "pending" | "ready" | "refreshing" | "errored";
|
|
@@ -50,6 +52,10 @@ export function createResource<T, S, R = unknown>(
|
|
|
50
52
|
fetcherOrOptions?: ResourceFetcher<S, T, R> | ResourceOptions<T>,
|
|
51
53
|
suppliedOptions?: ResourceOptions<T>,
|
|
52
54
|
): ResourceReturn<T, R> {
|
|
55
|
+
if (isSSRRendering() || peekComponentContext()?.mode === "ssr")
|
|
56
|
+
throw new Error(
|
|
57
|
+
"Client createResource is unavailable during SSR; use createResource from the server resource module",
|
|
58
|
+
);
|
|
53
59
|
const hasSource = typeof fetcherOrOptions === "function";
|
|
54
60
|
const source = (hasSource ? sourceOrFetcher : true) as ResourceSource<S>;
|
|
55
61
|
const fetcher = (
|
|
@@ -63,7 +69,7 @@ export function createResource<T, S, R = unknown>(
|
|
|
63
69
|
}
|
|
64
70
|
const [value, setValue] = createSignal<T | undefined>(options?.initialValue);
|
|
65
71
|
const [error, setError] = createSignal<Error | undefined>(undefined);
|
|
66
|
-
let resolved = options != null &&
|
|
72
|
+
let resolved = options != null && "initialValue" in options;
|
|
67
73
|
const [state, setState] = createSignal<ResourceState>(
|
|
68
74
|
resolved ? "ready" : "unresolved",
|
|
69
75
|
);
|
package/src/ssr-attempt.ts
CHANGED
|
@@ -5,16 +5,51 @@ export type SSRRenderAttempt = {
|
|
|
5
5
|
status: "pending" | "accepted" | "discarded";
|
|
6
6
|
parent?: SSRRenderAttempt;
|
|
7
7
|
};
|
|
8
|
+
let active = false;
|
|
9
|
+
let currentAttempt: SSRRenderAttempt | undefined;
|
|
10
|
+
/** Keep serialization attempts current when lexical component owners are restored. */
|
|
11
|
+
export function withSSRRenderAttempt<T>(
|
|
12
|
+
attempt: SSRRenderAttempt | undefined,
|
|
13
|
+
read: () => T,
|
|
14
|
+
): T {
|
|
15
|
+
const previous = currentAttempt;
|
|
16
|
+
const wasActive = active;
|
|
17
|
+
active = true;
|
|
18
|
+
currentAttempt = attempt;
|
|
19
|
+
try {
|
|
20
|
+
return read();
|
|
21
|
+
} finally {
|
|
22
|
+
currentAttempt = previous;
|
|
23
|
+
active = wasActive;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
8
26
|
export function captureSSRRenderAttempt(): SSRRenderAttempt | undefined {
|
|
9
27
|
return getOwner()?.context?.get(attemptKey) as SSRRenderAttempt | undefined;
|
|
10
28
|
}
|
|
29
|
+
/** Publishing follows the active buffer while lifecycle capture remains lexical. */
|
|
30
|
+
export function captureActiveSSRRenderAttempt(): SSRRenderAttempt | undefined {
|
|
31
|
+
const lexical = captureSSRRenderAttempt();
|
|
32
|
+
if (!active) return lexical;
|
|
33
|
+
if (!currentAttempt) return undefined;
|
|
34
|
+
for (let attempt = lexical; attempt; attempt = attempt.parent)
|
|
35
|
+
if (attempt === currentAttempt) return lexical;
|
|
36
|
+
return currentAttempt;
|
|
37
|
+
}
|
|
38
|
+
/** A new request must not inherit its caller's pending lifecycle attempt. */
|
|
39
|
+
export function resetSSRRenderAttempt(): void {
|
|
40
|
+
const owner = getOwner();
|
|
41
|
+
if (owner?.context?.has(attemptKey)) {
|
|
42
|
+
owner.context = new Map(owner.context);
|
|
43
|
+
owner.context.delete(attemptKey);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
11
46
|
export function createSSRRenderAttempt(): SSRRenderAttempt {
|
|
12
47
|
const owner = getOwner();
|
|
13
48
|
if (!owner || owner.disposed)
|
|
14
49
|
throw new Error("SSR attempts require a live native owner");
|
|
15
50
|
const attempt: SSRRenderAttempt = {
|
|
16
51
|
status: "pending",
|
|
17
|
-
parent:
|
|
52
|
+
parent: captureActiveSSRRenderAttempt(),
|
|
18
53
|
};
|
|
19
54
|
owner.context = new Map(owner.context);
|
|
20
55
|
owner.context.set(attemptKey, attempt);
|
package/src/ssr-owner.ts
CHANGED
|
@@ -3,13 +3,20 @@ import { getOwner, type Owner } from "./reactive";
|
|
|
3
3
|
import type { SSRChild } from "./ssr";
|
|
4
4
|
|
|
5
5
|
let rendering = 0;
|
|
6
|
+
let renderScope: object | undefined;
|
|
7
|
+
export function getSSRRenderScope(): object | undefined {
|
|
8
|
+
return renderScope;
|
|
9
|
+
}
|
|
6
10
|
/** shortcut: synchronous SSR only; use request-local context for async rendering. */
|
|
7
11
|
export function withSSRRendering<T>(fn: () => T): T {
|
|
12
|
+
const previous = renderScope;
|
|
13
|
+
renderScope = {};
|
|
8
14
|
rendering++;
|
|
9
15
|
try {
|
|
10
16
|
return fn();
|
|
11
17
|
} finally {
|
|
12
18
|
rendering--;
|
|
19
|
+
renderScope = previous;
|
|
13
20
|
}
|
|
14
21
|
}
|
|
15
22
|
export function isSSRRendering(): boolean {
|