@thexjs/core 1.3.4 → 1.3.5
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/dist/adapter.js +1 -1
- package/dist/{chunk-LV4CJQYM.js → chunk-PQ7ZVWEJ.js} +24 -5
- package/dist/index.d.ts +9 -0
- package/dist/index.js +71 -46
- package/package.json +1 -1
package/dist/adapter.js
CHANGED
|
@@ -540,17 +540,36 @@ function resolveIsland(name: string) {
|
|
|
540
540
|
|
|
541
541
|
// Islands whose render is non-deterministic (e.g. \`useState(() => ...)\` with
|
|
542
542
|
// Math.random) can't be hydrated against their SSR output. React recovers by
|
|
543
|
-
// re-rendering client-side, so the island is still fully interactive
|
|
544
|
-
// mismatch is
|
|
545
|
-
//
|
|
546
|
-
function
|
|
543
|
+
// re-rendering client-side, so the island is still fully interactive. The
|
|
544
|
+
// mismatch is NOT silently swallowed: it is reported to the server so real
|
|
545
|
+
// rendering bugs are observable in production instead of hidden.
|
|
546
|
+
function reportHydrationMismatch(error, name) {
|
|
547
|
+
try {
|
|
548
|
+
console.error("[x] hydration mismatch on island '" + name + "':", error);
|
|
549
|
+
} catch (_) {}
|
|
550
|
+
try {
|
|
551
|
+
var message = error instanceof Error ? error.message : String(error);
|
|
552
|
+
navigator.sendBeacon(
|
|
553
|
+
"/__x/hydration-mismatch",
|
|
554
|
+
new Blob([JSON.stringify({ error: message, island: name, url: location.href })], {
|
|
555
|
+
type: "application/json",
|
|
556
|
+
}),
|
|
557
|
+
);
|
|
558
|
+
} catch (_) {
|
|
559
|
+
// sendBeacon is best-effort; a failing beacon must not break hydration.
|
|
560
|
+
}
|
|
561
|
+
}
|
|
547
562
|
|
|
548
563
|
document.querySelectorAll("[data-island]").forEach((el) => {
|
|
549
564
|
const name = el.getAttribute("data-island");
|
|
550
565
|
if (!name) return;
|
|
551
566
|
const Component = resolveIsland(name);
|
|
552
567
|
if (!Component) return;
|
|
553
|
-
hydrateRoot(el, React.createElement(Component), {
|
|
568
|
+
hydrateRoot(el, React.createElement(Component), {
|
|
569
|
+
onRecoverableError(error) {
|
|
570
|
+
reportHydrationMismatch(error, name);
|
|
571
|
+
},
|
|
572
|
+
});
|
|
554
573
|
});
|
|
555
574
|
`;
|
|
556
575
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -201,6 +201,8 @@ interface ErrorContext {
|
|
|
201
201
|
route?: string;
|
|
202
202
|
requestId?: string;
|
|
203
203
|
phase: "ssr" | "action" | "api" | "loader";
|
|
204
|
+
/** Free-form discriminator, e.g. `hydration-mismatch`, for filtering. */
|
|
205
|
+
tag?: string;
|
|
204
206
|
}
|
|
205
207
|
interface ErrorReporter {
|
|
206
208
|
captureException(error: unknown, context: ErrorContext): void;
|
|
@@ -640,6 +642,13 @@ interface RenderOptions {
|
|
|
640
642
|
* reporter/metrics instead of shipping a partial page silently.
|
|
641
643
|
*/
|
|
642
644
|
onRenderError?: (error: unknown) => void;
|
|
645
|
+
/**
|
|
646
|
+
* Single-render island resolution. When set, the page renders exactly once
|
|
647
|
+
* and this callback is awaited to resolve the island script list from the
|
|
648
|
+
* islands that pass actually produced (the caller holds the registry). Used
|
|
649
|
+
* to eliminate the two-pass discovery render.
|
|
650
|
+
*/
|
|
651
|
+
resolveIslandScripts?: () => Promise<string[]>;
|
|
643
652
|
}
|
|
644
653
|
interface LoaderArgs {
|
|
645
654
|
params: Record<string, string>;
|
package/dist/index.js
CHANGED
|
@@ -34,7 +34,7 @@ import {
|
|
|
34
34
|
verifyOrigin,
|
|
35
35
|
withCsrfCookie,
|
|
36
36
|
writeManifest
|
|
37
|
-
} from "./chunk-
|
|
37
|
+
} from "./chunk-PQ7ZVWEJ.js";
|
|
38
38
|
|
|
39
39
|
// src/build.ts
|
|
40
40
|
import { cpSync, existsSync, mkdirSync, writeFileSync } from "fs";
|
|
@@ -411,29 +411,24 @@ function htmlShell(title, headExtras, propsScript, scripts, bodySlot, navScriptT
|
|
|
411
411
|
</html>`;
|
|
412
412
|
}
|
|
413
413
|
function renderPage(node, options = {}) {
|
|
414
|
-
const { islandScripts, islandProps } = options;
|
|
415
|
-
const title = options.title ?? "x app";
|
|
416
414
|
const body = renderToString(node);
|
|
417
|
-
|
|
418
|
-
const propsScript = islandProps ? `<script id="__X_ISLAND_PROPS" type="application/json">${propsJson}</script>` : "";
|
|
419
|
-
const islandScriptsHtml = islandScripts?.map((src) => `<script data-island-script src="${escapeHtml2(src)}"></script>`).join("\n ");
|
|
420
|
-
return htmlShell(
|
|
421
|
-
title,
|
|
422
|
-
buildHeadExtras(options.stylesheet),
|
|
423
|
-
propsScript,
|
|
424
|
-
islandScriptsHtml,
|
|
425
|
-
body,
|
|
426
|
-
buildNavScriptTag(options.clientNav),
|
|
427
|
-
buildLiveReloadTag(options.liveReload)
|
|
428
|
-
);
|
|
415
|
+
return buildShellHtml(body, options, options.islandScripts ?? []);
|
|
429
416
|
}
|
|
430
417
|
function renderStaticPage(node, options = {}) {
|
|
431
|
-
const { islandScripts, islandProps } = options;
|
|
432
|
-
const title = options.title ?? "x app";
|
|
433
418
|
const body = renderToStaticMarkup(node);
|
|
419
|
+
return buildShellHtml(body, options, options.islandScripts ?? []);
|
|
420
|
+
}
|
|
421
|
+
async function renderPageOnce(node, options = {}) {
|
|
422
|
+
const body = renderToString(node);
|
|
423
|
+
const scripts = options.resolveIslandScripts ? await options.resolveIslandScripts() : options.islandScripts ?? [];
|
|
424
|
+
return buildShellHtml(body, options, scripts);
|
|
425
|
+
}
|
|
426
|
+
function buildShellHtml(body, options, islandScripts) {
|
|
427
|
+
const { islandProps } = options;
|
|
428
|
+
const title = options.title ?? "x app";
|
|
434
429
|
const propsJson = islandProps ? escapeJsonForScript(JSON.stringify(islandProps)) : "";
|
|
435
430
|
const propsScript = islandProps ? `<script id="__X_ISLAND_PROPS" type="application/json">${propsJson}</script>` : "";
|
|
436
|
-
const islandScriptsHtml = islandScripts
|
|
431
|
+
const islandScriptsHtml = islandScripts.map((src) => `<script data-island-script src="${escapeHtml2(src)}"></script>`).join("\n ");
|
|
437
432
|
return htmlShell(
|
|
438
433
|
title,
|
|
439
434
|
buildHeadExtras(options.stylesheet),
|
|
@@ -458,9 +453,10 @@ function pumpStreamingResponse(source, options) {
|
|
|
458
453
|
const { done, value } = await reader.read();
|
|
459
454
|
if (done) {
|
|
460
455
|
if (!ended) {
|
|
456
|
+
const footerBytes = typeof footer === "function" ? await footer() : footer;
|
|
461
457
|
ended = true;
|
|
462
458
|
try {
|
|
463
|
-
controller.enqueue(
|
|
459
|
+
controller.enqueue(footerBytes);
|
|
464
460
|
} catch {
|
|
465
461
|
}
|
|
466
462
|
controller.close();
|
|
@@ -498,13 +494,9 @@ async function renderStreamingPage(node, options = {}) {
|
|
|
498
494
|
const title = escapeHtml2(options.title ?? "x app");
|
|
499
495
|
const propsJson = islandProps ? escapeJsonForScript(JSON.stringify(islandProps)) : "";
|
|
500
496
|
const propsScript = islandProps ? `<script id="__X_ISLAND_PROPS" type="application/json">${propsJson}</script>` : "";
|
|
501
|
-
const islandScriptsHtml = islandScripts?.map((src) => `<script data-island-script src="${escapeHtml2(src)}"></script>`).join("\n ");
|
|
502
497
|
const navScriptTag = buildNavScriptTag(options.clientNav);
|
|
503
498
|
const liveReloadTag = buildLiveReloadTag(options.liveReload);
|
|
504
499
|
const headExtras = buildHeadExtras(options.stylesheet);
|
|
505
|
-
const rootFooter = `${propsScript ? ` ${propsScript}
|
|
506
|
-
` : ""}${islandScriptsHtml ? ` ${islandScriptsHtml}
|
|
507
|
-
` : ""}`;
|
|
508
500
|
const footer = `</div>${navScriptTag}
|
|
509
501
|
${liveReloadTag} </body>
|
|
510
502
|
</html>`;
|
|
@@ -525,8 +517,18 @@ ${liveReloadTag} </body>
|
|
|
525
517
|
<div id="root">`;
|
|
526
518
|
return pumpStreamingResponse(reactStream, {
|
|
527
519
|
header: encoder.encode(header),
|
|
528
|
-
footer:
|
|
529
|
-
|
|
520
|
+
// Lazy footer: the island script list may only be knowable after the
|
|
521
|
+
// render completed (single-render mode), so resolve it right before the
|
|
522
|
+
// closing tags are emitted rather than up front.
|
|
523
|
+
footer: async () => {
|
|
524
|
+
const scripts = islandScripts ?? await options.resolveIslandScripts?.() ?? [];
|
|
525
|
+
const islandScriptsHtml = scripts.map((src) => `<script data-island-script src="${escapeHtml2(src)}"></script>`).join("\n ");
|
|
526
|
+
const rootFooter = `${propsScript ? ` ${propsScript}
|
|
527
|
+
` : ""}${islandScriptsHtml ? ` ${islandScriptsHtml}
|
|
528
|
+
` : ""}`;
|
|
529
|
+
return encoder.encode(`${rootFooter}
|
|
530
|
+
${footer}`);
|
|
531
|
+
},
|
|
530
532
|
...options.onRenderError ? { onRenderError: options.onRenderError } : {}
|
|
531
533
|
});
|
|
532
534
|
}
|
|
@@ -1726,19 +1728,17 @@ async function createApp(options) {
|
|
|
1726
1728
|
{ registry },
|
|
1727
1729
|
wrapWithLayouts(Component, ctx.params, loaderData, layoutModules)
|
|
1728
1730
|
);
|
|
1729
|
-
let scripts = islandScripts;
|
|
1730
|
-
if (scripts === void 0) {
|
|
1731
|
-
renderPage(content, { stylesheet: stylesheetHref, liveReload: dev });
|
|
1732
|
-
scripts = await resolveIslandScripts(
|
|
1733
|
-
route.filePath,
|
|
1734
|
-
layoutFilePaths,
|
|
1735
|
-
registry.entries
|
|
1736
|
-
);
|
|
1737
|
-
}
|
|
1738
1731
|
const stream = await renderStreamingPage(content, {
|
|
1739
1732
|
stylesheet: stylesheetHref,
|
|
1740
1733
|
liveReload: dev,
|
|
1741
|
-
islandScripts
|
|
1734
|
+
...islandScripts === void 0 ? {
|
|
1735
|
+
// Single-render mode: islands register into `registry`
|
|
1736
|
+
// during the one render that produces the HTML, and the
|
|
1737
|
+
// script list is resolved from that same pass in the lazy
|
|
1738
|
+
// footer — no separate discovery render, so non-deterministic
|
|
1739
|
+
// components can't diverge between two passes.
|
|
1740
|
+
resolveIslandScripts: () => resolveIslandScripts(route.filePath, layoutFilePaths, registry.entries)
|
|
1741
|
+
} : { islandScripts },
|
|
1742
1742
|
onRenderError: (error) => {
|
|
1743
1743
|
reportException(error, { route: route.routePath, phase: "ssr" });
|
|
1744
1744
|
metricsReporter?.incr("x_http_errors_total", 1, { phase: "ssr" });
|
|
@@ -1774,19 +1774,12 @@ async function createApp(options) {
|
|
|
1774
1774
|
{ registry },
|
|
1775
1775
|
wrapWithLayouts(Component, ctx.params, loaderData, layoutModules)
|
|
1776
1776
|
);
|
|
1777
|
-
|
|
1778
|
-
if (scripts === void 0) {
|
|
1779
|
-
renderPage(content, { stylesheet: stylesheetHref, liveReload: dev });
|
|
1780
|
-
scripts = await resolveIslandScripts(
|
|
1781
|
-
route.filePath,
|
|
1782
|
-
layoutFilePaths,
|
|
1783
|
-
registry.entries
|
|
1784
|
-
);
|
|
1785
|
-
}
|
|
1786
|
-
return renderPage(content, {
|
|
1777
|
+
return renderPageOnce(content, {
|
|
1787
1778
|
stylesheet: stylesheetHref,
|
|
1788
1779
|
liveReload: dev,
|
|
1789
|
-
islandScripts
|
|
1780
|
+
...islandScripts === void 0 ? {
|
|
1781
|
+
resolveIslandScripts: () => resolveIslandScripts(route.filePath, layoutFilePaths, registry.entries)
|
|
1782
|
+
} : { islandScripts }
|
|
1790
1783
|
});
|
|
1791
1784
|
};
|
|
1792
1785
|
if (revalidateSeconds > 0) {
|
|
@@ -2063,6 +2056,34 @@ async function createApp(options) {
|
|
|
2063
2056
|
staticCache.clear();
|
|
2064
2057
|
return new Response("Revalidated all");
|
|
2065
2058
|
}
|
|
2059
|
+
async function handleHydrationMismatch(req) {
|
|
2060
|
+
if (new URL(req.url).pathname !== "/__x/hydration-mismatch") return null;
|
|
2061
|
+
if (req.method !== "POST") return new Response("Method not allowed", { status: 405 });
|
|
2062
|
+
const origin = req.headers.get("origin");
|
|
2063
|
+
const referer = req.headers.get("referer");
|
|
2064
|
+
const selfOrigin = `${new URL(req.url).protocol}//${new URL(req.url).host}`;
|
|
2065
|
+
const attempt = origin ?? referer;
|
|
2066
|
+
if (attempt && !attempt.startsWith(selfOrigin)) {
|
|
2067
|
+
return new Response("Forbidden", { status: 403 });
|
|
2068
|
+
}
|
|
2069
|
+
let body;
|
|
2070
|
+
try {
|
|
2071
|
+
const text = await req.text();
|
|
2072
|
+
if (text.length > 2048) return new Response("Payload too large", { status: 413 });
|
|
2073
|
+
body = JSON.parse(text);
|
|
2074
|
+
} catch {
|
|
2075
|
+
return new Response("Invalid JSON body", { status: 400 });
|
|
2076
|
+
}
|
|
2077
|
+
const error = new Error(body.error ?? "hydration mismatch");
|
|
2078
|
+
const routeFromUrl = body.url ? new URL(body.url).pathname : "/";
|
|
2079
|
+
reportException(error, {
|
|
2080
|
+
route: routeFromUrl,
|
|
2081
|
+
phase: "ssr",
|
|
2082
|
+
tag: "hydration-mismatch"
|
|
2083
|
+
});
|
|
2084
|
+
metricsReporter?.incr("x_http_hydration_mismatch", 1, { island: body.island ?? "unknown" });
|
|
2085
|
+
return new Response(null, { status: 204 });
|
|
2086
|
+
}
|
|
2066
2087
|
if (options.development) {
|
|
2067
2088
|
let notifyClients2 = function() {
|
|
2068
2089
|
for (const send of sseClients) {
|
|
@@ -2177,6 +2198,8 @@ data: ${data}
|
|
|
2177
2198
|
}
|
|
2178
2199
|
const revalidationResult = await handleRevalidation(req);
|
|
2179
2200
|
if (revalidationResult !== null) return revalidationResult;
|
|
2201
|
+
const mismatchResult = await handleHydrationMismatch(req);
|
|
2202
|
+
if (mismatchResult !== null) return mismatchResult;
|
|
2180
2203
|
const serverFnResult = await serverFnHandler(req);
|
|
2181
2204
|
if (serverFnResult !== null) return serverFnResult;
|
|
2182
2205
|
const staticAsset = await serveStaticAsset(publicDir, req);
|
|
@@ -2233,6 +2256,8 @@ data: ${data}
|
|
|
2233
2256
|
}
|
|
2234
2257
|
const revalidationResult = await handleRevalidation(req);
|
|
2235
2258
|
if (revalidationResult !== null) return revalidationResult;
|
|
2259
|
+
const mismatchResult = await handleHydrationMismatch(req);
|
|
2260
|
+
if (mismatchResult !== null) return mismatchResult;
|
|
2236
2261
|
const serverFnResult = await serverFnHandler(req);
|
|
2237
2262
|
if (serverFnResult !== null) return serverFnResult;
|
|
2238
2263
|
const staticAsset = await serveStaticAsset(publicDir, req);
|
package/package.json
CHANGED