@thexjs/core 1.3.3 → 1.3.4
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/index.d.ts +6 -0
- package/dist/index.js +59 -35
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -634,6 +634,12 @@ interface RenderOptions {
|
|
|
634
634
|
clientNav?: boolean;
|
|
635
635
|
/** Inject live-reload script (development mode). */
|
|
636
636
|
liveReload?: boolean;
|
|
637
|
+
/**
|
|
638
|
+
* Called when the SSR stream fails mid-flight. Pass this through to
|
|
639
|
+
* `renderStreamingPage` so the app can report the failure via its error
|
|
640
|
+
* reporter/metrics instead of shipping a partial page silently.
|
|
641
|
+
*/
|
|
642
|
+
onRenderError?: (error: unknown) => void;
|
|
637
643
|
}
|
|
638
644
|
interface LoaderArgs {
|
|
639
645
|
params: Record<string, string>;
|
package/dist/index.js
CHANGED
|
@@ -444,6 +444,55 @@ function renderStaticPage(node, options = {}) {
|
|
|
444
444
|
buildLiveReloadTag(options.liveReload)
|
|
445
445
|
);
|
|
446
446
|
}
|
|
447
|
+
function pumpStreamingResponse(source, options) {
|
|
448
|
+
const { header, footer, onRenderError, onCancel } = options;
|
|
449
|
+
const reader = source.getReader();
|
|
450
|
+
let headerSent = false;
|
|
451
|
+
let ended = false;
|
|
452
|
+
async function readAhead(controller) {
|
|
453
|
+
if (!headerSent) {
|
|
454
|
+
headerSent = true;
|
|
455
|
+
controller.enqueue(header);
|
|
456
|
+
}
|
|
457
|
+
while (true) {
|
|
458
|
+
const { done, value } = await reader.read();
|
|
459
|
+
if (done) {
|
|
460
|
+
if (!ended) {
|
|
461
|
+
ended = true;
|
|
462
|
+
try {
|
|
463
|
+
controller.enqueue(footer);
|
|
464
|
+
} catch {
|
|
465
|
+
}
|
|
466
|
+
controller.close();
|
|
467
|
+
}
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
controller.enqueue(value);
|
|
471
|
+
if (controller.desiredSize !== null && controller.desiredSize <= 0) return;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
return new ReadableStream({
|
|
475
|
+
async pull(controller) {
|
|
476
|
+
try {
|
|
477
|
+
await readAhead(controller);
|
|
478
|
+
} catch (err) {
|
|
479
|
+
if (!ended) {
|
|
480
|
+
ended = true;
|
|
481
|
+
onRenderError?.(err);
|
|
482
|
+
}
|
|
483
|
+
try {
|
|
484
|
+
controller.error(err);
|
|
485
|
+
} catch {
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
},
|
|
489
|
+
cancel(reason) {
|
|
490
|
+
reader.cancel(reason).catch(() => {
|
|
491
|
+
});
|
|
492
|
+
onCancel?.(reason);
|
|
493
|
+
}
|
|
494
|
+
});
|
|
495
|
+
}
|
|
447
496
|
async function renderStreamingPage(node, options = {}) {
|
|
448
497
|
const { islandScripts, islandProps } = options;
|
|
449
498
|
const title = escapeHtml2(options.title ?? "x app");
|
|
@@ -474,40 +523,11 @@ ${liveReloadTag} </body>
|
|
|
474
523
|
</head>
|
|
475
524
|
<body>
|
|
476
525
|
<div id="root">`;
|
|
477
|
-
return
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
try {
|
|
483
|
-
const { done, value } = await reader.read();
|
|
484
|
-
if (done) {
|
|
485
|
-
try {
|
|
486
|
-
controller.enqueue(encoder.encode(`${rootFooter}
|
|
487
|
-
${footer}`));
|
|
488
|
-
controller.close();
|
|
489
|
-
} catch {
|
|
490
|
-
}
|
|
491
|
-
return;
|
|
492
|
-
}
|
|
493
|
-
controller.enqueue(value);
|
|
494
|
-
await pump();
|
|
495
|
-
} catch (err) {
|
|
496
|
-
console.error("[x] stream read error:", err);
|
|
497
|
-
try {
|
|
498
|
-
controller.enqueue(
|
|
499
|
-
encoder.encode(
|
|
500
|
-
`${rootFooter}<div style="color:red;padding:1em;margin:1rem">Render error: ${err instanceof Error ? err.message : "Unknown"}</div>
|
|
501
|
-
${footer}`
|
|
502
|
-
)
|
|
503
|
-
);
|
|
504
|
-
controller.close();
|
|
505
|
-
} catch {
|
|
506
|
-
}
|
|
507
|
-
}
|
|
508
|
-
}
|
|
509
|
-
await pump();
|
|
510
|
-
}
|
|
526
|
+
return pumpStreamingResponse(reactStream, {
|
|
527
|
+
header: encoder.encode(header),
|
|
528
|
+
footer: encoder.encode(`${rootFooter}
|
|
529
|
+
${footer}`),
|
|
530
|
+
...options.onRenderError ? { onRenderError: options.onRenderError } : {}
|
|
511
531
|
});
|
|
512
532
|
}
|
|
513
533
|
|
|
@@ -1718,7 +1738,11 @@ async function createApp(options) {
|
|
|
1718
1738
|
const stream = await renderStreamingPage(content, {
|
|
1719
1739
|
stylesheet: stylesheetHref,
|
|
1720
1740
|
liveReload: dev,
|
|
1721
|
-
islandScripts: scripts
|
|
1741
|
+
islandScripts: scripts,
|
|
1742
|
+
onRenderError: (error) => {
|
|
1743
|
+
reportException(error, { route: route.routePath, phase: "ssr" });
|
|
1744
|
+
metricsReporter?.incr("x_http_errors_total", 1, { phase: "ssr" });
|
|
1745
|
+
}
|
|
1722
1746
|
});
|
|
1723
1747
|
return new Response(stream, {
|
|
1724
1748
|
headers: { "Content-Type": "text/html; charset=utf-8" }
|
package/package.json
CHANGED