@crombolo/render 0.56.0 → 0.57.0
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 +5 -4
- package/src/context.ts +13 -0
- package/src/extension.ts +29 -0
- package/src/index.ts +1 -0
- package/src/renderer.ts +15 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crombolo/render",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.57.0",
|
|
4
4
|
"description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. TSX server rendering and the published-content model for crombolo.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Crombolo",
|
|
@@ -24,7 +24,8 @@
|
|
|
24
24
|
"./jsx-runtime": "./src/jsx-runtime.ts",
|
|
25
25
|
"./jsx-dev-runtime": "./src/jsx-runtime.ts",
|
|
26
26
|
"./loose/jsx-runtime": "./src/loose/jsx-runtime.ts",
|
|
27
|
-
"./loose/jsx-dev-runtime": "./src/loose/jsx-runtime.ts"
|
|
27
|
+
"./loose/jsx-dev-runtime": "./src/loose/jsx-runtime.ts",
|
|
28
|
+
"./html": "./src/html.ts"
|
|
28
29
|
},
|
|
29
30
|
"files": [
|
|
30
31
|
"src",
|
|
@@ -34,8 +35,8 @@
|
|
|
34
35
|
"access": "public"
|
|
35
36
|
},
|
|
36
37
|
"dependencies": {
|
|
37
|
-
"@crombolo/common": "0.
|
|
38
|
-
"@crombolo/core": "0.
|
|
38
|
+
"@crombolo/common": "0.57.0",
|
|
39
|
+
"@crombolo/core": "0.57.0",
|
|
39
40
|
"@types/react": "^19.3.0"
|
|
40
41
|
}
|
|
41
42
|
}
|
package/src/context.ts
CHANGED
|
@@ -28,6 +28,8 @@ interface RenderScope {
|
|
|
28
28
|
ids: { next: number }
|
|
29
29
|
/** A block's component by element type alias, from the page's own views generation. */
|
|
30
30
|
blockComponent?: BlockComponentLoader
|
|
31
|
+
/** An extension's component, `<id>/<name>`, for `<Extension use>` (story 312). */
|
|
32
|
+
extensionComponent?: ExtensionComponentLoader
|
|
31
33
|
/** A draft is being previewed in the back office. */
|
|
32
34
|
preview?: boolean
|
|
33
35
|
/** The signed-in member's anti-forgery token, for `<AntiforgeryMeta />`. */
|
|
@@ -65,9 +67,15 @@ export type BlockComponentLoader = (
|
|
|
65
67
|
) => Promise<((props: never) => Child | Promise<Child>) | undefined>
|
|
66
68
|
|
|
67
69
|
/** What a page render carries besides its props: the search, blocks, and whether it is a preview. */
|
|
70
|
+
/** An extension's component as a render function, or undefined when no extension offers it. */
|
|
71
|
+
export type ExtensionComponentLoader = (
|
|
72
|
+
alias: string,
|
|
73
|
+
) => ((props: object) => Child | Promise<Child>) | undefined
|
|
74
|
+
|
|
68
75
|
export interface RenderScopeOptions {
|
|
69
76
|
search?: SiteSearchPort
|
|
70
77
|
blockComponent?: BlockComponentLoader
|
|
78
|
+
extensionComponent?: ExtensionComponentLoader
|
|
71
79
|
preview?: boolean
|
|
72
80
|
antiforgery?: string
|
|
73
81
|
request?: RequestValues
|
|
@@ -208,6 +216,11 @@ export function actionState<T = unknown>(action: unknown): ActionState<T> | unde
|
|
|
208
216
|
}
|
|
209
217
|
|
|
210
218
|
/** The signed-in member's anti-forgery token for this render, or undefined for a visitor. */
|
|
219
|
+
/** The loader for an extension's components, while a page renders; undefined outside one. */
|
|
220
|
+
export function currentExtensionComponent(): ExtensionComponentLoader | undefined {
|
|
221
|
+
return store.getStore()?.extensionComponent
|
|
222
|
+
}
|
|
223
|
+
|
|
211
224
|
export function currentAntiforgery(): string | undefined {
|
|
212
225
|
return memberAntiforgery(within('<AntiforgeryMeta />'))
|
|
213
226
|
}
|
package/src/extension.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `<Extension use="brochures/card" … />`: an extension's component in a site's
|
|
3
|
+
* page (story 312). It renders in the extension's own process, given the page's
|
|
4
|
+
* props and these, and its HTML goes here. Children render here first and go as
|
|
5
|
+
* HTML. Nothing renders where no extension offers it, or where it is not granted
|
|
6
|
+
* `templates`, and the site's log says why. docs/writing-components.md.
|
|
7
|
+
*/
|
|
8
|
+
import { currentExtensionComponent, usePage } from './context.ts'
|
|
9
|
+
import { RawHtml } from './html.ts'
|
|
10
|
+
import { type Child, renderToString } from './jsx-runtime.ts'
|
|
11
|
+
|
|
12
|
+
export interface ExtensionProps {
|
|
13
|
+
/** `<extension id>/<component>`. */
|
|
14
|
+
use: string
|
|
15
|
+
children?: Child
|
|
16
|
+
[prop: string]: unknown
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export async function Extension({ use, children, ...props }: ExtensionProps): Promise<RawHtml> {
|
|
20
|
+
const render = currentExtensionComponent()?.(use)
|
|
21
|
+
if (!render) return new RawHtml('')
|
|
22
|
+
const inner = children === undefined ? undefined : new RawHtml(await renderToString(children))
|
|
23
|
+
const rendered = await render({
|
|
24
|
+
...usePage(),
|
|
25
|
+
...props,
|
|
26
|
+
...(inner ? { children: inner } : {}),
|
|
27
|
+
})
|
|
28
|
+
return rendered instanceof RawHtml ? rendered : new RawHtml(await renderToString(rendered))
|
|
29
|
+
}
|
package/src/index.ts
CHANGED
package/src/renderer.ts
CHANGED
|
@@ -72,6 +72,12 @@ export interface RenderOptions {
|
|
|
72
72
|
* with (story 209); by default the generation's own build.
|
|
73
73
|
*/
|
|
74
74
|
islandsManifest?: (generation: string | undefined) => string | undefined
|
|
75
|
+
/**
|
|
76
|
+
* Components an extension owns (story 312): `<id>/<name>`, or a block's
|
|
77
|
+
* `blocks/<alias>`, where the site has none of its own. Each renders in the
|
|
78
|
+
* extension's process.
|
|
79
|
+
*/
|
|
80
|
+
extensionComponents?: { module(alias: string): ComponentModule | undefined }
|
|
75
81
|
}
|
|
76
82
|
|
|
77
83
|
export const DEFAULT_RENDER_TIMEOUT_MS = 10_000
|
|
@@ -228,6 +234,8 @@ export class Renderer {
|
|
|
228
234
|
#renderTimeoutMs: number
|
|
229
235
|
#islandsManifest: (generation: string | undefined) => string | undefined
|
|
230
236
|
|
|
237
|
+
#extensionComponents: RenderOptions['extensionComponents']
|
|
238
|
+
|
|
231
239
|
constructor(options: RenderOptions) {
|
|
232
240
|
this.#cache = options.cache
|
|
233
241
|
// Resolved to an absolute path: a dynamic import of a relative specifier
|
|
@@ -235,6 +243,7 @@ export class Renderer {
|
|
|
235
243
|
this.#componentsDir = resolve(options.componentsDir)
|
|
236
244
|
this.#snapshots = options.snapshots
|
|
237
245
|
this.#renderTimeoutMs = options.renderTimeoutMs ?? DEFAULT_RENDER_TIMEOUT_MS
|
|
246
|
+
this.#extensionComponents = options.extensionComponents
|
|
238
247
|
this.#islandsManifest =
|
|
239
248
|
options.islandsManifest ??
|
|
240
249
|
((generation) => (generation ? `${ISLANDS_PATH}/${generation}/manifest.json` : undefined))
|
|
@@ -577,6 +586,10 @@ export class Renderer {
|
|
|
577
586
|
preview: request.preview,
|
|
578
587
|
antiforgery: request.antiforgery,
|
|
579
588
|
blockComponent: (blockAlias) => this.blockComponent(blockAlias, used),
|
|
589
|
+
extensionComponent: (use) =>
|
|
590
|
+
this.#extensionComponents?.module(use)?.default as
|
|
591
|
+
| ((props: object) => Child | Promise<Child>)
|
|
592
|
+
| undefined,
|
|
580
593
|
request: { headers: shared ? new Headers() : (request.headers ?? new Headers()) },
|
|
581
594
|
site: request.site,
|
|
582
595
|
personal: shared ? { value: false } : personal,
|
|
@@ -662,7 +675,8 @@ export class Renderer {
|
|
|
662
675
|
|
|
663
676
|
async #load(alias: string, used?: Set<string>): Promise<ComponentModule | undefined> {
|
|
664
677
|
const file = this.componentPath(alias)
|
|
665
|
-
|
|
678
|
+
// The site's own component wins; an extension's renders in its own process.
|
|
679
|
+
if (!file) return this.#extensionComponents?.module(alias)
|
|
666
680
|
used?.add(file)
|
|
667
681
|
// The path carries the generation, so one entry per view per generation and
|
|
668
682
|
// a swap never answers from the generation before it.
|