@ecopages/react 0.2.0-beta.25 → 0.2.0-beta.27

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.
Files changed (98) hide show
  1. package/README.md +17 -9
  2. package/package.json +42 -34
  3. package/src/{services/react-bundle.service.d.ts → bundling/bundle.d.ts} +7 -7
  4. package/src/{services/react-bundle.service.js → bundling/bundle.js} +9 -11
  5. package/src/{utils → bundling}/discover-layout-runtime-modules.js +2 -2
  6. package/src/{utils/react-runtime-alias-map.d.ts → bundling/runtime-alias-map.d.ts} +3 -3
  7. package/src/{services/react-runtime-bundle.service.d.ts → bundling/runtime-bundle.d.ts} +5 -5
  8. package/src/{services/react-runtime-bundle.service.js → bundling/runtime-bundle.js} +4 -4
  9. package/src/bundling/runtime-mode.d.ts +9 -0
  10. package/src/bundling/runtime-mode.js +6 -0
  11. package/src/{utils/client-graph-ast-transform.d.ts → client-graph/ast-transform.d.ts} +1 -1
  12. package/src/{utils/client-graph-ast-transform.js → client-graph/ast-transform.js} +1 -1
  13. package/src/{utils/client-graph-boundary-cache.d.ts → client-graph/boundary-cache.d.ts} +0 -5
  14. package/src/{utils/client-graph-boundary-cache.js → client-graph/boundary-cache.js} +1 -3
  15. package/src/{utils/client-graph-boundary-plugin.d.ts → client-graph/boundary-plugin.d.ts} +1 -1
  16. package/src/{utils/client-graph-boundary-plugin.js → client-graph/boundary-plugin.js} +3 -3
  17. package/src/{utils/client-graph-reachability.d.ts → client-graph/specifier-classification.d.ts} +1 -1
  18. package/src/{react-hmr-strategy.d.ts → hmr/hmr-strategy.d.ts} +3 -6
  19. package/src/{react-hmr-strategy.js → hmr/hmr-strategy.js} +11 -20
  20. package/src/{services/react-hmr-page-metadata-cache.d.ts → hmr/page-metadata-cache.d.ts} +5 -1
  21. package/src/{services/react-hmr-page-metadata-cache.js → hmr/page-metadata-cache.js} +8 -2
  22. package/src/{services/react-hydration-asset.service.d.ts → hydration/hydration-asset.d.ts} +33 -33
  23. package/src/{services/react-hydration-asset.service.js → hydration/hydration-asset.js} +45 -52
  24. package/src/{utils → hydration}/hydration-scripts.d.ts +33 -41
  25. package/src/hydration/hydration-scripts.js +353 -0
  26. package/src/{utils → hydration}/hydration-scripts.test.browser.js +6 -4
  27. package/src/hydration/page-data-manifest.d.ts +58 -0
  28. package/src/hydration/page-data-manifest.js +53 -0
  29. package/src/hydration/page-data-reader.d.ts +31 -0
  30. package/src/hydration/page-data-reader.js +64 -0
  31. package/src/hydration/page-payload.d.ts +67 -0
  32. package/src/hydration/page-payload.js +104 -0
  33. package/src/hydration/serialize-page-data-script.d.ts +27 -0
  34. package/src/hydration/serialize-page-data-script.js +31 -0
  35. package/src/{services/react-mdx-config-dependency.service.d.ts → mdx/mdx-config-dependency.d.ts} +5 -5
  36. package/src/{services/react-mdx-config-dependency.service.js → mdx/mdx-config-dependency.js} +3 -3
  37. package/src/{services/react-page-module.service.d.ts → page/page-module.d.ts} +4 -7
  38. package/src/{services/react-page-module.service.js → page/page-module.js} +5 -9
  39. package/src/{react.plugin.d.ts → plugin/react.plugin.d.ts} +7 -3
  40. package/src/{react.plugin.js → plugin/react.plugin.js} +32 -30
  41. package/src/{react.types.d.ts → plugin/react.types.d.ts} +23 -8
  42. package/src/render/component-ownership.d.ts +50 -0
  43. package/src/render/component-ownership.js +23 -0
  44. package/src/render/component-ssr.d.ts +113 -0
  45. package/src/render/component-ssr.js +202 -0
  46. package/src/render/errors.d.ts +16 -0
  47. package/src/render/errors.js +18 -0
  48. package/src/{layout-compose.d.ts → render/layout-compose.d.ts} +8 -0
  49. package/src/{layout-compose.js → render/layout-compose.js} +14 -6
  50. package/src/render/react-renderer.d.ts +137 -0
  51. package/src/render/react-renderer.js +444 -0
  52. package/src/render/react-runtime.d.ts +13 -0
  53. package/src/render/react-runtime.js +21 -0
  54. package/src/render/unified-layout-composition.d.ts +53 -0
  55. package/src/render/unified-layout-composition.js +89 -0
  56. package/src/react-renderer.d.ts +0 -254
  57. package/src/react-renderer.js +0 -726
  58. package/src/serialize-page-data-script.d.ts +0 -8
  59. package/src/serialize-page-data-script.js +0 -10
  60. package/src/services/pages-index.d.ts +0 -64
  61. package/src/services/pages-index.js +0 -73
  62. package/src/services/react-page-payload.service.d.ts +0 -46
  63. package/src/services/react-page-payload.service.js +0 -67
  64. package/src/utils/hydration-scripts.js +0 -371
  65. /package/src/{utils → app}/client-only.d.ts +0 -0
  66. /package/src/{utils → app}/client-only.js +0 -0
  67. /package/src/{utils → app}/dynamic.d.ts +0 -0
  68. /package/src/{utils → app}/dynamic.js +0 -0
  69. /package/src/{utils → app}/dynamic.test.browser.d.ts +0 -0
  70. /package/src/{utils → app}/dynamic.test.browser.js +0 -0
  71. /package/src/{utils → bundling}/discover-layout-runtime-modules.d.ts +0 -0
  72. /package/src/{utils/react-runtime-alias-map.js → bundling/runtime-alias-map.js} +0 -0
  73. /package/src/{utils/react-dom-runtime-interop-plugin.d.ts → bundling/runtime-interop-plugin.d.ts} +0 -0
  74. /package/src/{utils/react-dom-runtime-interop-plugin.js → bundling/runtime-interop-plugin.js} +0 -0
  75. /package/src/{utils/react-plugin-runtime-modules.d.ts → bundling/runtime-modules.d.ts} +0 -0
  76. /package/src/{utils/react-plugin-runtime-modules.js → bundling/runtime-modules.js} +0 -0
  77. /package/src/{utils → client-graph}/component-config-traversal.d.ts +0 -0
  78. /package/src/{utils → client-graph}/component-config-traversal.js +0 -0
  79. /package/src/{utils → client-graph}/declared-modules.d.ts +0 -0
  80. /package/src/{utils → client-graph}/declared-modules.js +0 -0
  81. /package/src/{utils → client-graph}/reachability-analyzer.d.ts +0 -0
  82. /package/src/{utils → client-graph}/reachability-analyzer.js +0 -0
  83. /package/src/{utils/client-graph-reachability.js → client-graph/specifier-classification.js} +0 -0
  84. /package/src/{declarations.d.ts → contracts/declarations.d.ts} +0 -0
  85. /package/src/{router-adapter.d.ts → contracts/router-adapter.d.ts} +0 -0
  86. /package/src/{router-adapter.js → contracts/router-adapter.js} +0 -0
  87. /package/src/{utils → hmr}/hmr-scripts.d.ts +0 -0
  88. /package/src/{utils → hmr}/hmr-scripts.js +0 -0
  89. /package/src/{utils → hydration}/hydration-scripts.test.browser.d.ts +0 -0
  90. /package/src/{utils/react-mdx-loader-plugin.d.ts → mdx/mdx-loader-plugin.d.ts} +0 -0
  91. /package/src/{utils/react-mdx-loader-plugin.js → mdx/mdx-loader-plugin.js} +0 -0
  92. /package/src/{react.constants.d.ts → plugin/react.constants.d.ts} +0 -0
  93. /package/src/{react.constants.js → plugin/react.constants.js} +0 -0
  94. /package/src/{react.types.js → plugin/react.types.js} +0 -0
  95. /package/src/{eco-embed.d.ts → render/eco-embed.d.ts} +0 -0
  96. /package/src/{eco-embed.js → render/eco-embed.js} +0 -0
  97. /package/src/{utils → render}/html-boundary.d.ts +0 -0
  98. /package/src/{utils → render}/html-boundary.js +0 -0
