@ecopages/react 0.2.0-alpha.41 → 0.2.0-alpha.42
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/package.json
CHANGED
|
@@ -121,6 +121,7 @@ class ReactHydrationAssetService {
|
|
|
121
121
|
position: "head",
|
|
122
122
|
content: createIslandHydrationScript({
|
|
123
123
|
importPath,
|
|
124
|
+
scriptId: hydrationName,
|
|
124
125
|
reactImportPath: runtimeImports.react,
|
|
125
126
|
reactDomClientImportPath: runtimeImports.reactDomClient,
|
|
126
127
|
targetSelector: `[data-eco-component-key="${componentKey}"]`,
|
|
@@ -30,6 +30,8 @@ export type HydrationScriptOptions = {
|
|
|
30
30
|
export type IslandHydrationScriptOptions = {
|
|
31
31
|
/** Bundled browser module path for the island component. */
|
|
32
32
|
importPath: string;
|
|
33
|
+
/** Stable id of the island bootstrap script tag in the document. */
|
|
34
|
+
scriptId: string;
|
|
33
35
|
/** Browser import path for React runtime. */
|
|
34
36
|
reactImportPath: string;
|
|
35
37
|
/** Browser import path for react-dom/client runtime. */
|
|
@@ -58,6 +58,14 @@ function getDevReuseExistingRouterRootScript() {
|
|
|
58
58
|
function getProdReuseExistingRouterRootScript() {
|
|
59
59
|
return 'const sr=()=>{const o=window.__ECO_PAGES__?.navigation?.getOwnerState?.();return!!(window.__ECO_PAGES__.react?.pageRoot&&o?.owner==="react-router"&&o.canHandleSpaNavigation)};';
|
|
60
60
|
}
|
|
61
|
+
function getDevRerunRegistrationScript(scriptId) {
|
|
62
|
+
return `window.__ECO_PAGES__ = window.__ECO_PAGES__ || {};
|
|
63
|
+
window.__ECO_PAGES__.rerunScripts = window.__ECO_PAGES__.rerunScripts || {};
|
|
64
|
+
window.__ECO_PAGES__.rerunScripts[${JSON.stringify(scriptId)}] = mount;`;
|
|
65
|
+
}
|
|
66
|
+
function getProdRerunRegistrationScript(scriptId) {
|
|
67
|
+
return `window.__ECO_PAGES__=window.__ECO_PAGES__||{};window.__ECO_PAGES__.rerunScripts=window.__ECO_PAGES__.rerunScripts||{};window.__ECO_PAGES__.rerunScripts[${JSON.stringify(scriptId)}]=m;`;
|
|
68
|
+
}
|
|
61
69
|
function createDevScriptWithRouter(options) {
|
|
62
70
|
const { importPath, isMdx, router, reactImportPath, reactDomClientImportPath, routerImportPath, scriptId } = options;
|
|
63
71
|
const pageModuleUrlExpression = options.pageModuleUrlExpression ?? "import.meta.url";
|
|
@@ -107,6 +115,12 @@ const createTree = (Component, props) => {
|
|
|
107
115
|
};
|
|
108
116
|
|
|
109
117
|
const mount = () => {
|
|
118
|
+
const props = getPageData();
|
|
119
|
+
window.__ECO_PAGES__.page = {
|
|
120
|
+
module: pageModuleUrl,
|
|
121
|
+
props
|
|
122
|
+
};
|
|
123
|
+
|
|
110
124
|
if (shouldReuseExistingRouterRoot()) {
|
|
111
125
|
root = window.__ECO_PAGES__.react.pageRoot;
|
|
112
126
|
} else if (window.__ECO_PAGES__.react?.pageRoot) {
|
|
@@ -148,6 +162,8 @@ const mount = () => {
|
|
|
148
162
|
};
|
|
149
163
|
};
|
|
150
164
|
|
|
165
|
+
${getDevRerunRegistrationScript(scriptId)}
|
|
166
|
+
|
|
151
167
|
if (document.readyState === "loading") {
|
|
152
168
|
document.addEventListener("DOMContentLoaded", mount);
|
|
153
169
|
} else {
|
|
@@ -200,6 +216,12 @@ const createTree = (Component, props) => {
|
|
|
200
216
|
};
|
|
201
217
|
|
|
202
218
|
const mount = () => {
|
|
219
|
+
const props = getPageData();
|
|
220
|
+
window.__ECO_PAGES__.page = {
|
|
221
|
+
module: pageModuleUrl,
|
|
222
|
+
props
|
|
223
|
+
};
|
|
224
|
+
|
|
203
225
|
if (window.__ECO_PAGES__.react?.pageRoot) {
|
|
204
226
|
root = window.__ECO_PAGES__.react.pageRoot;
|
|
205
227
|
root.render(createTree(Page, props));
|
|
@@ -221,6 +243,8 @@ const mount = () => {
|
|
|
221
243
|
};
|
|
222
244
|
};
|
|
223
245
|
|
|
246
|
+
${getDevRerunRegistrationScript(scriptId)}
|
|
247
|
+
|
|
224
248
|
if (document.readyState === "loading") {
|
|
225
249
|
document.addEventListener("DOMContentLoaded", mount);
|
|
226
250
|
} else {
|
|
@@ -237,17 +261,17 @@ function createProdScriptWithRouter(options) {
|
|
|
237
261
|
throw new Error("routerImportPath is required when router adapter is configured");
|
|
238
262
|
}
|
|
239
263
|
if (isMdx) {
|
|
240
|
-
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
|
|
264
|
+
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;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()}`;
|
|
241
265
|
}
|
|
242
|
-
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
|
|
266
|
+
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;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()}`;
|
|
243
267
|
}
|
|
244
268
|
function createProdScriptWithoutRouter(options) {
|
|
245
269
|
const { importPath, isMdx, reactImportPath, reactDomClientImportPath, scriptId } = options;
|
|
246
270
|
const pageModuleUrlExpression = options.pageModuleUrlExpression ?? "import.meta.url";
|
|
247
271
|
if (isMdx) {
|
|
248
|
-
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
|
|
272
|
+
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()}`;
|
|
249
273
|
}
|
|
250
|
-
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
|
|
274
|
+
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()}`;
|
|
251
275
|
}
|
|
252
276
|
function createHydrationScript(options) {
|
|
253
277
|
const { isDevelopment, router } = options;
|
|
@@ -260,6 +284,7 @@ function createIslandHydrationScript(options) {
|
|
|
260
284
|
const targetSelector = JSON.stringify(options.targetSelector);
|
|
261
285
|
const componentRef = JSON.stringify(options.componentRef ?? "");
|
|
262
286
|
const componentFile = JSON.stringify(options.componentFile ?? "");
|
|
287
|
+
const scriptId = options.scriptId;
|
|
263
288
|
if (options.isDevelopment) {
|
|
264
289
|
return `
|
|
265
290
|
import { createRoot } from "${options.reactDomClientImportPath}";
|
|
@@ -313,6 +338,8 @@ const mount = () => {
|
|
|
313
338
|
});
|
|
314
339
|
};
|
|
315
340
|
|
|
341
|
+
${getDevRerunRegistrationScript(scriptId)}
|
|
342
|
+
|
|
316
343
|
if (document.readyState === "loading") {
|
|
317
344
|
document.addEventListener("DOMContentLoaded", mount, { once: true });
|
|
318
345
|
} else {
|
|
@@ -320,7 +347,7 @@ if (document.readyState === "loading") {
|
|
|
320
347
|
}
|
|
321
348
|
`.trim();
|
|
322
349
|
}
|
|
323
|
-
return `import{createRoot as cr}from"${options.reactDomClientImportPath}";import{createElement as ce}from"${options.reactImportPath}";import*as M from"${options.importPath}";const r=${componentRef};const f=${componentFile};const mv=Object.values(M);const c=mv.find((e)=>{if(typeof e!=="function")return false;const ec=e.config?.__eco;if(!ec)return false;if(r&&ec.id===r)return true;if(f&&ec.file===f)return true;return false;})??(typeof M.default==="function"?M.default:mv.find((e)=>typeof e==="function")??null);const m=()=>{const ts=document.querySelectorAll(${targetSelector});if(!c||ts.length===0)return;ts.forEach((t)=>{if(!(t instanceof HTMLElement))return;const p=JSON.parse(atob(t.getAttribute("data-eco-props")||"e30="));const ct=document.createElement("eco-island");ct.style.display="block";t.replaceWith(ct);cr(ct).render(ce(c,p))})}
|
|
350
|
+
return `import{createRoot as cr}from"${options.reactDomClientImportPath}";import{createElement as ce}from"${options.reactImportPath}";import*as M from"${options.importPath}";const r=${componentRef};const f=${componentFile};const mv=Object.values(M);const c=mv.find((e)=>{if(typeof e!=="function")return false;const ec=e.config?.__eco;if(!ec)return false;if(r&&ec.id===r)return true;if(f&&ec.file===f)return true;return false;})??(typeof M.default==="function"?M.default:mv.find((e)=>typeof e==="function")??null);const m=()=>{const ts=document.querySelectorAll(${targetSelector});if(!c||ts.length===0)return;ts.forEach((t)=>{if(!(t instanceof HTMLElement))return;const p=JSON.parse(atob(t.getAttribute("data-eco-props")||"e30="));const ct=document.createElement("eco-island");ct.style.display="block";t.replaceWith(ct);cr(ct).render(ce(c,p))})};${getProdRerunRegistrationScript(scriptId)}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",m,{once:true}):m()`;
|
|
324
351
|
}
|
|
325
352
|
export {
|
|
326
353
|
createHydrationScript,
|