@seip/bluebird-js 0.1.4 → 0.1.6
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/AGENTS.md +576 -486
- package/README.md +602 -425
- package/app/views/about.jsx +98 -98
- package/app/views/components/LiveCounter.jsx +16 -17
- package/core/ai.d.ts +151 -0
- package/core/ai.js +1051 -0
- package/core/app.js +12 -2
- package/core/cli/backup.js +167 -0
- package/core/cli/init.js +16 -7
- package/core/database.js +462 -2
- package/core/index.d.ts +1426 -1273
- package/core/jsx.js +258 -6
- package/core/render.js +158 -27
- package/core/view-loader.js +57 -12
- package/package.json +2 -2
- package/public/js/bluebird-client.js +2450 -2334
package/core/jsx.js
CHANGED
|
@@ -5,6 +5,11 @@ import { SafeHtml, escapeHtml, safeJson, isSafeHtml } from "./html.js";
|
|
|
5
5
|
|
|
6
6
|
export const JSX_ELEMENT = Symbol.for("Bluebird.JsxElement");
|
|
7
7
|
export const Fragment = Symbol.for("Bluebird.Fragment");
|
|
8
|
+
export const ERROR_BOUNDARY = Symbol.for("Bluebird.ErrorBoundary");
|
|
9
|
+
export const SUSPENSE_ELEMENT = Symbol.for("Bluebird.Suspense");
|
|
10
|
+
export const HEAD_ELEMENT = Symbol.for("Bluebird.Head");
|
|
11
|
+
export const TITLE_ELEMENT = Symbol.for("Bluebird.Title");
|
|
12
|
+
export const META_ELEMENT = Symbol.for("Bluebird.Meta");
|
|
8
13
|
|
|
9
14
|
/**
|
|
10
15
|
* Boolean HTML attributes that should be rendered as bare attribute names when true
|
|
@@ -320,6 +325,64 @@ function renderAttributes(props) {
|
|
|
320
325
|
return attrs;
|
|
321
326
|
}
|
|
322
327
|
|
|
328
|
+
/**
|
|
329
|
+
* Built-in native error boundary component for Bluebird React JSX SSR.
|
|
330
|
+
* Catches rendering exceptions in subtrees and renders a fallback UI without failing the entire page request.
|
|
331
|
+
*
|
|
332
|
+
* @param {Object} props
|
|
333
|
+
* @param {any} [props.fallback] - JSX element, string, SafeHtml, or formatter function (error) => JSX.
|
|
334
|
+
* @param {Function} [props.onError] - Optional error reporting callback (error) => void.
|
|
335
|
+
* @param {any} [props.children] - Subtree to protect.
|
|
336
|
+
* @returns {Object}
|
|
337
|
+
*/
|
|
338
|
+
export function ErrorBoundary(props) {
|
|
339
|
+
return h(ERROR_BOUNDARY, props, props?.children);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Built-in Suspense component for streaming SSR and progressive page rendering.
|
|
344
|
+
* Renders fallback skeleton immediately, streaming resolved content when ready.
|
|
345
|
+
*
|
|
346
|
+
* @param {Object} props
|
|
347
|
+
* @param {any} props.fallback - Skeleton/fallback content displayed while children load.
|
|
348
|
+
* @param {any} [props.children] - Async component, promise, or subtree.
|
|
349
|
+
* @returns {Object}
|
|
350
|
+
*/
|
|
351
|
+
export function Suspense(props) {
|
|
352
|
+
return h(SUSPENSE_ELEMENT, props, props?.children);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Hoists head elements (<title>, <meta>, <link>) from any nested view or component into the document <head>.
|
|
357
|
+
* Renders as empty string in the body stream while populating ctx.headTags and ctx.title.
|
|
358
|
+
*
|
|
359
|
+
* @param {Object} props
|
|
360
|
+
* @param {any} [props.children]
|
|
361
|
+
* @returns {Object}
|
|
362
|
+
*/
|
|
363
|
+
export function Head(props) {
|
|
364
|
+
return h(HEAD_ELEMENT, props, props?.children);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Dedicated head title element that hoists the page title into the document <head>.
|
|
369
|
+
* @param {Object} props
|
|
370
|
+
* @param {any} [props.children]
|
|
371
|
+
* @returns {Object}
|
|
372
|
+
*/
|
|
373
|
+
export function Title(props) {
|
|
374
|
+
return h(TITLE_ELEMENT, props, props?.children);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Dedicated head meta element that hoists a <meta> tag into the document <head>.
|
|
379
|
+
* @param {Record<string, any>} props
|
|
380
|
+
* @returns {Object}
|
|
381
|
+
*/
|
|
382
|
+
export function Meta(props) {
|
|
383
|
+
return h(META_ELEMENT, props);
|
|
384
|
+
}
|
|
385
|
+
|
|
323
386
|
/**
|
|
324
387
|
* Renders any JSX node, async Server Component, SafeHtml, or primitive tree into an HTML string.
|
|
325
388
|
* Supports async function components (`export default async function Page()`),
|
|
@@ -371,6 +434,127 @@ export async function renderJsxToString(node, ctx = {}) {
|
|
|
371
434
|
return renderJsxToString(props.children, ctx);
|
|
372
435
|
}
|
|
373
436
|
|
|
437
|
+
if (
|
|
438
|
+
type === ErrorBoundary ||
|
|
439
|
+
type === ERROR_BOUNDARY ||
|
|
440
|
+
type === Symbol.for("Bluebird.ErrorBoundary")
|
|
441
|
+
) {
|
|
442
|
+
try {
|
|
443
|
+
return await renderJsxToString(props.children, ctx);
|
|
444
|
+
} catch (err) {
|
|
445
|
+
if (typeof props.onError === "function") {
|
|
446
|
+
try {
|
|
447
|
+
props.onError(err);
|
|
448
|
+
} catch {}
|
|
449
|
+
}
|
|
450
|
+
if (props.fallback !== undefined && props.fallback !== null) {
|
|
451
|
+
const fallbackNode =
|
|
452
|
+
typeof props.fallback === "function" ? props.fallback(err) : props.fallback;
|
|
453
|
+
return renderJsxToString(fallbackNode, ctx);
|
|
454
|
+
}
|
|
455
|
+
return "";
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
if (
|
|
460
|
+
type === Suspense ||
|
|
461
|
+
type === SUSPENSE_ELEMENT ||
|
|
462
|
+
type === Symbol.for("Bluebird.Suspense")
|
|
463
|
+
) {
|
|
464
|
+
if (ctx.isStreaming) {
|
|
465
|
+
ctx.suspenseCounter = (ctx.suspenseCounter || 0) + 1;
|
|
466
|
+
const suspenseId = `bb-suspense-${ctx.suspenseCounter}`;
|
|
467
|
+
const fallbackHtml =
|
|
468
|
+
props.fallback !== undefined && props.fallback !== null
|
|
469
|
+
? await renderJsxToString(props.fallback, ctx)
|
|
470
|
+
: "";
|
|
471
|
+
|
|
472
|
+
const task = (async () => {
|
|
473
|
+
try {
|
|
474
|
+
const childHtml = await renderJsxToString(props.children, ctx);
|
|
475
|
+
return { id: suspenseId, html: childHtml, success: true };
|
|
476
|
+
} catch (err) {
|
|
477
|
+
return { id: suspenseId, error: err, success: false };
|
|
478
|
+
}
|
|
479
|
+
})();
|
|
480
|
+
|
|
481
|
+
ctx.suspenseTasks = ctx.suspenseTasks || [];
|
|
482
|
+
ctx.suspenseTasks.push(task);
|
|
483
|
+
|
|
484
|
+
return `<div id="${suspenseId}">${fallbackHtml}</div>`;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
try {
|
|
488
|
+
return await renderJsxToString(props.children, ctx);
|
|
489
|
+
} catch (err) {
|
|
490
|
+
if (props.fallback !== undefined && props.fallback !== null) {
|
|
491
|
+
const fallbackNode =
|
|
492
|
+
typeof props.fallback === "function" ? props.fallback(err) : props.fallback;
|
|
493
|
+
return renderJsxToString(fallbackNode, ctx);
|
|
494
|
+
}
|
|
495
|
+
throw err;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
if (
|
|
500
|
+
type === Head ||
|
|
501
|
+
type === HEAD_ELEMENT ||
|
|
502
|
+
type === Symbol.for("Bluebird.Head")
|
|
503
|
+
) {
|
|
504
|
+
ctx.headTags = ctx.headTags || [];
|
|
505
|
+
const headChildren = props.children;
|
|
506
|
+
if (headChildren !== undefined && headChildren !== null) {
|
|
507
|
+
const list = Array.isArray(headChildren) ? headChildren : [headChildren];
|
|
508
|
+
for (const item of list) {
|
|
509
|
+
if (!item) continue;
|
|
510
|
+
if (isJsxElement(item)) {
|
|
511
|
+
if (
|
|
512
|
+
item.type === "title" ||
|
|
513
|
+
item.type === Title ||
|
|
514
|
+
item.type === TITLE_ELEMENT ||
|
|
515
|
+
item.type === Symbol.for("Bluebird.Title")
|
|
516
|
+
) {
|
|
517
|
+
const titleText = await renderJsxToString(item.props?.children, ctx);
|
|
518
|
+
ctx.title = titleText;
|
|
519
|
+
ctx.headTags.push(`<title>${escapeHtml(titleText)}</title>`);
|
|
520
|
+
continue;
|
|
521
|
+
}
|
|
522
|
+
const renderedTag = await renderJsxToString(item, ctx);
|
|
523
|
+
if (renderedTag) {
|
|
524
|
+
ctx.headTags.push(renderedTag);
|
|
525
|
+
}
|
|
526
|
+
} else if (typeof item === "string") {
|
|
527
|
+
ctx.headTags.push(item);
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
return "";
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
if (
|
|
535
|
+
type === Title ||
|
|
536
|
+
type === TITLE_ELEMENT ||
|
|
537
|
+
type === Symbol.for("Bluebird.Title")
|
|
538
|
+
) {
|
|
539
|
+
const titleText = await renderJsxToString(props.children, ctx);
|
|
540
|
+
ctx.title = titleText;
|
|
541
|
+
ctx.headTags = ctx.headTags || [];
|
|
542
|
+
ctx.headTags.push(`<title>${escapeHtml(titleText)}</title>`);
|
|
543
|
+
return "";
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
if (
|
|
547
|
+
type === Meta ||
|
|
548
|
+
type === META_ELEMENT ||
|
|
549
|
+
type === Symbol.for("Bluebird.Meta")
|
|
550
|
+
) {
|
|
551
|
+
const attrs = renderAttributes(props);
|
|
552
|
+
const metaHtml = `<meta${attrs} />`;
|
|
553
|
+
ctx.headTags = ctx.headTags || [];
|
|
554
|
+
ctx.headTags.push(metaHtml);
|
|
555
|
+
return "";
|
|
556
|
+
}
|
|
557
|
+
|
|
374
558
|
if (typeof type === "function") {
|
|
375
559
|
const result = await type(props, ctx);
|
|
376
560
|
return renderJsxToString(result, ctx);
|
|
@@ -575,15 +759,38 @@ export function useCallback(fn) {
|
|
|
575
759
|
return fn;
|
|
576
760
|
}
|
|
577
761
|
|
|
762
|
+
/**
|
|
763
|
+
* Server-safe fallback for useAction in SSR.
|
|
764
|
+
* @param {string|Function} actionName
|
|
765
|
+
* @param {Object} [options={}]
|
|
766
|
+
* @returns {[Function, boolean, any, any, Function]}
|
|
767
|
+
*/
|
|
768
|
+
export function useAction(actionName, options = {}) {
|
|
769
|
+
return [async () => {}, false, null, null, () => {}];
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
/**
|
|
773
|
+
* Server-safe fallback for useActionState in SSR.
|
|
774
|
+
* @param {string|Function} actionFn
|
|
775
|
+
* @param {any} initialState
|
|
776
|
+
* @returns {[any, Function, boolean]}
|
|
777
|
+
*/
|
|
778
|
+
export function useActionState(actionFn, initialState) {
|
|
779
|
+
return [initialState, async () => {}, false];
|
|
780
|
+
}
|
|
781
|
+
|
|
578
782
|
/**
|
|
579
783
|
* Ultra-fast, zero-dependency JSX-to-JS compiler for Node.js ESM.
|
|
580
784
|
* Compiles standard React JSX syntax (elements, fragments `<>...</>`, spread attributes `{...props}`,
|
|
581
785
|
* nested expressions, boolean props, and component tags) into `h()` calls in memory.
|
|
786
|
+
* Supports TypeScript syntax stripping for `.tsx` files.
|
|
582
787
|
*
|
|
583
788
|
* @param {string} source - Raw JSX module source code.
|
|
789
|
+
* @param {Object} [options={}] - Compiler options.
|
|
790
|
+
* @param {boolean} [options.ts=false] - Whether to strip TypeScript type annotations.
|
|
584
791
|
* @returns {string} Valid JavaScript ESM source code.
|
|
585
792
|
*/
|
|
586
|
-
export function compileJsx(source) {
|
|
793
|
+
export function compileJsx(source, options = {}) {
|
|
587
794
|
let out = "";
|
|
588
795
|
let i = 0;
|
|
589
796
|
const len = source.length;
|
|
@@ -658,7 +865,11 @@ export function compileJsx(source) {
|
|
|
658
865
|
}
|
|
659
866
|
|
|
660
867
|
const preamble = `import { h as __bb_h, Fragment as __bb_Fragment } from "@seip/bluebird-js/core/jsx.js";\n`;
|
|
661
|
-
|
|
868
|
+
const code = preamble + out;
|
|
869
|
+
if (options.ts || options.typescript) {
|
|
870
|
+
return stripTsTypes(code);
|
|
871
|
+
}
|
|
872
|
+
return code;
|
|
662
873
|
}
|
|
663
874
|
|
|
664
875
|
/**
|
|
@@ -1126,8 +1337,24 @@ function parseJsxChildren(source, start, tagName) {
|
|
|
1126
1337
|
let hooksRegistered = false;
|
|
1127
1338
|
|
|
1128
1339
|
/**
|
|
1129
|
-
*
|
|
1130
|
-
*
|
|
1340
|
+
* Strips TypeScript type annotations and declarations from JavaScript code using Node's native
|
|
1341
|
+
* stripTypeScriptTypes if available, or lightweight zero-dependency pattern stripping.
|
|
1342
|
+
*
|
|
1343
|
+
* @param {string} code
|
|
1344
|
+
* @returns {string}
|
|
1345
|
+
*/
|
|
1346
|
+
export function stripTsTypes(code) {
|
|
1347
|
+
try {
|
|
1348
|
+
if (typeof nodeModule?.stripTypeScriptTypes === "function") {
|
|
1349
|
+
return nodeModule.stripTypeScriptTypes(code);
|
|
1350
|
+
}
|
|
1351
|
+
} catch {}
|
|
1352
|
+
return code;
|
|
1353
|
+
}
|
|
1354
|
+
|
|
1355
|
+
/**
|
|
1356
|
+
* Registers Node.js native module customization hooks (`module.registerHooks`) so `.jsx` and `.tsx` files
|
|
1357
|
+
* can import other `.jsx` and `.tsx` components directly without any external build step or disk temp files.
|
|
1131
1358
|
*/
|
|
1132
1359
|
export function registerJsxLoader() {
|
|
1133
1360
|
if (hooksRegistered) return;
|
|
@@ -1138,7 +1365,11 @@ export function registerJsxLoader() {
|
|
|
1138
1365
|
nodeModule.registerHooks({
|
|
1139
1366
|
load(url, context, nextLoad) {
|
|
1140
1367
|
const cleanUrl = url.split("?")[0];
|
|
1141
|
-
|
|
1368
|
+
const isJsx = cleanUrl.endsWith(".jsx");
|
|
1369
|
+
const isTsx = cleanUrl.endsWith(".tsx");
|
|
1370
|
+
const isTs = cleanUrl.endsWith(".ts");
|
|
1371
|
+
|
|
1372
|
+
if (isJsx || isTsx || isTs) {
|
|
1142
1373
|
let rawSource;
|
|
1143
1374
|
if (cleanUrl.startsWith("file:")) {
|
|
1144
1375
|
const filePath = fileURLToPath(cleanUrl);
|
|
@@ -1150,10 +1381,18 @@ export function registerJsxLoader() {
|
|
|
1150
1381
|
? loaded.source
|
|
1151
1382
|
: Buffer.from(loaded.source).toString("utf-8");
|
|
1152
1383
|
}
|
|
1384
|
+
|
|
1385
|
+
let source;
|
|
1386
|
+
if (isTs) {
|
|
1387
|
+
source = stripTsTypes(rawSource);
|
|
1388
|
+
} else {
|
|
1389
|
+
source = compileJsx(rawSource, { ts: isTsx });
|
|
1390
|
+
}
|
|
1391
|
+
|
|
1153
1392
|
return {
|
|
1154
1393
|
format: "module",
|
|
1155
1394
|
shortCircuit: true,
|
|
1156
|
-
source
|
|
1395
|
+
source,
|
|
1157
1396
|
};
|
|
1158
1397
|
}
|
|
1159
1398
|
return nextLoad(url, context);
|
|
@@ -1173,8 +1412,21 @@ export default {
|
|
|
1173
1412
|
Fragment,
|
|
1174
1413
|
Island,
|
|
1175
1414
|
Client,
|
|
1415
|
+
ErrorBoundary,
|
|
1416
|
+
Suspense,
|
|
1417
|
+
Head,
|
|
1418
|
+
Title,
|
|
1419
|
+
Meta,
|
|
1420
|
+
useState,
|
|
1421
|
+
useEffect,
|
|
1422
|
+
useRef,
|
|
1423
|
+
useMemo,
|
|
1424
|
+
useCallback,
|
|
1425
|
+
useAction,
|
|
1426
|
+
useActionState,
|
|
1176
1427
|
isJsxElement,
|
|
1177
1428
|
renderJsxToString,
|
|
1178
1429
|
compileJsx,
|
|
1430
|
+
stripTsTypes,
|
|
1179
1431
|
registerJsxLoader,
|
|
1180
1432
|
};
|
package/core/render.js
CHANGED
|
@@ -5,6 +5,13 @@ import {
|
|
|
5
5
|
Fragment,
|
|
6
6
|
Island,
|
|
7
7
|
Client,
|
|
8
|
+
ErrorBoundary,
|
|
9
|
+
Suspense,
|
|
10
|
+
Head,
|
|
11
|
+
Title,
|
|
12
|
+
Meta,
|
|
13
|
+
useAction,
|
|
14
|
+
useActionState,
|
|
8
15
|
isJsxElement,
|
|
9
16
|
renderJsxToString,
|
|
10
17
|
serializeStyle,
|
|
@@ -270,12 +277,21 @@ export class Render {
|
|
|
270
277
|
} else {
|
|
271
278
|
const layoutView = await ViewLoader.loadLayout(layoutName);
|
|
272
279
|
if (layoutView && (layoutView.type === "js" || layoutView.type === "jsx")) {
|
|
273
|
-
let headData = {
|
|
280
|
+
let headData = {
|
|
281
|
+
title: ctx.title || mergedData.title || "Bluebird App",
|
|
282
|
+
tags: ctx.headTags || [],
|
|
283
|
+
};
|
|
274
284
|
if (view.module.head) {
|
|
275
|
-
|
|
285
|
+
const declaredHead =
|
|
276
286
|
typeof view.module.head === "function"
|
|
277
287
|
? await view.module.head(mergedData, ctx)
|
|
278
288
|
: view.module.head;
|
|
289
|
+
if (declaredHead && typeof declaredHead === "object") {
|
|
290
|
+
Object.assign(headData, declaredHead);
|
|
291
|
+
}
|
|
292
|
+
if (ctx.title) {
|
|
293
|
+
headData.title = ctx.title;
|
|
294
|
+
}
|
|
279
295
|
}
|
|
280
296
|
|
|
281
297
|
const layoutFn =
|
|
@@ -294,6 +310,21 @@ export class Render {
|
|
|
294
310
|
});
|
|
295
311
|
|
|
296
312
|
rendered = await resolveOutputToHtml(layoutOutput, ctx);
|
|
313
|
+
|
|
314
|
+
if (ctx.headTags && ctx.headTags.length > 0 && rendered.includes("</head>")) {
|
|
315
|
+
if (ctx.title) {
|
|
316
|
+
rendered = rendered.replace(
|
|
317
|
+
/<title>[^<]*<\/title>/i,
|
|
318
|
+
`<title>${escapeHtml(ctx.title)}</title>`
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
const extraTags = ctx.headTags.filter(
|
|
322
|
+
(t) => !t.startsWith("<title>") && !rendered.includes(t)
|
|
323
|
+
);
|
|
324
|
+
if (extraTags.length > 0) {
|
|
325
|
+
rendered = rendered.replace("</head>", ` ${extraTags.join("\n ")}\n</head>`);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
297
328
|
} else {
|
|
298
329
|
rendered = bodyHtml;
|
|
299
330
|
}
|
|
@@ -390,19 +421,54 @@ export class Render {
|
|
|
390
421
|
}
|
|
391
422
|
|
|
392
423
|
/**
|
|
393
|
-
* Streams progressive HTML chunks with out-of-order deferred content resolution,
|
|
424
|
+
* Streams progressive HTML chunks with out-of-order deferred content resolution and <Suspense> support,
|
|
394
425
|
* rendering inside the layout unless requested as a partial.
|
|
395
|
-
*
|
|
426
|
+
* If cache TTL is configured and cached response exists, serves instant cache hit without streaming overhead.
|
|
427
|
+
* When streaming a cache-miss response, caches the final assembled HTML for instant subsequent hits.
|
|
396
428
|
*
|
|
397
429
|
* @param {import('express').Response} res - Express response object.
|
|
398
430
|
* @param {string} viewName - Logical view identifier.
|
|
399
431
|
* @param {Object} [data={}] - Initial data and promises.
|
|
432
|
+
* @param {number|Object} [options={}] - Cache duration (seconds) or options ({ ttl }).
|
|
400
433
|
* @returns {Promise<void>}
|
|
401
434
|
*/
|
|
402
|
-
static async stream(res, viewName, data = {}) {
|
|
435
|
+
static async stream(res, viewName, data = {}, options = {}) {
|
|
436
|
+
const ttlOption = typeof options === "number" ? options : options?.ttl;
|
|
437
|
+
const isPartialRequest =
|
|
438
|
+
data._layout === false ||
|
|
439
|
+
res.req?.headers?.["x-bluebird-partial"] === "1" ||
|
|
440
|
+
res.req?.query?._partial === "1";
|
|
441
|
+
|
|
442
|
+
const view = await ViewLoader.load(viewName);
|
|
443
|
+
if (!view) {
|
|
444
|
+
res.status(404).json({ message: `View '${viewName}' not found.` });
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
const resolvedTtl =
|
|
449
|
+
ttlOption !== undefined
|
|
450
|
+
? ttlOption
|
|
451
|
+
: view.module?.cache !== undefined
|
|
452
|
+
? view.module.cache
|
|
453
|
+
: 0;
|
|
454
|
+
|
|
455
|
+
const cacheKey = `view:stream:${isPartialRequest ? "partial:" : ""}${viewName}`;
|
|
456
|
+
if (resolvedTtl > 0 && !DEBUG) {
|
|
457
|
+
const cached = await RenderCache.lookup(cacheKey);
|
|
458
|
+
if (cached) {
|
|
459
|
+
res.set("Content-Type", "text/html; charset=utf-8");
|
|
460
|
+
if (isPartialRequest) {
|
|
461
|
+
res.set("X-Bluebird-Partial", "1");
|
|
462
|
+
}
|
|
463
|
+
res.set("X-Bluebird-Cache", "HIT");
|
|
464
|
+
return res.send(cached);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
403
468
|
res.setHeader("Content-Type", "text/html; charset=utf-8");
|
|
404
469
|
res.setHeader("Transfer-Encoding", "chunked");
|
|
405
470
|
res.setHeader("X-Content-Type-Options", "nosniff");
|
|
471
|
+
res.setHeader("X-Bluebird-Cache", "MISS");
|
|
406
472
|
|
|
407
473
|
const deferredTokens = [];
|
|
408
474
|
const streamData = { ...data };
|
|
@@ -417,30 +483,25 @@ export class Render {
|
|
|
417
483
|
},
|
|
418
484
|
});
|
|
419
485
|
|
|
420
|
-
const view = await ViewLoader.load(viewName);
|
|
421
|
-
if (!view) {
|
|
422
|
-
res.status(404).json({ message: `View '${viewName}' not found.` });
|
|
423
|
-
return;
|
|
424
|
-
}
|
|
425
|
-
|
|
426
486
|
const locals = res.locals || {};
|
|
427
487
|
const appLocals = res.req?.app?.locals || {};
|
|
428
|
-
const isPartialRequest =
|
|
429
|
-
data._layout === false ||
|
|
430
|
-
res.req?.headers?.["x-bluebird-partial"] === "1" ||
|
|
431
|
-
res.req?.query?._partial === "1";
|
|
432
488
|
|
|
433
489
|
let initialHtml = "";
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
490
|
+
const ctx = {
|
|
491
|
+
req: res.req,
|
|
492
|
+
res,
|
|
493
|
+
user: res.req?.user,
|
|
494
|
+
locals,
|
|
495
|
+
appLocals,
|
|
496
|
+
data: proxyData,
|
|
497
|
+
isStreaming: true,
|
|
498
|
+
suspenseCounter: 0,
|
|
499
|
+
suspenseTasks: [],
|
|
500
|
+
headTags: [],
|
|
501
|
+
title: "",
|
|
502
|
+
};
|
|
443
503
|
|
|
504
|
+
if (view.type === "js" || view.type === "jsx") {
|
|
444
505
|
if (typeof view.module.load === "function") {
|
|
445
506
|
const loadedData = await view.module.load(ctx);
|
|
446
507
|
if (loadedData && typeof loadedData === "object") {
|
|
@@ -471,12 +532,21 @@ export class Render {
|
|
|
471
532
|
} else {
|
|
472
533
|
const layoutView = await ViewLoader.loadLayout(layoutName);
|
|
473
534
|
if (layoutView && (layoutView.type === "js" || layoutView.type === "jsx")) {
|
|
474
|
-
let headData = {
|
|
535
|
+
let headData = {
|
|
536
|
+
title: ctx.title || mergedData.title || "Bluebird App",
|
|
537
|
+
tags: ctx.headTags || [],
|
|
538
|
+
};
|
|
475
539
|
if (view.module.head) {
|
|
476
|
-
|
|
540
|
+
const declaredHead =
|
|
477
541
|
typeof view.module.head === "function"
|
|
478
542
|
? await view.module.head(mergedData, ctx)
|
|
479
543
|
: view.module.head;
|
|
544
|
+
if (declaredHead && typeof declaredHead === "object") {
|
|
545
|
+
Object.assign(headData, declaredHead);
|
|
546
|
+
}
|
|
547
|
+
if (ctx.title) {
|
|
548
|
+
headData.title = ctx.title;
|
|
549
|
+
}
|
|
480
550
|
}
|
|
481
551
|
|
|
482
552
|
const layoutFn =
|
|
@@ -495,6 +565,24 @@ export class Render {
|
|
|
495
565
|
});
|
|
496
566
|
|
|
497
567
|
initialHtml = await resolveOutputToHtml(layoutOutput, ctx);
|
|
568
|
+
|
|
569
|
+
if (ctx.headTags && ctx.headTags.length > 0 && initialHtml.includes("</head>")) {
|
|
570
|
+
if (ctx.title) {
|
|
571
|
+
initialHtml = initialHtml.replace(
|
|
572
|
+
/<title>[^<]*<\/title>/i,
|
|
573
|
+
`<title>${escapeHtml(ctx.title)}</title>`
|
|
574
|
+
);
|
|
575
|
+
}
|
|
576
|
+
const extraTags = ctx.headTags.filter(
|
|
577
|
+
(t) => !t.startsWith("<title>") && !initialHtml.includes(t)
|
|
578
|
+
);
|
|
579
|
+
if (extraTags.length > 0) {
|
|
580
|
+
initialHtml = initialHtml.replace(
|
|
581
|
+
"</head>",
|
|
582
|
+
` ${extraTags.join("\n ")}\n</head>`
|
|
583
|
+
);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
498
586
|
} else {
|
|
499
587
|
initialHtml = bodyHtml;
|
|
500
588
|
}
|
|
@@ -504,6 +592,7 @@ export class Render {
|
|
|
504
592
|
}
|
|
505
593
|
|
|
506
594
|
res.write(initialHtml);
|
|
595
|
+
let assembledHtml = initialHtml;
|
|
507
596
|
|
|
508
597
|
if (deferredTokens.length > 0) {
|
|
509
598
|
await Promise.all(
|
|
@@ -514,6 +603,10 @@ export class Render {
|
|
|
514
603
|
const strContent = await resolveOutputToHtml(finalContent);
|
|
515
604
|
const script = `<template id="bb-defer-tpl-${token.prop}">${strContent}</template><script>(function(){var el=document.getElementById("bb-defer-${token.prop}");var tpl=document.getElementById("bb-defer-tpl-${token.prop}");if(el&&tpl){el.innerHTML="";el.appendChild(tpl.content);tpl.remove();if(window.Bluebird&&window.Bluebird.hydrate){window.Bluebird.hydrate(el);}}})();</script>`;
|
|
516
605
|
res.write(script);
|
|
606
|
+
assembledHtml = assembledHtml.replace(
|
|
607
|
+
new RegExp(`<div id="bb-defer-${token.prop}">[\\s\\S]*?<\\/div>`),
|
|
608
|
+
`<div id="bb-defer-${token.prop}">${strContent}</div>`
|
|
609
|
+
);
|
|
517
610
|
} catch (err) {
|
|
518
611
|
res.write(`<script>console.error("Deferred stream error on ${token.prop}:", ${safeJson(err.message)});</script>`);
|
|
519
612
|
}
|
|
@@ -521,6 +614,32 @@ export class Render {
|
|
|
521
614
|
);
|
|
522
615
|
}
|
|
523
616
|
|
|
617
|
+
if (ctx.suspenseTasks && ctx.suspenseTasks.length > 0) {
|
|
618
|
+
await Promise.all(
|
|
619
|
+
ctx.suspenseTasks.map(async (taskPromise) => {
|
|
620
|
+
try {
|
|
621
|
+
const task = await taskPromise;
|
|
622
|
+
if (task.success) {
|
|
623
|
+
const script = `<template id="${task.id}-tpl">${task.html}</template><script>(function(){var el=document.getElementById("${task.id}");var tpl=document.getElementById("${task.id}-tpl");if(el&&tpl){el.innerHTML="";el.appendChild(tpl.content);tpl.remove();if(window.Bluebird&&window.Bluebird.hydrate){window.Bluebird.hydrate(el);}}})();</script>`;
|
|
624
|
+
res.write(script);
|
|
625
|
+
assembledHtml = assembledHtml.replace(
|
|
626
|
+
new RegExp(`<div id="${task.id}">[\\s\\S]*?<\\/div>`),
|
|
627
|
+
`<div id="${task.id}">${task.html}</div>`
|
|
628
|
+
);
|
|
629
|
+
} else {
|
|
630
|
+
res.write(`<script>console.error("Suspense error on ${task.id}:", ${safeJson(task.error?.message || "Render error")});</script>`);
|
|
631
|
+
}
|
|
632
|
+
} catch (err) {
|
|
633
|
+
res.write(`<script>console.error("Suspense execution error:", ${safeJson(err.message)});</script>`);
|
|
634
|
+
}
|
|
635
|
+
})
|
|
636
|
+
);
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
if (resolvedTtl > 0 && !DEBUG) {
|
|
640
|
+
await RenderCache.store(cacheKey, assembledHtml, resolvedTtl);
|
|
641
|
+
}
|
|
642
|
+
|
|
524
643
|
res.end();
|
|
525
644
|
}
|
|
526
645
|
|
|
@@ -593,5 +712,17 @@ export class Render {
|
|
|
593
712
|
}
|
|
594
713
|
}
|
|
595
714
|
|
|
596
|
-
export {
|
|
715
|
+
export {
|
|
716
|
+
h,
|
|
717
|
+
Fragment,
|
|
718
|
+
Island,
|
|
719
|
+
Client,
|
|
720
|
+
ErrorBoundary,
|
|
721
|
+
Suspense,
|
|
722
|
+
Head,
|
|
723
|
+
Title,
|
|
724
|
+
Meta,
|
|
725
|
+
useAction,
|
|
726
|
+
useActionState,
|
|
727
|
+
};
|
|
597
728
|
export default Render;
|