@ecopages/react 0.2.0-beta.13 → 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.
@@ -4,8 +4,10 @@ function walkConfigTree(config, visitor, visited = /* @__PURE__ */ new Set()) {
4
4
  }
5
5
  visited.add(config);
6
6
  visitor(config);
7
- if (config.layout?.config) {
8
- walkConfigTree(config.layout.config, visitor, visited);
7
+ for (const layout of config.layouts ?? []) {
8
+ if (layout?.config) {
9
+ walkConfigTree(layout.config, visitor, visited);
10
+ }
9
11
  }
10
12
  for (const component of config.dependencies?.components ?? []) {
11
13
  walkConfigTree(component.config, visitor, visited);
@@ -26,6 +26,8 @@ export type HydrationScriptOptions = {
26
26
  isMdx: boolean;
27
27
  /** Optional router adapter for SPA navigation */
28
28
  router?: ReactRouterAdapter;
29
+ /** Import path for shared layout composition helper */
30
+ layoutComposeImportPath?: string;
29
31
  };
30
32
  export type IslandHydrationScriptOptions = {
31
33
  /** Bundled browser module path for the island component. */
@@ -1,10 +1,23 @@
1
+ const DEFAULT_LAYOUT_COMPOSE_IMPORT_PATH = "@ecopages/react/layout-compose";
2
+ const PAGE_LAYOUT_NORMALIZATION_IMPORT = "@ecopages/core/eco/page-layout-normalization";
3
+ function resolveLayoutComposeImportPath(options) {
4
+ return options.layoutComposeImportPath ?? DEFAULT_LAYOUT_COMPOSE_IMPORT_PATH;
5
+ }
1
6
  function getImportStatement(importPath, isMdx) {
2
7
  return isMdx ? `import * as MDXModule from "${importPath}";
8
+ import { ensurePageConfigLayouts } from "${PAGE_LAYOUT_NORMALIZATION_IMPORT}";
3
9
  const Page = MDXModule.default;
4
- if (MDXModule.config) Page.config = MDXModule.config;` : `import Page from "${importPath}";`;
10
+ if (MDXModule.config) {
11
+ Page.config = MDXModule.config;
12
+ ensurePageConfigLayouts(Page.config);
13
+ }` : `import Page from "${importPath}";`;
5
14
  }
6
15
  function getHmrImportStatement(isMdx) {
7
- return isMdx ? "const NewPage = newModule.default; if (newModule.config) NewPage.config = newModule.config;" : "const NewPage = newModule.default;";
16
+ return isMdx ? `const NewPage = newModule.default;
17
+ if (newModule.config) {
18
+ NewPage.config = newModule.config;
19
+ ensurePageConfigLayouts(NewPage.config);
20
+ }` : "const NewPage = newModule.default;";
8
21
  }
9
22
  function getComponentType(isMdx) {
10
23
  return isMdx ? "MDX" : "React";
@@ -140,12 +153,14 @@ const mount = () => {
140
153
  const newModule = await import(newUrl);
141
154
  const nextProps = getPageData();
142
155
  ${getHmrImportStatement(isMdx)}
143
- const currentPageLayout = Page.config?.layout;
144
- const nextPageLayout = NewPage.config?.layout;
156
+ const currentPageLayoutStack = (Component) =>
157
+ (Component.config?.layouts ?? []).map((layout) => layout?.config?.__eco?.file ?? '').join('|');
158
+ const currentPageLayoutStackKey = currentPageLayoutStack(Page);
159
+ const nextPageLayoutStackKey = currentPageLayoutStack(NewPage);
145
160
 
146
161
  if (window.__ECO_PAGES__?.navigation?.getOwnerState().owner === "react-router") {
147
162
  await window.__ECO_PAGES__?.navigation?.reloadCurrentPage?.({
148
- clearCache: currentPageLayout !== nextPageLayout,
163
+ clearCache: currentPageLayoutStackKey !== nextPageLayoutStackKey,
149
164
  moduleUrl: "${importPath}",
150
165
  source: "react-router"
151
166
  });
@@ -178,9 +193,11 @@ if (document.readyState === "loading") {
178
193
  function createDevScriptWithoutRouter(options) {
179
194
  const { importPath, isMdx, reactImportPath, reactDomClientImportPath, scriptId } = options;
180
195
  const pageModuleUrlExpression = options.pageModuleUrlExpression ?? "import.meta.url";
196
+ const layoutComposeImportPath = resolveLayoutComposeImportPath(options);
181
197
  return `
182
198
  import { hydrateRoot } from "${reactDomClientImportPath}";
183
199
  import { createElement } from "${reactImportPath}";
200
+ import { composeLayoutPageTree } from "${layoutComposeImportPath}";
184
201
  ${getImportStatement(importPath, isMdx)}
185
202
  const pageModuleUrl = ${pageModuleUrlExpression};
186
203
  export default Page;
@@ -211,12 +228,7 @@ window.__ECO_PAGES__.page = {
211
228
  props
212
229
  };
213
230
 
214
- const createTree = (Component, props) => {
215
- const Layout = Component.config?.layout;
216
- const pageElement = createElement(Component, props);
217
- const layoutProps = props?.locals ? { locals: props.locals } : null;
218
- return Layout ? createElement(Layout, layoutProps, pageElement) : pageElement;
219
- };
231
+ const createTree = (Component, props) => composeLayoutPageTree(Component, props);
220
232
 
221
233
  const mount = () => {
222
234
  const props = getPageData();
@@ -264,17 +276,18 @@ function createProdScriptWithRouter(options) {
264
276
  throw new Error("routerImportPath is required when router adapter is configured");
265
277
  }
266
278
  if (isMdx) {
267
- return `import{hydrateRoot as hr}from"${reactDomClientImportPath}";import{createElement as ce}from"${reactImportPath}";import{${components.router} as R,${components.pageContent} as PC}from"${routerImportPath}";import*as M from"${importPath}";const P=M.default;if(M.config)P.config=M.config;const u=${pageModuleUrlExpression};export default P;export const config=P.config;const a=!!document.querySelector('script[data-eco-script-id="${scriptId}"]');if(a){window.__ECO_PAGES__=window.__ECO_PAGES__||{};window.__ECO_PAGES__.react=window.__ECO_PAGES__.react||{};window.__ECO_PAGES__.react.pageRoot=window.__ECO_PAGES__.react.pageRoot||null;let root=window.__ECO_PAGES__.react.pageRoot;${getProdPageRootCleanupScript()}${getProdRouterBootstrapRegistrationScript()}${getProdReuseExistingRouterRootScript()}const gd=()=>{const e=document.getElementById("__ECO_PAGE_DATA__");if(e?.textContent){try{return JSON.parse(e.textContent)}catch{}}return{}};const ct=(C,p)=>ce(R,${getRouterProps("C", "p")},ce(PC));const m=()=>{const pr=gd();window.__ECO_PAGES__.page={module:u,props:pr};if(sr()){root=window.__ECO_PAGES__.react.pageRoot;return}if(window.__ECO_PAGES__.react?.pageRoot){root=window.__ECO_PAGES__.react.pageRoot;return}root=hr(document.body,ct(P,pr),{onRecoverableError:(e)=>console.warn("[ecopages] Hydration error:",e)});window.__ECO_PAGES__.react.pageRoot=root};${getProdRerunRegistrationScript(scriptId)}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",m):m()}`;
279
+ return `import{hydrateRoot as hr}from"${reactDomClientImportPath}";import{createElement as ce}from"${reactImportPath}";import{${components.router} as R,${components.pageContent} as PC}from"${routerImportPath}";import{ensurePageConfigLayouts as epl}from"${PAGE_LAYOUT_NORMALIZATION_IMPORT}";import*as M from"${importPath}";const P=M.default;if(M.config){P.config=M.config;epl(P.config);}const u=${pageModuleUrlExpression};export default P;export const config=P.config;const a=!!document.querySelector('script[data-eco-script-id="${scriptId}"]');if(a){window.__ECO_PAGES__=window.__ECO_PAGES__||{};window.__ECO_PAGES__.react=window.__ECO_PAGES__.react||{};window.__ECO_PAGES__.react.pageRoot=window.__ECO_PAGES__.react.pageRoot||null;let root=window.__ECO_PAGES__.react.pageRoot;${getProdPageRootCleanupScript()}${getProdRouterBootstrapRegistrationScript()}${getProdReuseExistingRouterRootScript()}const gd=()=>{const e=document.getElementById("__ECO_PAGE_DATA__");if(e?.textContent){try{return JSON.parse(e.textContent)}catch{}}return{}};const ct=(C,p)=>ce(R,${getRouterProps("C", "p")},ce(PC));const m=()=>{const pr=gd();window.__ECO_PAGES__.page={module:u,props:pr};if(sr()){root=window.__ECO_PAGES__.react.pageRoot;return}if(window.__ECO_PAGES__.react?.pageRoot){root=window.__ECO_PAGES__.react.pageRoot;return}root=hr(document.body,ct(P,pr),{onRecoverableError:(e)=>console.warn("[ecopages] Hydration error:",e)});window.__ECO_PAGES__.react.pageRoot=root};${getProdRerunRegistrationScript(scriptId)}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",m):m()}`;
268
280
  }
269
281
  return `import{hydrateRoot as hr}from"${reactDomClientImportPath}";import{createElement as ce}from"${reactImportPath}";import{${components.router} as R,${components.pageContent} as PC}from"${routerImportPath}";import P from"${importPath}";const u=${pageModuleUrlExpression};export default P;export const config=P.config;const a=!!document.querySelector('script[data-eco-script-id="${scriptId}"]');if(a){window.__ECO_PAGES__=window.__ECO_PAGES__||{};window.__ECO_PAGES__.react=window.__ECO_PAGES__.react||{};window.__ECO_PAGES__.react.pageRoot=window.__ECO_PAGES__.react.pageRoot||null;let root=window.__ECO_PAGES__.react.pageRoot;${getProdPageRootCleanupScript()}${getProdRouterBootstrapRegistrationScript()}${getProdReuseExistingRouterRootScript()}const gd=()=>{const e=document.getElementById("__ECO_PAGE_DATA__");if(e?.textContent){try{return JSON.parse(e.textContent)}catch{}}return{}};const ct=(C,p)=>ce(R,${getRouterProps("C", "p")},ce(PC));const m=()=>{const pr=gd();window.__ECO_PAGES__.page={module:u,props:pr};if(sr()){root=window.__ECO_PAGES__.react.pageRoot;return}if(window.__ECO_PAGES__.react?.pageRoot){root=window.__ECO_PAGES__.react.pageRoot;return}root=hr(document.body,ct(P,pr),{onRecoverableError:(e)=>console.warn("[ecopages] Hydration error:",e)});window.__ECO_PAGES__.react.pageRoot=root};${getProdRerunRegistrationScript(scriptId)}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",m):m()}`;
270
282
  }
271
283
  function createProdScriptWithoutRouter(options) {
272
284
  const { importPath, isMdx, reactImportPath, reactDomClientImportPath, scriptId } = options;
273
285
  const pageModuleUrlExpression = options.pageModuleUrlExpression ?? "import.meta.url";
286
+ const layoutComposeImportPath = resolveLayoutComposeImportPath(options);
274
287
  if (isMdx) {
275
- return `import{hydrateRoot as hr}from"${reactDomClientImportPath}";import{createElement as ce}from"${reactImportPath}";import*as M from"${importPath}";const P=M.default;if(M.config)P.config=M.config;const u=${pageModuleUrlExpression};export default P;export const config=P.config;const a=!!document.querySelector('script[data-eco-script-id="${scriptId}"]');if(a){window.__ECO_PAGES__=window.__ECO_PAGES__||{};window.__ECO_PAGES__.react=window.__ECO_PAGES__.react||{};window.__ECO_PAGES__.react.pageRoot=window.__ECO_PAGES__.react.pageRoot||null;let root=window.__ECO_PAGES__.react.pageRoot;${getProdPageRootCleanupScript()}const gd=()=>{const e=document.getElementById("__ECO_PAGE_DATA__");if(e?.textContent){try{return JSON.parse(e.textContent)}catch{}}return{}};const ct=(C,p)=>{const L=C.config?.layout;const pe=ce(C,p);const lp=p?.locals?{locals:p.locals}:null;return L?ce(L,lp,pe):pe};const m=()=>{const pr=gd();window.__ECO_PAGES__.page={module:u,props:pr};if(window.__ECO_PAGES__.react?.pageRoot){root=window.__ECO_PAGES__.react.pageRoot;root.render(ct(P,pr));return}root=hr(document.body,ct(P,pr),{onRecoverableError:(e)=>console.warn("[ecopages] Hydration error:",e)});window.__ECO_PAGES__.react.pageRoot=root};${getProdRerunRegistrationScript(scriptId)}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",m):m()}`;
288
+ return `import{hydrateRoot as hr}from"${reactDomClientImportPath}";import{createElement as ce}from"${reactImportPath}";import{composeLayoutPageTree as clp}from"${layoutComposeImportPath}";import{ensurePageConfigLayouts as epl}from"${PAGE_LAYOUT_NORMALIZATION_IMPORT}";import*as M from"${importPath}";const P=M.default;if(M.config){P.config=M.config;epl(P.config);}const u=${pageModuleUrlExpression};export default P;export const config=P.config;const a=!!document.querySelector('script[data-eco-script-id="${scriptId}"]');if(a){window.__ECO_PAGES__=window.__ECO_PAGES__||{};window.__ECO_PAGES__.react=window.__ECO_PAGES__.react||{};window.__ECO_PAGES__.react.pageRoot=window.__ECO_PAGES__.react.pageRoot||null;let root=window.__ECO_PAGES__.react.pageRoot;${getProdPageRootCleanupScript()}const gd=()=>{const e=document.getElementById("__ECO_PAGE_DATA__");if(e?.textContent){try{return JSON.parse(e.textContent)}catch{}}return{}};const ct=(C,p)=>clp(C,p);const m=()=>{const pr=gd();window.__ECO_PAGES__.page={module:u,props:pr};if(window.__ECO_PAGES__.react?.pageRoot){root=window.__ECO_PAGES__.react.pageRoot;root.render(ct(P,pr));return}root=hr(document.body,ct(P,pr),{onRecoverableError:(e)=>console.warn("[ecopages] Hydration error:",e)});window.__ECO_PAGES__.react.pageRoot=root};${getProdRerunRegistrationScript(scriptId)}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",m):m()}`;
276
289
  }
277
- return `import{hydrateRoot as hr}from"${reactDomClientImportPath}";import{createElement as ce}from"${reactImportPath}";import P from"${importPath}";const u=${pageModuleUrlExpression};export default P;export const config=P.config;const a=!!document.querySelector('script[data-eco-script-id="${scriptId}"]');if(a){window.__ECO_PAGES__=window.__ECO_PAGES__||{};window.__ECO_PAGES__.react=window.__ECO_PAGES__.react||{};window.__ECO_PAGES__.react.pageRoot=window.__ECO_PAGES__.react.pageRoot||null;let root=window.__ECO_PAGES__.react.pageRoot;${getProdPageRootCleanupScript()}const gd=()=>{const e=document.getElementById("__ECO_PAGE_DATA__");if(e?.textContent){try{return JSON.parse(e.textContent)}catch{}}return{}};const ct=(C,p)=>{const L=C.config?.layout;const pe=ce(C,p);const lp=p?.locals?{locals:p.locals}:null;return L?ce(L,lp,pe):pe};const m=()=>{const pr=gd();window.__ECO_PAGES__.page={module:u,props:pr};if(window.__ECO_PAGES__.react?.pageRoot){root=window.__ECO_PAGES__.react.pageRoot;root.render(ct(P,pr));return}root=hr(document.body,ct(P,pr),{onRecoverableError:(e)=>console.warn("[ecopages] Hydration error:",e)});window.__ECO_PAGES__.react.pageRoot=root};${getProdRerunRegistrationScript(scriptId)}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",m):m()}`;
290
+ return `import{hydrateRoot as hr}from"${reactDomClientImportPath}";import{createElement as ce}from"${reactImportPath}";import{composeLayoutPageTree as clp}from"${layoutComposeImportPath}";import P from"${importPath}";const u=${pageModuleUrlExpression};export default P;export const config=P.config;const a=!!document.querySelector('script[data-eco-script-id="${scriptId}"]');if(a){window.__ECO_PAGES__=window.__ECO_PAGES__||{};window.__ECO_PAGES__.react=window.__ECO_PAGES__.react||{};window.__ECO_PAGES__.react.pageRoot=window.__ECO_PAGES__.react.pageRoot||null;let root=window.__ECO_PAGES__.react.pageRoot;${getProdPageRootCleanupScript()}const gd=()=>{const e=document.getElementById("__ECO_PAGE_DATA__");if(e?.textContent){try{return JSON.parse(e.textContent)}catch{}}return{}};const ct=(C,p)=>clp(C,p);const m=()=>{const pr=gd();window.__ECO_PAGES__.page={module:u,props:pr};if(window.__ECO_PAGES__.react?.pageRoot){root=window.__ECO_PAGES__.react.pageRoot;root.render(ct(P,pr));return}root=hr(document.body,ct(P,pr),{onRecoverableError:(e)=>console.warn("[ecopages] Hydration error:",e)});window.__ECO_PAGES__.react.pageRoot=root};${getProdRerunRegistrationScript(scriptId)}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",m):m()}`;
278
291
  }
279
292
  function createHydrationScript(options) {
280
293
  const { isDevelopment, router } = options;