@ecopages/react 0.2.0-beta.12 → 0.2.0-beta.14
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 +12 -0
- package/package.json +20 -7
- package/src/layout-compose.d.ts +58 -0
- package/src/layout-compose.js +98 -0
- package/src/react-hmr-strategy.js +1 -1
- package/src/react-renderer.d.ts +31 -4
- package/src/react-renderer.js +157 -45
- package/src/react.plugin.js +1 -1
- package/src/serialize-page-data-script.d.ts +8 -0
- package/src/serialize-page-data-script.js +10 -0
- package/src/services/react-mdx-config-dependency.service.js +5 -7
- package/src/services/react-page-payload.service.js +2 -2
- package/src/utils/client-graph-ast-transform.d.ts +18 -0
- package/src/utils/client-graph-ast-transform.js +335 -0
- package/src/utils/client-graph-boundary-plugin.d.ts +1 -12
- package/src/utils/client-graph-boundary-plugin.js +8 -454
- package/src/utils/client-graph-reachability.d.ts +75 -0
- package/src/utils/client-graph-reachability.js +148 -0
- package/src/utils/component-config-traversal.js +4 -2
- package/src/utils/hydration-scripts.d.ts +2 -0
- package/src/utils/hydration-scripts.js +27 -14
- package/src/utils/react-mdx-loader-plugin.js +1 -1
- package/node_modules/@ecopages/mdx-core/package.json +0 -22
- package/node_modules/@ecopages/mdx-core/src/index.d.ts +0 -2
- package/node_modules/@ecopages/mdx-core/src/index.js +0 -18
- package/node_modules/@ecopages/mdx-core/src/mdx-loader-plugin.d.ts +0 -11
- package/node_modules/@ecopages/mdx-core/src/mdx-loader-plugin.js +0 -44
- package/node_modules/@ecopages/mdx-core/src/mdx-utils.d.ts +0 -29
- package/node_modules/@ecopages/mdx-core/src/mdx-utils.js +0 -55
package/README.md
CHANGED
|
@@ -206,3 +206,15 @@ The main regression coverage lives in:
|
|
|
206
206
|
- [../../react-router/test/hmr-reload.test.browser.ts](../../react-router/test/hmr-reload.test.browser.ts): verifies router-backed layout hydration receives `locals` with `persistLayouts` both enabled and disabled.
|
|
207
207
|
|
|
208
208
|
If you change the AST transform or hydration flow, update the corresponding tests in the same change.
|
|
209
|
+
|
|
210
|
+
### Nested layouts and unified SSR
|
|
211
|
+
|
|
212
|
+
Pages may declare `layout` as a single component or an **outer → inner** array on `eco.page()`. On the server, React-managed layout stacks compose through `composeDocumentShell` + `composeLayoutPageTree` so provider context reaches nested pages during SSR. On the client, `@ecopages/react-router` `PageContent` uses the same `composeLayoutPageTree` helper unless `persistLayouts` is enabled (then each tier is cached independently).
|
|
213
|
+
|
|
214
|
+
During SSR, `ReactRenderer` passes the app-resolved React runtime into `composeLayoutPageTree` via `options.react` so layout trees are built with the same module instance as `renderToString`. App code should import hooks and context from `react` normally; keep a single React version in the app dependency graph (avoid duplicate `react` copies in monorepos).
|
|
215
|
+
|
|
216
|
+
Layout prop factories receive `LayoutPropsContext` (`params`, `query`, `locals`). Route-scoped `locals` are passed to layout tiers via document-shell props during SSR; serialized `pageProps.locals` follow `Page.requires` and are intended for the page component.
|
|
217
|
+
|
|
218
|
+
- [src/layout-compose.ts](src/layout-compose.ts): shared client/SSR tree builder.
|
|
219
|
+
- [src/test/react-ssr-hydration-parity.test.tsx](src/test/react-ssr-hydration-parity.test.tsx): nested tier order parity between SSR and `composeLayoutPageTree`.
|
|
220
|
+
- [src/test/react-ssr-unified.test.tsx](src/test/react-ssr-unified.test.tsx): provider context through nested SSR layouts.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ecopages/react",
|
|
3
|
-
"version": "0.2.0-beta.
|
|
3
|
+
"version": "0.2.0-beta.14",
|
|
4
4
|
"description": "React integration for Ecopages",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ecopages",
|
|
@@ -39,6 +39,14 @@
|
|
|
39
39
|
"types": "./src/router-adapter.d.ts",
|
|
40
40
|
"default": "./src/router-adapter.js"
|
|
41
41
|
},
|
|
42
|
+
"./layout-compose": {
|
|
43
|
+
"types": "./src/layout-compose.d.ts",
|
|
44
|
+
"default": "./src/layout-compose.js"
|
|
45
|
+
},
|
|
46
|
+
"./serialize-page-data-script": {
|
|
47
|
+
"types": "./src/serialize-page-data-script.d.ts",
|
|
48
|
+
"default": "./src/serialize-page-data-script.js"
|
|
49
|
+
},
|
|
42
50
|
"./eco-embed.ts": {
|
|
43
51
|
"default": "./src/eco-embed.js",
|
|
44
52
|
"types": "./src/eco-embed.d.ts"
|
|
@@ -61,6 +69,14 @@
|
|
|
61
69
|
"./router-adapter.ts": {
|
|
62
70
|
"types": "./src/router-adapter.d.ts",
|
|
63
71
|
"default": "./src/router-adapter.js"
|
|
72
|
+
},
|
|
73
|
+
"./layout-compose.ts": {
|
|
74
|
+
"types": "./src/layout-compose.d.ts",
|
|
75
|
+
"default": "./src/layout-compose.js"
|
|
76
|
+
},
|
|
77
|
+
"./serialize-page-data-script.ts": {
|
|
78
|
+
"types": "./src/serialize-page-data-script.d.ts",
|
|
79
|
+
"default": "./src/serialize-page-data-script.js"
|
|
64
80
|
}
|
|
65
81
|
},
|
|
66
82
|
"repository": {
|
|
@@ -69,19 +85,16 @@
|
|
|
69
85
|
"directory": "packages/integrations/react"
|
|
70
86
|
},
|
|
71
87
|
"peerDependencies": {
|
|
72
|
-
"@ecopages/core": "0.2.0-beta.
|
|
88
|
+
"@ecopages/core": "0.2.0-beta.14",
|
|
73
89
|
"@types/react": "^19",
|
|
74
90
|
"@types/react-dom": "^19",
|
|
75
91
|
"react": "^19",
|
|
76
92
|
"react-dom": "^19"
|
|
77
93
|
},
|
|
78
|
-
"bundleDependencies": [
|
|
79
|
-
"@ecopages/mdx-core"
|
|
80
|
-
],
|
|
81
94
|
"dependencies": {
|
|
82
|
-
"@ecopages/file-system": "0.2.0-beta.
|
|
95
|
+
"@ecopages/file-system": "0.2.0-beta.14",
|
|
83
96
|
"@ecopages/logger": "^0.2.3",
|
|
84
|
-
"@ecopages/mdx
|
|
97
|
+
"@ecopages/mdx": "0.2.0-beta.14",
|
|
85
98
|
"@mdx-js/mdx": "^3.1.1",
|
|
86
99
|
"oxc-parser": "^0.124.0",
|
|
87
100
|
"oxc-transform": "^0.124.0",
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import * as bundledReact from 'react';
|
|
2
|
+
import type { FunctionComponent, ReactElement } from 'react';
|
|
3
|
+
import type { EcoComponent, EcoDeclaredComponent, EcoPageLayoutEntry, LayoutPropsContext } from '@ecopages/core';
|
|
4
|
+
import type { EcoComponentConfig } from '@ecopages/core';
|
|
5
|
+
export type LayoutComposeContext = LayoutPropsContext;
|
|
6
|
+
export type ReactRuntime = typeof bundledReact;
|
|
7
|
+
export type LayoutComposeOptions = {
|
|
8
|
+
context?: LayoutComposeContext;
|
|
9
|
+
/** @remarks Pass the app-resolved React runtime during SSR so composition matches `renderToString`. */
|
|
10
|
+
react?: ReactRuntime;
|
|
11
|
+
};
|
|
12
|
+
export type LayoutShellEntry = {
|
|
13
|
+
component: EcoComponent;
|
|
14
|
+
props?: Record<string, unknown>;
|
|
15
|
+
};
|
|
16
|
+
export type ComposablePage<P extends Record<string, unknown> = Record<string, unknown>> = FunctionComponent<P> & {
|
|
17
|
+
config?: Pick<EcoComponentConfig, 'layouts' | 'layoutEntries'>;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Reads request-scoped layout context from serialized page props.
|
|
21
|
+
*/
|
|
22
|
+
export declare function resolveLayoutContext(pageProps: Record<string, unknown>): LayoutComposeContext;
|
|
23
|
+
/**
|
|
24
|
+
* Builds layout prop factory context from document-shell layout entries.
|
|
25
|
+
*
|
|
26
|
+
* @remarks
|
|
27
|
+
* Route-scoped `locals` come from shell layout props, not `pageProps.locals` (pageLocals).
|
|
28
|
+
*/
|
|
29
|
+
export declare function resolveLayoutContextFromShell(pageProps: Record<string, unknown>, shellLayouts: LayoutShellEntry[]): LayoutComposeContext;
|
|
30
|
+
/**
|
|
31
|
+
* Resolves props for one layout entry, merging shell locals with an optional factory.
|
|
32
|
+
*/
|
|
33
|
+
export declare function resolveLayoutEntryProps(entry: EcoPageLayoutEntry, context: LayoutComposeContext): Record<string, unknown>;
|
|
34
|
+
/**
|
|
35
|
+
* @remarks
|
|
36
|
+
* Eco page components are wider than React layout composition input; runtime checks narrow callables.
|
|
37
|
+
*/
|
|
38
|
+
export declare function assertComposablePage(Page: unknown): ComposablePage;
|
|
39
|
+
/**
|
|
40
|
+
* Builds the client or SSR React tree for a page and its outer→inner layout stack.
|
|
41
|
+
*
|
|
42
|
+
* @remarks
|
|
43
|
+
* Layout prop factories receive `LayoutPropsContext`. For SSR, pass `options.react` from the
|
|
44
|
+
* app-resolved runtime. Client code can omit it and use the bundled React import.
|
|
45
|
+
*/
|
|
46
|
+
export declare function composeLayoutPageTree<P extends Record<string, unknown>>(Page: ComposablePage<P>, pageProps: P, options?: LayoutComposeOptions): ReactElement;
|
|
47
|
+
/**
|
|
48
|
+
* Builds a layout tree from explicit document-shell layout entries.
|
|
49
|
+
*
|
|
50
|
+
* @remarks
|
|
51
|
+
* Use when the route renderer passes per-tier props (e.g. route `locals` on the layout tier).
|
|
52
|
+
* Shell `entry.props` are authoritative; page config normalization is not required.
|
|
53
|
+
*/
|
|
54
|
+
export declare function composeLayoutPageTreeFromShell<P extends Record<string, unknown>>(Page: ComposablePage<P>, pageProps: P, shellLayouts: LayoutShellEntry[], options?: LayoutComposeOptions): ReactElement;
|
|
55
|
+
/**
|
|
56
|
+
* Normalizes page config layout metadata to an outer→inner component list.
|
|
57
|
+
*/
|
|
58
|
+
export declare function normalizePageLayoutComponents(layouts?: EcoDeclaredComponent[]): EcoDeclaredComponent[];
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import * as bundledReact from "react";
|
|
2
|
+
function resolveReact(options) {
|
|
3
|
+
return options?.react ?? bundledReact;
|
|
4
|
+
}
|
|
5
|
+
function toReactComponent(component) {
|
|
6
|
+
if (typeof component !== "function") {
|
|
7
|
+
throw new TypeError("[ecopages] Expected a function component for layout composition.");
|
|
8
|
+
}
|
|
9
|
+
return component;
|
|
10
|
+
}
|
|
11
|
+
function resolveLayoutContext(pageProps) {
|
|
12
|
+
const context = {};
|
|
13
|
+
if (pageProps.params !== void 0) {
|
|
14
|
+
context.params = pageProps.params;
|
|
15
|
+
}
|
|
16
|
+
if (pageProps.query !== void 0) {
|
|
17
|
+
context.query = pageProps.query;
|
|
18
|
+
}
|
|
19
|
+
if (pageProps.locals !== void 0) {
|
|
20
|
+
context.locals = pageProps.locals;
|
|
21
|
+
}
|
|
22
|
+
return context;
|
|
23
|
+
}
|
|
24
|
+
function resolveLayoutContextFromShell(pageProps, shellLayouts) {
|
|
25
|
+
const context = {};
|
|
26
|
+
if (pageProps.params !== void 0) {
|
|
27
|
+
context.params = pageProps.params;
|
|
28
|
+
}
|
|
29
|
+
if (pageProps.query !== void 0) {
|
|
30
|
+
context.query = pageProps.query;
|
|
31
|
+
}
|
|
32
|
+
for (const entry of shellLayouts) {
|
|
33
|
+
if (entry.props !== void 0 && Object.prototype.hasOwnProperty.call(entry.props, "locals")) {
|
|
34
|
+
context.locals = entry.props.locals;
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return context;
|
|
39
|
+
}
|
|
40
|
+
function resolveLayoutEntryProps(entry, context) {
|
|
41
|
+
const shellProps = context.locals ? { locals: context.locals } : {};
|
|
42
|
+
if (!entry.props) {
|
|
43
|
+
return shellProps;
|
|
44
|
+
}
|
|
45
|
+
return {
|
|
46
|
+
...shellProps,
|
|
47
|
+
...entry.props(context)
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function assertComposablePage(Page) {
|
|
51
|
+
if (typeof Page !== "function") {
|
|
52
|
+
throw new TypeError("[ecopages] Expected a function page component for layout composition.");
|
|
53
|
+
}
|
|
54
|
+
return Page;
|
|
55
|
+
}
|
|
56
|
+
function composeLayoutPageTree(Page, pageProps, options) {
|
|
57
|
+
const { createElement } = resolveReact(options);
|
|
58
|
+
const context = options?.context ?? resolveLayoutContext(pageProps);
|
|
59
|
+
const pageElement = createElement(Page, pageProps);
|
|
60
|
+
const layoutEntries = Page.config?.layoutEntries;
|
|
61
|
+
if (layoutEntries && layoutEntries.length > 0) {
|
|
62
|
+
return [...layoutEntries].reverse().reduce((children, entry) => {
|
|
63
|
+
const layoutProps = resolveLayoutEntryProps(entry, context);
|
|
64
|
+
return createElement(toReactComponent(entry.component), layoutProps, children);
|
|
65
|
+
}, pageElement);
|
|
66
|
+
}
|
|
67
|
+
const layouts = Page.config?.layouts;
|
|
68
|
+
if (layouts && layouts.length > 0) {
|
|
69
|
+
const layoutProps = context.locals ? { locals: context.locals } : {};
|
|
70
|
+
return [...layouts].reverse().reduce(
|
|
71
|
+
(children, Layout) => createElement(toReactComponent(Layout), layoutProps, children),
|
|
72
|
+
pageElement
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
return pageElement;
|
|
76
|
+
}
|
|
77
|
+
function composeLayoutPageTreeFromShell(Page, pageProps, shellLayouts, options) {
|
|
78
|
+
const { createElement } = resolveReact(options);
|
|
79
|
+
if (shellLayouts.length === 0) {
|
|
80
|
+
return composeLayoutPageTree(Page, pageProps, options);
|
|
81
|
+
}
|
|
82
|
+
const pageElement = createElement(Page, pageProps);
|
|
83
|
+
return [...shellLayouts].reverse().reduce((children, entry) => {
|
|
84
|
+
return createElement(toReactComponent(entry.component), entry.props ?? {}, children);
|
|
85
|
+
}, pageElement);
|
|
86
|
+
}
|
|
87
|
+
function normalizePageLayoutComponents(layouts) {
|
|
88
|
+
return layouts && layouts.length > 0 ? layouts : [];
|
|
89
|
+
}
|
|
90
|
+
export {
|
|
91
|
+
assertComposablePage,
|
|
92
|
+
composeLayoutPageTree,
|
|
93
|
+
composeLayoutPageTreeFromShell,
|
|
94
|
+
normalizePageLayoutComponents,
|
|
95
|
+
resolveLayoutContext,
|
|
96
|
+
resolveLayoutContextFromShell,
|
|
97
|
+
resolveLayoutEntryProps
|
|
98
|
+
};
|
|
@@ -126,7 +126,7 @@ class ReactHmrStrategy extends HmrStrategy {
|
|
|
126
126
|
}
|
|
127
127
|
pageModuleRequiresLayoutRefresh(pageModule, filePath) {
|
|
128
128
|
return [pageModule.default?.config, pageModule.config].some((config) => {
|
|
129
|
-
return this.configContainsFile(
|
|
129
|
+
return (config?.layouts ?? []).some((layout) => this.configContainsFile(layout?.config, filePath));
|
|
130
130
|
});
|
|
131
131
|
}
|
|
132
132
|
async hasLayoutOwnedDependencyTarget(changedFilePath, requestedTargets) {
|
package/src/react-renderer.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* This module contains the React renderer
|
|
3
3
|
* @module
|
|
4
4
|
*/
|
|
5
|
-
import type { ComponentRenderInput, ComponentRenderResult, EcoComponent, EcoPageFile, IntegrationRendererRenderOptions, RouteRendererBody } from '@ecopages/core';
|
|
5
|
+
import type { ComponentRenderInput, ComponentRenderResult, EcoComponent, EcoPageFile, IntegrationRendererRenderOptions, PageMetadataProps, RouteRendererBody } from '@ecopages/core';
|
|
6
6
|
import { IntegrationRenderer, type HtmlDocumentContribution, type HtmlDocumentContributionContext, type PageBrowserGraphContributionContext, type RenderToResponseContext } from '@ecopages/core/route-renderer/integration-renderer';
|
|
7
7
|
import type { AssetDefinition, ProcessedAsset } from '@ecopages/core/services/asset-processing-service';
|
|
8
8
|
import type { ReactNode } from 'react';
|
|
@@ -12,10 +12,12 @@ import { ReactMdxConfigDependencyService } from './services/react-mdx-config-dep
|
|
|
12
12
|
import { ReactPageModuleService } from './services/react-page-module.service.js';
|
|
13
13
|
import { ReactPagePayloadService } from './services/react-page-payload.service.js';
|
|
14
14
|
import { ReactHydrationAssetService } from './services/react-hydration-asset.service.js';
|
|
15
|
+
import { type DocumentShellLayoutInput } from '@ecopages/core/route-renderer/orchestration/document-shell-render.service';
|
|
16
|
+
import { type ReactRuntime } from './layout-compose.js';
|
|
15
17
|
export type { ReactRendererConfig } from './react.types.js';
|
|
16
18
|
type ReactRuntimeModules = {
|
|
17
|
-
react:
|
|
18
|
-
reactDomServer:
|
|
19
|
+
react: ReactRuntime;
|
|
20
|
+
reactDomServer: typeof import('react-dom/server');
|
|
19
21
|
};
|
|
20
22
|
export type ReactRendererOptions = ConstructorParameters<typeof IntegrationRenderer>[0] & {
|
|
21
23
|
reactConfig?: ReactRendererConfig;
|
|
@@ -128,8 +130,8 @@ export declare class ReactRenderer extends IntegrationRenderer<ReactNode> {
|
|
|
128
130
|
* @returns Serialized component HTML with resolved child markup preserved.
|
|
129
131
|
*/
|
|
130
132
|
private renderComponentHtml;
|
|
133
|
+
private toReactNode;
|
|
131
134
|
/**
|
|
132
|
-
* Restores raw child HTML that was temporarily replaced by a token during React SSR.
|
|
133
135
|
*
|
|
134
136
|
* Queued foreign-subtree resolution may render children through a fragment path before all
|
|
135
137
|
* nested integration tokens are resolved. When that happens, React must never see
|
|
@@ -204,6 +206,25 @@ export declare class ReactRenderer extends IntegrationRenderer<ReactNode> {
|
|
|
204
206
|
dependencies?: AssetDefinition[];
|
|
205
207
|
assets?: ProcessedAsset[];
|
|
206
208
|
}>;
|
|
209
|
+
/**
|
|
210
|
+
* Composes page and layout tiers as one React element tree for SSR.
|
|
211
|
+
*/
|
|
212
|
+
private composeReactLayoutPageChildren;
|
|
213
|
+
private composeReactLayoutPageTree;
|
|
214
|
+
private shouldUseUnifiedReactLayoutComposition;
|
|
215
|
+
protected renderPageWithDocumentShell(input: {
|
|
216
|
+
page: {
|
|
217
|
+
component: EcoComponent;
|
|
218
|
+
props: Record<string, unknown>;
|
|
219
|
+
};
|
|
220
|
+
layout?: DocumentShellLayoutInput;
|
|
221
|
+
layouts?: DocumentShellLayoutInput[];
|
|
222
|
+
htmlTemplate: EcoComponent;
|
|
223
|
+
metadata: PageMetadataProps;
|
|
224
|
+
pageProps: Record<string, unknown>;
|
|
225
|
+
documentProps?: Record<string, unknown>;
|
|
226
|
+
transformDocumentHtml?: (html: string) => string;
|
|
227
|
+
}): Promise<string>;
|
|
207
228
|
/**
|
|
208
229
|
* Renders a full route response for the filesystem page pipeline.
|
|
209
230
|
*
|
|
@@ -215,6 +236,12 @@ export declare class ReactRenderer extends IntegrationRenderer<ReactNode> {
|
|
|
215
236
|
render({ params, query, props, locals, pageLocals, metadata, Page, Layout, HtmlTemplate, pageProps, }: IntegrationRendererRenderOptions<ReactNode>): Promise<RouteRendererBody>;
|
|
216
237
|
protected getHtmlDocumentContributions(options: HtmlDocumentContributionContext<ReactNode>): HtmlDocumentContribution[] | undefined;
|
|
217
238
|
protected getDocumentAttributes(): Record<string, string> | undefined;
|
|
239
|
+
protected renderViewWithDocumentShell<P>(input: {
|
|
240
|
+
view: EcoComponent<P>;
|
|
241
|
+
props: P;
|
|
242
|
+
ctx: RenderToResponseContext;
|
|
243
|
+
layout?: EcoComponent;
|
|
244
|
+
}): Promise<Response>;
|
|
218
245
|
/**
|
|
219
246
|
* Renders an arbitrary React view through the application's HTML shell.
|
|
220
247
|
*
|
package/src/react-renderer.js
CHANGED
|
@@ -3,8 +3,10 @@ import {
|
|
|
3
3
|
} from "@ecopages/core/route-renderer/integration-renderer";
|
|
4
4
|
import { RESOLVED_ASSETS_DIR } from "@ecopages/core/constants";
|
|
5
5
|
import { ECO_DOCUMENT_OWNER_ATTRIBUTE } from "@ecopages/core/router/navigation-coordinator";
|
|
6
|
+
import { ensurePageConfigLayouts } from "@ecopages/core/eco/page-layout-normalization";
|
|
6
7
|
import { createRequire } from "node:module";
|
|
7
8
|
import path from "node:path";
|
|
9
|
+
import { isValidElement } from "react";
|
|
8
10
|
import { REACT_PLUGIN_NAME } from "./react.constants.js";
|
|
9
11
|
import { hasSingleRootElement } from "./utils/html-boundary.js";
|
|
10
12
|
import { ReactBundleService } from "./services/react-bundle.service.js";
|
|
@@ -13,6 +15,16 @@ import { ReactMdxConfigDependencyService } from "./services/react-mdx-config-dep
|
|
|
13
15
|
import { ReactPageModuleService } from "./services/react-page-module.service.js";
|
|
14
16
|
import { ReactPagePayloadService } from "./services/react-page-payload.service.js";
|
|
15
17
|
import { getReactIslandComponentKey, ReactHydrationAssetService } from "./services/react-hydration-asset.service.js";
|
|
18
|
+
import {
|
|
19
|
+
composeDocumentShell,
|
|
20
|
+
renderPageDocumentShell
|
|
21
|
+
} from "@ecopages/core/route-renderer/orchestration/document-shell-render.service";
|
|
22
|
+
import {
|
|
23
|
+
composeLayoutPageTree,
|
|
24
|
+
composeLayoutPageTreeFromShell,
|
|
25
|
+
assertComposablePage,
|
|
26
|
+
resolveLayoutContextFromShell
|
|
27
|
+
} from "./layout-compose.js";
|
|
16
28
|
class ReactRenderError extends Error {
|
|
17
29
|
constructor(message) {
|
|
18
30
|
super(message);
|
|
@@ -212,6 +224,13 @@ class ReactRenderer extends IntegrationRenderer {
|
|
|
212
224
|
reactDomServer.renderToString(react.createElement(this.asReactComponent(input.component), input.props))
|
|
213
225
|
);
|
|
214
226
|
}
|
|
227
|
+
if (isValidElement(input.children)) {
|
|
228
|
+
return this.normalizeUnresolvedMarkerArtifactHtml(
|
|
229
|
+
reactDomServer.renderToString(
|
|
230
|
+
react.createElement(this.asReactComponent(input.component), input.props, input.children)
|
|
231
|
+
)
|
|
232
|
+
);
|
|
233
|
+
}
|
|
215
234
|
const resolvedChildHtml = typeof input.children === "string" ? input.children : String(input.children ?? "");
|
|
216
235
|
const rawChildrenToken = `__ECO_RAW_HTML_CHILD_${context.componentInstanceId ?? "component"}__`;
|
|
217
236
|
if (runtimeContext) {
|
|
@@ -223,8 +242,13 @@ class ReactRenderer extends IntegrationRenderer {
|
|
|
223
242
|
);
|
|
224
243
|
return this.normalizeUnresolvedMarkerArtifactHtml(html.split(rawChildrenToken).join(resolvedChildHtml));
|
|
225
244
|
}
|
|
245
|
+
toReactNode(children) {
|
|
246
|
+
if (children === null || typeof children === "string" || typeof children === "number" || typeof children === "boolean" || isValidElement(children) || Array.isArray(children)) {
|
|
247
|
+
return children;
|
|
248
|
+
}
|
|
249
|
+
throw new TypeError(`[ecopages] ${this.name} renderer expected a React node child.`);
|
|
250
|
+
}
|
|
226
251
|
/**
|
|
227
|
-
* Restores raw child HTML that was temporarily replaced by a token during React SSR.
|
|
228
252
|
*
|
|
229
253
|
* Queued foreign-subtree resolution may render children through a fragment path before all
|
|
230
254
|
* nested integration tokens are resolved. When that happens, React must never see
|
|
@@ -252,7 +276,7 @@ class ReactRenderer extends IntegrationRenderer {
|
|
|
252
276
|
}
|
|
253
277
|
const { react, reactDomServer } = this.getReactRuntimeModules();
|
|
254
278
|
let html = this.normalizeUnresolvedMarkerArtifactHtml(
|
|
255
|
-
reactDomServer.renderToString(react.createElement(react.Fragment, null, children))
|
|
279
|
+
reactDomServer.renderToString(react.createElement(react.Fragment, null, this.toReactNode(children)))
|
|
256
280
|
);
|
|
257
281
|
html = this.restoreRuntimeChildHtml(html, runtimeContext);
|
|
258
282
|
html = await this.foreignSubtreeExecutionService.resolveQueuedTokens(
|
|
@@ -441,6 +465,7 @@ class ReactRenderer extends IntegrationRenderer {
|
|
|
441
465
|
const reactModule = pageModule;
|
|
442
466
|
const { default: Page, getMetadata, config } = reactModule;
|
|
443
467
|
if (this.pageModuleService.isMdxFile(file) && config) {
|
|
468
|
+
ensurePageConfigLayouts(config);
|
|
444
469
|
Page.config = config;
|
|
445
470
|
}
|
|
446
471
|
return {
|
|
@@ -483,6 +508,77 @@ class ReactRenderer extends IntegrationRenderer {
|
|
|
483
508
|
);
|
|
484
509
|
}
|
|
485
510
|
}
|
|
511
|
+
/**
|
|
512
|
+
* Composes page and layout tiers as one React element tree for SSR.
|
|
513
|
+
*/
|
|
514
|
+
async composeReactLayoutPageChildren(page, context) {
|
|
515
|
+
const pageComponent = assertComposablePage(page.component);
|
|
516
|
+
const shellEntries = context.layouts.map((layout) => ({
|
|
517
|
+
component: layout.component,
|
|
518
|
+
props: layout.props
|
|
519
|
+
}));
|
|
520
|
+
const { react, reactDomServer } = this.getReactRuntimeModules();
|
|
521
|
+
const tree = this.composeReactLayoutPageTree(pageComponent, page.props, shellEntries, {
|
|
522
|
+
context: resolveLayoutContextFromShell(page.props, shellEntries),
|
|
523
|
+
react
|
|
524
|
+
});
|
|
525
|
+
const runtimeInput = {
|
|
526
|
+
component: page.component,
|
|
527
|
+
props: page.props,
|
|
528
|
+
integrationContext: {
|
|
529
|
+
rendererCache: context.rendererCache
|
|
530
|
+
}
|
|
531
|
+
};
|
|
532
|
+
const runtimeContext = this.getQueuedForeignSubtreeResolutionContext(runtimeInput);
|
|
533
|
+
let html = this.normalizeUnresolvedMarkerArtifactHtml(reactDomServer.renderToString(tree));
|
|
534
|
+
const resolved = await this.resolveReactQueuedForeignSubtreeHtml(html, runtimeContext);
|
|
535
|
+
const primaryRender = {
|
|
536
|
+
html: resolved.html,
|
|
537
|
+
canAttachAttributes: hasSingleRootElement(resolved.html),
|
|
538
|
+
rootTag: this.getRootTagName(resolved.html),
|
|
539
|
+
integrationName: this.name,
|
|
540
|
+
assets: resolved.assets.length > 0 ? resolved.assets : void 0
|
|
541
|
+
};
|
|
542
|
+
return {
|
|
543
|
+
children: resolved.html,
|
|
544
|
+
layoutRenders: [],
|
|
545
|
+
primaryRender
|
|
546
|
+
};
|
|
547
|
+
}
|
|
548
|
+
composeReactLayoutPageTree(Page, pageProps, shellEntries, options) {
|
|
549
|
+
const hasNormalizedLayouts = Boolean(Page.config?.layoutEntries?.length) || Boolean(Page.config?.layouts?.length);
|
|
550
|
+
if (hasNormalizedLayouts) {
|
|
551
|
+
return composeLayoutPageTree(Page, pageProps, options);
|
|
552
|
+
}
|
|
553
|
+
return composeLayoutPageTreeFromShell(Page, pageProps, shellEntries, options);
|
|
554
|
+
}
|
|
555
|
+
shouldUseUnifiedReactLayoutComposition(page, shellLayouts) {
|
|
556
|
+
if (!this.isReactManagedComponent(page)) {
|
|
557
|
+
return false;
|
|
558
|
+
}
|
|
559
|
+
const composablePage = assertComposablePage(page);
|
|
560
|
+
const configLayouts = composablePage.config?.layouts ?? composablePage.config?.layoutEntries?.map((entry) => entry.component) ?? [];
|
|
561
|
+
const layoutComponents = shellLayouts.length > 0 ? shellLayouts.map((layout) => layout.component) : configLayouts;
|
|
562
|
+
if (layoutComponents.length === 0) {
|
|
563
|
+
return false;
|
|
564
|
+
}
|
|
565
|
+
return layoutComponents.every((component) => this.isReactManagedComponent(component));
|
|
566
|
+
}
|
|
567
|
+
async renderPageWithDocumentShell(input) {
|
|
568
|
+
const shellLayouts = input.layouts ?? (input.layout ? [input.layout] : []);
|
|
569
|
+
const composeChildren = this.shouldUseUnifiedReactLayoutComposition(input.page.component, shellLayouts) ? (context) => this.composeReactLayoutPageChildren(input.page, context) : void 0;
|
|
570
|
+
return renderPageDocumentShell(
|
|
571
|
+
{
|
|
572
|
+
renderComponentWithForeignChildren: (renderInput) => this.renderComponentWithForeignChildren(renderInput),
|
|
573
|
+
appendProcessedDependencies: (...assetGroups) => this.appendProcessedDependencies(...assetGroups)
|
|
574
|
+
},
|
|
575
|
+
{
|
|
576
|
+
...input,
|
|
577
|
+
composeChildren
|
|
578
|
+
},
|
|
579
|
+
this.DOC_TYPE
|
|
580
|
+
);
|
|
581
|
+
}
|
|
486
582
|
/**
|
|
487
583
|
* Renders a full route response for the filesystem page pipeline.
|
|
488
584
|
*
|
|
@@ -547,6 +643,59 @@ class ReactRenderer extends IntegrationRenderer {
|
|
|
547
643
|
getDocumentAttributes() {
|
|
548
644
|
return this.getRouterDocumentAttributes();
|
|
549
645
|
}
|
|
646
|
+
async renderViewWithDocumentShell(input) {
|
|
647
|
+
const normalizedProps = input.props ?? {};
|
|
648
|
+
if (input.ctx.partial) {
|
|
649
|
+
const { react, reactDomServer } = this.getReactRuntimeModules();
|
|
650
|
+
const ViewComponent = this.asReactComponent(input.view);
|
|
651
|
+
return this.renderPartialViewResponse({
|
|
652
|
+
...input,
|
|
653
|
+
renderInline: async () => await reactDomServer.renderToReadableStream(react.createElement(ViewComponent, normalizedProps))
|
|
654
|
+
});
|
|
655
|
+
}
|
|
656
|
+
await this.prepareViewDependencies(input.view, input.layout);
|
|
657
|
+
if (!input.ctx.partial) {
|
|
658
|
+
await this.appendHydrationAssetsForFile(input.view.config?.__eco?.file);
|
|
659
|
+
}
|
|
660
|
+
const HtmlTemplate = await this.getHtmlTemplate();
|
|
661
|
+
const metadata = await this.resolveViewMetadata(input.view, input.props);
|
|
662
|
+
const serializedPageProps = this.pagePayloadService.buildSerializedPageProps({
|
|
663
|
+
pageProps: normalizedProps,
|
|
664
|
+
params: {},
|
|
665
|
+
query: {},
|
|
666
|
+
safeLocals: this.pagePayloadService.getSerializableLocals(void 0, this.getComponentRequires(input.view))
|
|
667
|
+
});
|
|
668
|
+
const shellLayouts = input.layout ? [{ component: input.layout, props: {} }] : [];
|
|
669
|
+
const composeChildren = this.shouldUseUnifiedReactLayoutComposition(input.view, shellLayouts) ? (context) => this.composeReactLayoutPageChildren({ component: input.view, props: normalizedProps }, context) : void 0;
|
|
670
|
+
const { documentHtml } = await composeDocumentShell(
|
|
671
|
+
{
|
|
672
|
+
renderComponentWithForeignChildren: (renderInput) => this.renderComponentWithForeignChildren(renderInput),
|
|
673
|
+
appendProcessedDependencies: (...assetGroups) => this.appendProcessedDependencies(...assetGroups)
|
|
674
|
+
},
|
|
675
|
+
{
|
|
676
|
+
primaryComponent: input.view,
|
|
677
|
+
primaryProps: normalizedProps,
|
|
678
|
+
layout: input.layout ? {
|
|
679
|
+
component: input.layout,
|
|
680
|
+
props: {}
|
|
681
|
+
} : void 0,
|
|
682
|
+
layouts: shellLayouts.length > 0 ? shellLayouts : void 0,
|
|
683
|
+
composeChildren,
|
|
684
|
+
htmlTemplate: HtmlTemplate,
|
|
685
|
+
documentProps: {
|
|
686
|
+
metadata,
|
|
687
|
+
pageProps: serializedPageProps
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
);
|
|
691
|
+
const html = await this.finalizeResolvedHtml({
|
|
692
|
+
html: `${this.DOC_TYPE}${documentHtml}`,
|
|
693
|
+
partial: false,
|
|
694
|
+
documentAttributes: this.getRouterDocumentAttributes(),
|
|
695
|
+
htmlContributions: this.buildNonReactDocumentContributions(HtmlTemplate, serializedPageProps)
|
|
696
|
+
});
|
|
697
|
+
return this.createHtmlResponse(html, input.ctx);
|
|
698
|
+
}
|
|
550
699
|
/**
|
|
551
700
|
* Renders an arbitrary React view through the application's HTML shell.
|
|
552
701
|
*
|
|
@@ -557,50 +706,13 @@ class ReactRenderer extends IntegrationRenderer {
|
|
|
557
706
|
*/
|
|
558
707
|
async renderToResponse(view, props, ctx) {
|
|
559
708
|
try {
|
|
560
|
-
const
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
return this.renderPartialViewResponse({
|
|
567
|
-
view,
|
|
568
|
-
props,
|
|
569
|
-
ctx,
|
|
570
|
-
renderInline: async () => await reactDomServer.renderToReadableStream(
|
|
571
|
-
react.createElement(ViewComponent, normalizedProps)
|
|
572
|
-
)
|
|
573
|
-
});
|
|
574
|
-
}
|
|
575
|
-
const HtmlTemplate = await this.getHtmlTemplate();
|
|
576
|
-
const metadata = await this.resolveViewMetadata(view, props);
|
|
577
|
-
await this.prepareViewDependencies(view, Layout);
|
|
578
|
-
await this.appendHydrationAssetsForFile(viewConfig?.__eco?.file);
|
|
579
|
-
const viewRender = await this.renderComponentWithForeignChildren({
|
|
580
|
-
component: view,
|
|
581
|
-
props: normalizedProps
|
|
582
|
-
});
|
|
583
|
-
const layoutRender = Layout ? await this.renderComponentWithForeignChildren({
|
|
584
|
-
component: Layout,
|
|
585
|
-
props: {},
|
|
586
|
-
children: viewRender.html
|
|
587
|
-
}) : void 0;
|
|
588
|
-
const documentRender = await this.renderComponentWithForeignChildren({
|
|
589
|
-
component: HtmlTemplate,
|
|
590
|
-
props: {
|
|
591
|
-
metadata,
|
|
592
|
-
pageProps: normalizedProps
|
|
593
|
-
},
|
|
594
|
-
children: layoutRender?.html ?? viewRender.html
|
|
595
|
-
});
|
|
596
|
-
this.appendProcessedDependencies(viewRender.assets, layoutRender?.assets, documentRender.assets);
|
|
597
|
-
const transformedHtml = await this.finalizeResolvedHtml({
|
|
598
|
-
html: `${this.DOC_TYPE}${documentRender.html}`,
|
|
599
|
-
partial: false,
|
|
600
|
-
documentAttributes: this.getRouterDocumentAttributes(),
|
|
601
|
-
htmlContributions: this.buildNonReactDocumentContributions(HtmlTemplate, normalizedProps)
|
|
709
|
+
const layouts = view.config?.layouts;
|
|
710
|
+
return await this.renderViewWithDocumentShell({
|
|
711
|
+
view,
|
|
712
|
+
props,
|
|
713
|
+
ctx,
|
|
714
|
+
layout: layouts?.[layouts.length - 1]
|
|
602
715
|
});
|
|
603
|
-
return this.createHtmlResponse(transformedHtml, ctx);
|
|
604
716
|
} catch (error) {
|
|
605
717
|
throw this.createRenderError("Failed to render view", error);
|
|
606
718
|
}
|
package/src/react.plugin.js
CHANGED
|
@@ -9,7 +9,7 @@ import { ReactHmrStrategy } from "./react-hmr-strategy.js";
|
|
|
9
9
|
import { ReactRuntimeBundleService } from "./services/react-runtime-bundle.service.js";
|
|
10
10
|
import { ReactHmrPageMetadataCache } from "./services/react-hmr-page-metadata-cache.js";
|
|
11
11
|
import { createReactMdxLoaderPlugin } from "./utils/react-mdx-loader-plugin.js";
|
|
12
|
-
import { appendMdxExtensions, resolveMdxCompilerOptions } from "@ecopages/mdx
|
|
12
|
+
import { appendMdxExtensions, resolveMdxCompilerOptions } from "@ecopages/mdx/core";
|
|
13
13
|
import { ClientGraphBoundaryCache } from "./utils/client-graph-boundary-cache.js";
|
|
14
14
|
const appLogger = new Logger("[ReactPlugin]");
|
|
15
15
|
const PLUGIN_NAME = REACT_PLUGIN_NAME;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Escapes page data JSON for safe embedding in HTML script bodies.
|
|
3
|
+
*/
|
|
4
|
+
export declare function escapePageDataJson(pageProps: Record<string, unknown> | undefined): string;
|
|
5
|
+
/**
|
|
6
|
+
* Emits the canonical `__ECO_PAGE_DATA__` bootstrap script tag.
|
|
7
|
+
*/
|
|
8
|
+
export declare function serializePageDataScript(pageProps: Record<string, unknown> | undefined): string;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
function escapePageDataJson(pageProps) {
|
|
2
|
+
return JSON.stringify(pageProps ?? {}).replace(/</g, "\\u003c");
|
|
3
|
+
}
|
|
4
|
+
function serializePageDataScript(pageProps) {
|
|
5
|
+
return `<script id="__ECO_PAGE_DATA__" type="application/json">${escapePageDataJson(pageProps)}<\/script>`;
|
|
6
|
+
}
|
|
7
|
+
export {
|
|
8
|
+
escapePageDataJson,
|
|
9
|
+
serializePageDataScript
|
|
10
|
+
};
|
|
@@ -23,13 +23,11 @@ class ReactMdxConfigDependencyService {
|
|
|
23
23
|
}
|
|
24
24
|
createOwnedConfigComponents(pagePath, config) {
|
|
25
25
|
const components = [];
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
);
|
|
32
|
-
components.push({ config: layoutConfig });
|
|
26
|
+
for (const layout of config?.layouts ?? []) {
|
|
27
|
+
if (layout?.config?.dependencies) {
|
|
28
|
+
const layoutConfig = this.config.pageModuleService.ensureConfigFileMetadata(layout.config, pagePath);
|
|
29
|
+
components.push({ config: layoutConfig });
|
|
30
|
+
}
|
|
33
31
|
}
|
|
34
32
|
if (config?.dependencies) {
|
|
35
33
|
components.push({
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { LocalsAccessError } from "@ecopages/core/errors";
|
|
2
|
+
import { serializePageDataScript } from "../serialize-page-data-script.js";
|
|
2
3
|
class ReactPagePayloadService {
|
|
3
4
|
/**
|
|
4
5
|
* Creates the canonical page-props payload used by router hydration.
|
|
@@ -8,8 +9,7 @@ class ReactPagePayloadService {
|
|
|
8
9
|
* can read one shared document payload consistently.
|
|
9
10
|
*/
|
|
10
11
|
buildRouterPageDataScript(pageProps) {
|
|
11
|
-
|
|
12
|
-
return `<script id="__ECO_PAGE_DATA__" type="application/json">${safeJson}<\/script>`;
|
|
12
|
+
return serializePageDataScript(pageProps);
|
|
13
13
|
}
|
|
14
14
|
/**
|
|
15
15
|
* Builds the serialized page-props payload embedded into the final HTML.
|