@@ -0,0 +1,444 @@
1
+ import {
2
+ IntegrationRenderer
3
+ } from "@ecopages/core/route-renderer/integration-renderer";
4
+ import { RESOLVED_ASSETS_DIR } from "@ecopages/core/constants";
5
+ import { ECO_DOCUMENT_OWNER_ATTRIBUTE } from "@ecopages/core/router/navigation-coordinator";
6
+ import { ensurePageConfigLayouts } from "@ecopages/core/eco/page-layout-normalization";
7
+ import { REACT_PLUGIN_NAME } from "../plugin/react.constants.js";
8
+ import { BundleService } from "../bundling/bundle.js";
9
+ import { HmrPageMetadataCache } from "../hmr/page-metadata-cache.js";
10
+ import { MdxConfigDependencyService } from "../mdx/mdx-config-dependency.js";
11
+ import { PageModuleService } from "../page/page-module.js";
12
+ import { PagePayloadService } from "../hydration/page-payload.js";
13
+ import { HydrationAssetService } from "../hydration/hydration-asset.js";
14
+ import {
15
+ composeDocumentShell,
16
+ renderPageDocumentShell
17
+ } from "@ecopages/core/route-renderer/orchestration/document-shell-render.service";
18
+ import { asReactComponent, getComponentRequires, isReactManagedComponent } from "./component-ownership.js";
19
+ import {
20
+ createForeignSubtreeRuntimeContext,
21
+ renderForeignComponentWithSerializedHtml,
22
+ renderReactManagedComponent,
23
+ renderReactQueuedForeignSubtreeChildren
24
+ } from "./component-ssr.js";
25
+ import { BundleError, ReactRenderError } from "./errors.js";
26
+ import { resolveReactRuntimeModules } from "./react-runtime.js";
27
+ import { composeReactLayoutPageChildren, resolveComposeChildren } from "./unified-layout-composition.js";
28
+ import { BundleError as BundleError2, ReactRenderError as ReactRenderError2 } from "./errors.js";
29
+ class ReactRenderer extends IntegrationRenderer {
30
+ name = REACT_PLUGIN_NAME;
31
+ componentDirectory = RESOLVED_ASSETS_DIR;
32
+ reactRuntimeModules;
33
+ routerAdapter;
34
+ mdxCompilerOptions;
35
+ mdxExtensions;
36
+ hmrPageMetadataCache;
37
+ /**
38
+ * When true, always emit page browser graph / hydration assets for React pages.
39
+ *
40
+ * Mapped from the public `explicitGraph` plugin option. Does not disable the
41
+ * client-graph AST boundary.
42
+ */
43
+ forceBrowserGraph;
44
+ /** @internal */
45
+ bundleService;
46
+ /** @internal */
47
+ pageModuleService;
48
+ /** @internal */
49
+ hydrationAssetService;
50
+ /** @internal */
51
+ pagePayloadService;
52
+ /** @internal */
53
+ mdxConfigDependencyService;
54
+ constructor(options) {
55
+ const { reactConfig, ...rendererOptions } = options;
56
+ super(rendererOptions);
57
+ this.routerAdapter = reactConfig?.routerAdapter;
58
+ this.mdxCompilerOptions = reactConfig?.mdxCompilerOptions;
59
+ this.mdxExtensions = reactConfig?.mdxExtensions ?? [".mdx"];
60
+ this.hmrPageMetadataCache = reactConfig?.hmrPageMetadataCache;
61
+ this.forceBrowserGraph = reactConfig?.forceBrowserGraph ?? false;
62
+ this.bundleService = new BundleService({
63
+ rootDir: this.appConfig.rootDir,
64
+ appConfig: this.appConfig,
65
+ hostIntegrationName: this.name,
66
+ routerAdapter: this.routerAdapter,
67
+ runtimeModules: reactConfig?.runtimeModules,
68
+ mdxCompilerOptions: this.mdxCompilerOptions
69
+ });
70
+ this.pageModuleService = new PageModuleService({
71
+ layoutsDir: this.appConfig.absolutePaths.layoutsDir,
72
+ componentsDir: this.appConfig.absolutePaths.componentsDir,
73
+ mdxExtensions: this.mdxExtensions,
74
+ integrationName: this.name,
75
+ hasRouterAdapter: Boolean(this.routerAdapter)
76
+ });
77
+ this.hydrationAssetService = new HydrationAssetService({
78
+ srcDir: this.appConfig.srcDir,
79
+ routerAdapter: this.routerAdapter,
80
+ assetProcessingService: this.assetProcessingService,
81
+ bundleService: this.bundleService,
82
+ hmrPageMetadataCache: this.hmrPageMetadataCache
83
+ });
84
+ this.pagePayloadService = new PagePayloadService();
85
+ this.mdxConfigDependencyService = new MdxConfigDependencyService({
86
+ integrationName: this.name,
87
+ pageModuleService: this.pageModuleService,
88
+ assetProcessingService: this.assetProcessingService
89
+ });
90
+ }
91
+ getRouterDocumentAttributes() {
92
+ if (!this.routerAdapter) {
93
+ return void 0;
94
+ }
95
+ return {
96
+ [ECO_DOCUMENT_OWNER_ATTRIBUTE]: "react-router"
97
+ };
98
+ }
99
+ resolveReactRuntimeModules() {
100
+ return resolveReactRuntimeModules(this.appConfig.rootDir);
101
+ }
102
+ getReactRuntimeModules() {
103
+ this.reactRuntimeModules ??= this.resolveReactRuntimeModules();
104
+ return this.reactRuntimeModules;
105
+ }
106
+ /**
107
+ * Appends route hydration assets for a concrete page/view file to the current
108
+ * HTML transformer state.
109
+ */
110
+ async appendHydrationAssetsForFile(filePath) {
111
+ if (!filePath) {
112
+ return;
113
+ }
114
+ const pageBrowserGraph = await this.resolvePageBrowserGraphForFile(filePath);
115
+ this.mergePageBrowserGraphIntoPagePackage(pageBrowserGraph);
116
+ }
117
+ resolveReactQueuedForeignSubtreeHtml(html, runtimeContext) {
118
+ return this.resolveQueuedForeignSubtreeHtml(
119
+ html,
120
+ runtimeContext,
121
+ (children, currentRuntimeContext, queuedResolutionsByToken, resolveToken) => renderReactQueuedForeignSubtreeChildren({
122
+ children,
123
+ currentRuntimeContext,
124
+ queuedResolutionsByToken,
125
+ resolveToken,
126
+ runtime: this.getReactRuntimeModules(),
127
+ integrationName: this.name,
128
+ normalizeUnresolvedMarkerArtifactHtml: (value) => this.normalizeUnresolvedMarkerArtifactHtml(value),
129
+ resolveQueuedTokens: (value, tokens, resolve) => this.foreignSubtreeExecutionService.resolveQueuedTokens(value, tokens, resolve)
130
+ }),
131
+ "React"
132
+ );
133
+ }
134
+ /**
135
+ * Renders a React component for component-level orchestration.
136
+ *
137
+ * Behavior:
138
+ * - SSR always returns the component's own root HTML (no synthetic wrapper).
139
+ * - When an explicit component instance id is provided, a stable
140
+ * `data-eco-component-id` attribute is attached so island hydration can target it.
141
+ * - Without an explicit instance id, component renders remain plain SSR output.
142
+ * - When resolved child HTML is provided, that foreign subtree is treated as a pure SSR
143
+ * composition step and does not emit hydration assets for the parent wrapper.
144
+ *
145
+ * This preserves DOM shape for global CSS/layout selectors while keeping a
146
+ * deterministic mount target per component instance.
147
+ */
148
+ async renderComponent(input) {
149
+ const runtimeContext = this.getQueuedForeignSubtreeResolutionContext(input);
150
+ if (!isReactManagedComponent(input.component, this.name)) {
151
+ return renderForeignComponentWithSerializedHtml({
152
+ input,
153
+ runtimeContext,
154
+ integrationName: this.name,
155
+ canResolveAssets: typeof this.assetProcessingService?.processDependencies === "function",
156
+ processComponentDependencies: (components) => this.processComponentDependencies(components),
157
+ dedupeProcessedAssets: (assets) => this.htmlTransformer.dedupeProcessedAssets(assets),
158
+ getRootTagName: (html) => this.getRootTagName(html),
159
+ resolveQueuedForeignSubtreeHtml: (html, context) => this.resolveReactQueuedForeignSubtreeHtml(html, context)
160
+ });
161
+ }
162
+ return renderReactManagedComponent({
163
+ input,
164
+ runtimeContext,
165
+ runtime: this.getReactRuntimeModules(),
166
+ integrationName: this.name,
167
+ normalizeUnresolvedMarkerArtifactHtml: (html) => this.normalizeUnresolvedMarkerArtifactHtml(html),
168
+ resolveQueuedForeignSubtreeHtml: (html, context) => this.resolveReactQueuedForeignSubtreeHtml(html, context),
169
+ getRootTagName: (html) => this.getRootTagName(html),
170
+ dedupeProcessedAssets: (assets) => this.htmlTransformer.dedupeProcessedAssets(assets),
171
+ hydrationAssetService: this.hydrationAssetService,
172
+ canBuildIslandAssets: Boolean(this.assetProcessingService)
173
+ });
174
+ }
175
+ createForeignChildRuntime(options) {
176
+ return this.createQueuedForeignSubtreeExecutionRuntime({
177
+ renderInput: options.renderInput,
178
+ rendererCache: options.rendererCache,
179
+ createRuntimeContext: (integrationContext, rendererCache) => createForeignSubtreeRuntimeContext({
180
+ rendererCache,
181
+ componentInstanceScope: integrationContext.componentInstanceId
182
+ })
183
+ });
184
+ }
185
+ /**
186
+ * Checks if the given file path corresponds to an MDX file based on configured extensions.
187
+ * @param filePath - The file path to check
188
+ * @returns True if the file is an MDX file
189
+ */
190
+ isMdxFile(filePath) {
191
+ return this.pageModuleService.isMdxFile(filePath);
192
+ }
193
+ normalizeImportedPageFile(file, pageModule) {
194
+ const reactModule = pageModule;
195
+ const { default: Page, getMetadata, config } = reactModule;
196
+ if (this.pageModuleService.isMdxFile(file) && config) {
197
+ ensurePageConfigLayouts(config);
198
+ Page.config = config;
199
+ }
200
+ return {
201
+ ...pageModule,
202
+ default: Page,
203
+ getMetadata,
204
+ config
205
+ };
206
+ }
207
+ async collectPageBrowserGraphContribution(context) {
208
+ try {
209
+ const { file: pagePath, pageModule } = context;
210
+ const shouldHydrate = this.forceBrowserGraph ? true : this.pageModuleService.shouldHydratePage(pageModule);
211
+ if (!shouldHydrate) {
212
+ return { assets: [] };
213
+ }
214
+ const isMdx = this.pageModuleService.isMdxFile(pagePath);
215
+ const declaredModules = this.pageModuleService.collectPageDeclaredModules(pageModule);
216
+ const dependencies = await this.hydrationAssetService.createPageBrowserGraphDependencies(
217
+ pagePath,
218
+ isMdx,
219
+ declaredModules
220
+ );
221
+ const assets = [];
222
+ if (isMdx) {
223
+ const mdxConfigAssets = await this.mdxConfigDependencyService.processMdxConfigDependencies({
224
+ pagePath,
225
+ config: pageModule.config,
226
+ processComponentDependencies: async (components) => await this.processComponentDependencies(components)
227
+ });
228
+ assets.push(...mdxConfigAssets);
229
+ }
230
+ return { dependencies, assets };
231
+ } catch (error) {
232
+ if (error instanceof BundleError) {
233
+ console.error("[ecopages] Bundle errors:", error.logs);
234
+ }
235
+ throw new ReactRenderError(
236
+ `Failed to generate hydration script: ${error instanceof Error ? error.message : String(error)}`
237
+ );
238
+ }
239
+ }
240
+ async composeReactLayoutPageChildrenForShell(page, context) {
241
+ return composeReactLayoutPageChildren({
242
+ page,
243
+ context,
244
+ runtime: this.getReactRuntimeModules(),
245
+ integrationName: this.name,
246
+ normalizeUnresolvedMarkerArtifactHtml: (html) => this.normalizeUnresolvedMarkerArtifactHtml(html),
247
+ getRootTagName: (html) => this.getRootTagName(html),
248
+ getQueuedForeignSubtreeResolutionContext: (input) => this.getQueuedForeignSubtreeResolutionContext(input),
249
+ resolveQueuedForeignSubtreeHtml: (html, runtimeContext) => this.resolveReactQueuedForeignSubtreeHtml(html, runtimeContext)
250
+ });
251
+ }
252
+ async renderPageWithDocumentShell(input) {
253
+ const shellLayouts = input.layouts ?? (input.layout ? [input.layout] : []);
254
+ const composeChildren = resolveComposeChildren({
255
+ page: input.page,
256
+ shellLayouts,
257
+ reactIntegrationName: this.name,
258
+ composeChildren: (page, context) => this.composeReactLayoutPageChildrenForShell(page, context)
259
+ });
260
+ return renderPageDocumentShell(
261
+ {
262
+ renderComponentWithForeignChildren: (renderInput) => this.renderComponentWithForeignChildren(renderInput),
263
+ appendProcessedDependencies: (...assetGroups) => this.appendProcessedDependencies(...assetGroups)
264
+ },
265
+ {
266
+ ...input,
267
+ composeChildren
268
+ },
269
+ this.DOC_TYPE
270
+ );
271
+ }
272
+ /**
273
+ * Renders a full route response for the filesystem page pipeline.
274
+ *
275
+ * This path receives already-resolved route metadata, layout, locals, and HTML
276
+ * template instances from the shared renderer orchestration. Its main job is to
277
+ * serialize only the browser-safe page payload, compose the mixed React/non-
278
+ * React shell tree, and hand the result back as a document body.
279
+ */
280
+ async render({
281
+ params,
282
+ query,
283
+ props,
284
+ locals,
285
+ pageLocals,
286
+ metadata,
287
+ Page,
288
+ Layout,
289
+ HtmlTemplate,
290
+ pageProps,
291
+ pagePackage
292
+ }) {
293
+ try {
294
+ const pageModuleUrl = this.pagePayloadService.resolvePageModuleUrl(pagePackage, {
295
+ routerEnabled: Boolean(this.routerAdapter)
296
+ });
297
+ const safeLocals = this.pagePayloadService.getSerializableLocals(locals, getComponentRequires(Page));
298
+ const allPageProps = this.pagePayloadService.buildSerializedPageProps({
299
+ pageProps,
300
+ params,
301
+ query,
302
+ safeLocals
303
+ });
304
+ return await this.renderPageWithDocumentShell({
305
+ page: {
306
+ component: Page,
307
+ props: { params, query, ...props, locals: pageLocals }
308
+ },
309
+ layout: Layout ? {
310
+ component: Layout,
311
+ props: locals ? { locals } : {}
312
+ } : void 0,
313
+ htmlTemplate: HtmlTemplate,
314
+ metadata,
315
+ pageProps: allPageProps,
316
+ documentProps: pageModuleUrl ? { pageModuleUrl } : void 0
317
+ });
318
+ } catch (error) {
319
+ throw this.createRenderError("Failed to render component", error);
320
+ }
321
+ }
322
+ getHtmlDocumentContributions(options) {
323
+ if (options.partial || !options.renderOptions) {
324
+ return void 0;
325
+ }
326
+ const safeLocals = this.pagePayloadService.getSerializableLocals(
327
+ options.renderOptions.locals,
328
+ getComponentRequires(options.renderOptions.Page)
329
+ );
330
+ const pageModuleUrl = this.pagePayloadService.resolvePageModuleUrl(options.renderOptions.pagePackage, {
331
+ routerEnabled: Boolean(this.routerAdapter)
332
+ });
333
+ const allPageProps = this.pagePayloadService.buildSerializedPageProps({
334
+ pageProps: options.renderOptions.pageProps,
335
+ params: options.renderOptions.params,
336
+ query: options.renderOptions.query,
337
+ safeLocals
338
+ });
339
+ return this.pagePayloadService.buildNonReactDocumentContributions({
340
+ htmlTemplate: options.renderOptions.HtmlTemplate,
341
+ pageProps: allPageProps,
342
+ pageModuleUrl,
343
+ reactIntegrationName: this.name,
344
+ routerEnabled: Boolean(this.routerAdapter)
345
+ });
346
+ }
347
+ getDocumentAttributes() {
348
+ return this.getRouterDocumentAttributes();
349
+ }
350
+ async renderViewWithDocumentShell(input) {
351
+ const normalizedProps = input.props ?? {};
352
+ if (input.ctx.partial) {
353
+ const { react, reactDomServer } = this.getReactRuntimeModules();
354
+ const ViewComponent = asReactComponent(input.view);
355
+ return this.renderPartialViewResponse({
356
+ ...input,
357
+ renderInline: async () => await reactDomServer.renderToReadableStream(react.createElement(ViewComponent, normalizedProps))
358
+ });
359
+ }
360
+ await this.prepareViewDependencies(input.view, input.layout);
361
+ if (!input.ctx.partial) {
362
+ await this.appendHydrationAssetsForFile(input.view.config?.__eco?.file);
363
+ }
364
+ const HtmlTemplate = await this.getHtmlTemplate();
365
+ const metadata = await this.resolveViewMetadata(input.view, input.props);
366
+ const pageModuleUrl = this.pagePayloadService.resolvePageModuleUrl(this.htmlTransformer.getPagePackage(), {
367
+ routerEnabled: Boolean(this.routerAdapter)
368
+ });
369
+ const serializedPageProps = this.pagePayloadService.buildSerializedPageProps({
370
+ pageProps: normalizedProps,
371
+ params: {},
372
+ query: {},
373
+ safeLocals: this.pagePayloadService.getSerializableLocals(void 0, getComponentRequires(input.view))
374
+ });
375
+ const shellLayouts = input.layout ? [{ component: input.layout, props: {} }] : [];
376
+ const composeChildren = resolveComposeChildren({
377
+ page: { component: input.view, props: normalizedProps },
378
+ shellLayouts,
379
+ reactIntegrationName: this.name,
380
+ composeChildren: (page, context) => this.composeReactLayoutPageChildrenForShell(page, context)
381
+ });
382
+ const { documentHtml } = await composeDocumentShell(
383
+ {
384
+ renderComponentWithForeignChildren: (renderInput) => this.renderComponentWithForeignChildren(renderInput),
385
+ appendProcessedDependencies: (...assetGroups) => this.appendProcessedDependencies(...assetGroups)
386
+ },
387
+ {
388
+ primaryComponent: input.view,
389
+ primaryProps: normalizedProps,
390
+ layout: input.layout ? {
391
+ component: input.layout,
392
+ props: {}
393
+ } : void 0,
394
+ layouts: shellLayouts.length > 0 ? shellLayouts : void 0,
395
+ composeChildren,
396
+ htmlTemplate: HtmlTemplate,
397
+ documentProps: {
398
+ metadata,
399
+ pageProps: serializedPageProps,
400
+ ...pageModuleUrl && { pageModuleUrl }
401
+ }
402
+ }
403
+ );
404
+ const html = await this.finalizeResolvedHtml({
405
+ html: `${this.DOC_TYPE}${documentHtml}`,
406
+ partial: false,
407
+ documentAttributes: this.getRouterDocumentAttributes(),
408
+ htmlContributions: this.pagePayloadService.buildNonReactDocumentContributions({
409
+ htmlTemplate: HtmlTemplate,
410
+ pageProps: serializedPageProps,
411
+ pageModuleUrl,
412
+ reactIntegrationName: this.name,
413
+ routerEnabled: Boolean(this.routerAdapter)
414
+ })
415
+ });
416
+ return this.createHtmlResponse(html, input.ctx);
417
+ }
418
+ /**
419
+ * Renders an arbitrary React view through the application's HTML shell.
420
+ *
421
+ * Unlike route rendering, this path starts from a single component rather than a
422
+ * page module discovered by the router. It still needs to resolve metadata,
423
+ * layout dependencies, and hydration assets so direct `ctx.render()` calls match
424
+ * normal page responses.
425
+ */
426
+ async renderToResponse(view, props, ctx) {
427
+ try {
428
+ const layouts = view.config?.layouts;
429
+ return await this.renderViewWithDocumentShell({
430
+ view,
431
+ props,
432
+ ctx,
433
+ layout: layouts?.[layouts.length - 1]
434
+ });
435
+ } catch (error) {
436
+ throw this.createRenderError("Failed to render view", error);
437
+ }
438
+ }
439
+ }
440
+ export {
441
+ BundleError2 as BundleError,
442
+ ReactRenderError2 as ReactRenderError,
443
+ ReactRenderer
444
+ };
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Resolves the React and react-dom/server modules used for SSR.
3
+ *
4
+ * @remarks
5
+ * Prefers the app's own React install so SSR identity matches the app bundle.
6
+ * Falls back to the integration's React when the app package cannot resolve them.
7
+ */
8
+ import type { ReactRuntime } from './layout-compose.js';
9
+ export type ReactRuntimeModules = {
10
+ react: ReactRuntime;
11
+ reactDomServer: typeof import('react-dom/server');
12
+ };
13
+ export declare function resolveReactRuntimeModules(rootDir: string | undefined): ReactRuntimeModules;
@@ -0,0 +1,21 @@
1
+ import { createRequire } from "node:module";
2
+ import path from "node:path";
3
+ function resolveReactRuntimeModules(rootDir) {
4
+ const appPackageJsonPath = path.resolve(rootDir || process.cwd(), "package.json");
5
+ try {
6
+ const requireFromApp = createRequire(appPackageJsonPath);
7
+ return {
8
+ react: requireFromApp("react"),
9
+ reactDomServer: requireFromApp("react-dom/server")
10
+ };
11
+ } catch {
12
+ const requireFromIntegration = createRequire(import.meta.url);
13
+ return {
14
+ react: requireFromIntegration("react"),
15
+ reactDomServer: requireFromIntegration("react-dom/server")
16
+ };
17
+ }
18
+ }
19
+ export {
20
+ resolveReactRuntimeModules
21
+ };
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Unified React layout + page composition for SSR document shells.
3
+ *
4
+ * @remarks
5
+ * When every shell layout is React-managed, compose one element tree via
6
+ * {@link composeLayoutPageTree} / {@link composeLayoutPageTreeFromShell} instead of
7
+ * rendering each tier separately. This keeps SSR DOM shape aligned with client
8
+ * hydration through `@ecopages/react/layout-compose`.
9
+ */
10
+ import type { ComponentRenderInput, EcoComponent } from '@ecopages/core';
11
+ import type { ProcessedAsset } from '@ecopages/core/services/asset-processing-service';
12
+ import type { DocumentShellComposeChildrenContext, DocumentShellComposeChildrenResult, DocumentShellLayoutInput } from '@ecopages/core/route-renderer/orchestration/document-shell-render.service';
13
+ import type { ReactElement } from 'react';
14
+ import type { ReactForeignSubtreeResolutionContext } from './component-ssr.js';
15
+ import type { ReactRuntimeModules } from './react-runtime.js';
16
+ import { type ComposablePage, type LayoutComposeOptions } from './layout-compose.js';
17
+ export declare function composeReactLayoutPageTree(Page: ComposablePage, pageProps: Record<string, unknown>, shellEntries: Array<{
18
+ component: EcoComponent;
19
+ props?: Record<string, unknown>;
20
+ }>, options: LayoutComposeOptions): ReactElement;
21
+ export declare function shouldUseUnifiedReactLayoutComposition(options: {
22
+ page: EcoComponent;
23
+ shellLayouts: DocumentShellLayoutInput[];
24
+ reactIntegrationName: string;
25
+ }): boolean;
26
+ export declare function resolveComposeChildren(options: {
27
+ page: {
28
+ component: EcoComponent;
29
+ props: Record<string, unknown>;
30
+ };
31
+ shellLayouts: DocumentShellLayoutInput[];
32
+ reactIntegrationName: string;
33
+ composeChildren: (page: {
34
+ component: EcoComponent;
35
+ props: Record<string, unknown>;
36
+ }, context: DocumentShellComposeChildrenContext) => Promise<DocumentShellComposeChildrenResult>;
37
+ }): ((context: DocumentShellComposeChildrenContext) => Promise<DocumentShellComposeChildrenResult>) | undefined;
38
+ export declare function composeReactLayoutPageChildren(options: {
39
+ page: {
40
+ component: EcoComponent;
41
+ props: Record<string, unknown>;
42
+ };
43
+ context: DocumentShellComposeChildrenContext;
44
+ runtime: ReactRuntimeModules;
45
+ integrationName: string;
46
+ normalizeUnresolvedMarkerArtifactHtml: (html: string) => string;
47
+ getRootTagName: (html: string) => string | undefined;
48
+ getQueuedForeignSubtreeResolutionContext: (input: ComponentRenderInput) => ReactForeignSubtreeResolutionContext | undefined;
49
+ resolveQueuedForeignSubtreeHtml: (html: string, runtimeContext: ReactForeignSubtreeResolutionContext | undefined) => Promise<{
50
+ assets: ProcessedAsset[];
51
+ html: string;
52
+ }>;
53
+ }): Promise<DocumentShellComposeChildrenResult>;
@@ -0,0 +1,89 @@
1
+ import { hasSingleRootElement } from "./html-boundary.js";
2
+ import { isReactManagedComponent } from "./component-ownership.js";
3
+ import {
4
+ assertComposablePage,
5
+ composeLayoutPageTree,
6
+ composeLayoutPageTreeFromShell,
7
+ resolveLayoutContextFromShell
8
+ } from "./layout-compose.js";
9
+ function composeReactLayoutPageTree(Page, pageProps, shellEntries, options) {
10
+ const hasNormalizedLayouts = Boolean(Page.config?.layoutEntries?.length) || Boolean(Page.config?.layouts?.length);
11
+ if (hasNormalizedLayouts) {
12
+ return composeLayoutPageTree(Page, pageProps, options);
13
+ }
14
+ return composeLayoutPageTreeFromShell(Page, pageProps, shellEntries, options);
15
+ }
16
+ function shouldUseUnifiedReactLayoutComposition(options) {
17
+ const { page, shellLayouts, reactIntegrationName } = options;
18
+ if (!isReactManagedComponent(page, reactIntegrationName)) {
19
+ return false;
20
+ }
21
+ const composablePage = assertComposablePage(page);
22
+ const configLayouts = composablePage.config?.layouts ?? composablePage.config?.layoutEntries?.map((entry) => entry.component) ?? [];
23
+ const layoutComponents = shellLayouts.length > 0 ? shellLayouts.map((layout) => layout.component) : configLayouts;
24
+ if (layoutComponents.length === 0) {
25
+ return false;
26
+ }
27
+ return layoutComponents.every((component) => isReactManagedComponent(component, reactIntegrationName));
28
+ }
29
+ function resolveComposeChildren(options) {
30
+ const { page, shellLayouts, reactIntegrationName, composeChildren } = options;
31
+ if (!shouldUseUnifiedReactLayoutComposition({
32
+ page: page.component,
33
+ shellLayouts,
34
+ reactIntegrationName
35
+ })) {
36
+ return void 0;
37
+ }
38
+ return (context) => composeChildren(page, context);
39
+ }
40
+ async function composeReactLayoutPageChildren(options) {
41
+ const {
42
+ page,
43
+ context,
44
+ runtime,
45
+ integrationName,
46
+ normalizeUnresolvedMarkerArtifactHtml,
47
+ getRootTagName,
48
+ getQueuedForeignSubtreeResolutionContext,
49
+ resolveQueuedForeignSubtreeHtml
50
+ } = options;
51
+ const pageComponent = assertComposablePage(page.component);
52
+ const shellEntries = context.layouts.map((layout) => ({
53
+ component: layout.component,
54
+ props: layout.props
55
+ }));
56
+ const { react, reactDomServer } = runtime;
57
+ const tree = composeReactLayoutPageTree(pageComponent, page.props, shellEntries, {
58
+ context: resolveLayoutContextFromShell(page.props, shellEntries),
59
+ react
60
+ });
61
+ const runtimeInput = {
62
+ component: page.component,
63
+ props: page.props,
64
+ integrationContext: {
65
+ rendererCache: context.rendererCache
66
+ }
67
+ };
68
+ const runtimeContext = getQueuedForeignSubtreeResolutionContext(runtimeInput);
69
+ const html = normalizeUnresolvedMarkerArtifactHtml(reactDomServer.renderToString(tree));
70
+ const resolved = await resolveQueuedForeignSubtreeHtml(html, runtimeContext);
71
+ const primaryRender = {
72
+ html: resolved.html,
73
+ canAttachAttributes: hasSingleRootElement(resolved.html),
74
+ rootTag: getRootTagName(resolved.html),
75
+ integrationName,
76
+ assets: resolved.assets.length > 0 ? resolved.assets : void 0
77
+ };
78
+ return {
79
+ children: resolved.html,
80
+ layoutRenders: [],
81
+ primaryRender
82
+ };
83
+ }
84
+ export {
85
+ composeReactLayoutPageChildren,
86
+ composeReactLayoutPageTree,
87
+ resolveComposeChildren,
88
+ shouldUseUnifiedReactLayoutComposition
89
+ };