deno.land / x / fresh@1.1.1 / src / server / render.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import { renderToString } from "preact-render-to-string";import { ComponentChildren, ComponentType, h, options } from "preact";import { AppModule, ErrorPage, Island, Plugin, PluginRenderFunctionResult, PluginRenderResult, PluginRenderStyleTag, RenderFunction, Route, UnknownPage,} from "./types.ts";import { HEAD_CONTEXT } from "../runtime/head.ts";import { CSP_CONTEXT, nonce, NONE, UNSAFE_INLINE } from "../runtime/csp.ts";import { ContentSecurityPolicy } from "../runtime/csp.ts";import { bundleAssetUrl } from "./constants.ts";import { assetHashingHook } from "../runtime/utils.ts";import { htmlEscapeJsonString } from "./htmlescape.ts";
export interface RenderOptions<Data> { route: Route<Data> | UnknownPage | ErrorPage; islands: Island[]; plugins: Plugin[]; app: AppModule; imports: string[]; preloads: string[]; url: URL; params: Record<string, string | string[]>; renderFn: RenderFunction; data?: Data; error?: unknown; lang?: string;}
export type InnerRenderFunction = () => string;
export class RenderContext { #id: string; #state: Map<string, unknown> = new Map(); #styles: string[] = []; #url: URL; #route: string; #lang: string;
constructor(id: string, url: URL, route: string, lang: string) { this.#id = id; this.#url = url; this.#route = route; this.#lang = lang; }
/** A unique ID for this logical JIT render. */ get id(): string { return this.#id; }
/** * State that is persisted between multiple renders with the same render * context. This is useful because one logical JIT render could have multiple * preact render passes due to suspense. */ get state(): Map<string, unknown> { return this.#state; }
/** * All of the CSS style rules that should be inlined into the document. * Adding to this list across multiple renders is supported (even across * suspense!). The CSS rules will always be inserted on the client in the * order specified here. */ get styles(): string[] { return this.#styles; }
/** The URL of the page being rendered. */ get url(): URL { return this.#url; }
/** The route matcher (e.g. /blog/:id) that the request matched for this page * to be rendered. */ get route(): string { return this.#route; }
/** The language of the page being rendered. Defaults to "en". */ get lang(): string { return this.#lang; } set lang(lang: string) { this.#lang = lang; }}
function defaultCsp() { return { directives: { defaultSrc: [NONE], styleSrc: [UNSAFE_INLINE] }, reportOnly: false, };}
/** * This function renders out a page. Rendering is synchronous and non streaming. * Suspense boundaries are not supported. */export async function render<Data>( opts: RenderOptions<Data>,): Promise<[string, ContentSecurityPolicy | undefined]> { const props: Record<string, unknown> = { params: opts.params, url: opts.url, route: opts.route.pattern, data: opts.data, }; if (opts.error) { props.error = opts.error; }
const csp: ContentSecurityPolicy | undefined = opts.route.csp ? defaultCsp() : undefined; const headComponents: ComponentChildren[] = [];
const vnode = h(CSP_CONTEXT.Provider, { value: csp, children: h(HEAD_CONTEXT.Provider, { value: headComponents, children: h(opts.app.default, { Component() { return h(opts.route.component! as ComponentType<unknown>, props); }, }), }), });
const ctx = new RenderContext( crypto.randomUUID(), opts.url, opts.route.pattern, opts.lang ?? "en", );
if (csp) { // Clear the csp const newCsp = defaultCsp(); csp.directives = newCsp.directives; csp.reportOnly = newCsp.reportOnly; } // Clear the head components headComponents.splice(0, headComponents.length);
// Setup the interesting VNode types ISLANDS.splice(0, ISLANDS.length, ...opts.islands);
// Clear the encountered vnodes ENCOUNTERED_ISLANDS.clear();
// Clear the island props ISLAND_PROPS = [];
let bodyHtml: string | null = null;
function realRender(): string { bodyHtml = renderToString(vnode); return bodyHtml; }
const plugins = opts.plugins.filter((p) => p.render !== null); const renderResults: [Plugin, PluginRenderResult][] = [];
function render(): PluginRenderFunctionResult { const plugin = plugins.shift(); if (plugin) { const res = plugin.render!({ render }); if (res === undefined) { throw new Error( `${plugin?.name}'s render hook did not return a PluginRenderResult object.`, ); } renderResults.push([plugin, res]); } else { realRender(); } if (bodyHtml === null) { throw new Error( `The 'render' function was not called by ${plugin?.name}'s render hook.`, ); } return { htmlText: bodyHtml, requiresHydration: ENCOUNTERED_ISLANDS.size > 0, }; }
await opts.renderFn(ctx, () => render().htmlText);
if (bodyHtml === null) { throw new Error("The `render` function was not called by the renderer."); }
bodyHtml = bodyHtml as string;
const imports = opts.imports.map((url) => { const randomNonce = crypto.randomUUID().replace(/-/g, ""); if (csp) { csp.directives.scriptSrc = [ ...csp.directives.scriptSrc ?? [], nonce(randomNonce), ]; } return [url, randomNonce] as const; });
const state: [islands: unknown[], plugins: unknown[]] = [ISLAND_PROPS, []]; const styleTags: PluginRenderStyleTag[] = [];
let script = `const STATE_COMPONENT = document.getElementById("__FRSH_STATE");const STATE = JSON.parse(STATE_COMPONENT?.textContent ?? "[[],[]]");`;
for (const [plugin, res] of renderResults) { for (const hydrate of res.scripts ?? []) { const i = state[1].push(hydrate.state) - 1; const randomNonce = crypto.randomUUID().replace(/-/g, ""); if (csp) { csp.directives.scriptSrc = [ ...csp.directives.scriptSrc ?? [], nonce(randomNonce), ]; } const url = bundleAssetUrl( `/plugin-${plugin.name}-${hydrate.entrypoint}.js`, ); imports.push([url, randomNonce] as const);
script += `import p${i} from "${url}";p${i}(STATE[1][${i}]);`; } styleTags.splice(styleTags.length, 0, ...res.styles ?? []); }
if (ENCOUNTERED_ISLANDS.size > 0) { // Load the main.js script { const randomNonce = crypto.randomUUID().replace(/-/g, ""); if (csp) { csp.directives.scriptSrc = [ ...csp.directives.scriptSrc ?? [], nonce(randomNonce), ]; } const url = bundleAssetUrl("/main.js"); imports.push([url, randomNonce] as const); }
script += `import { revive } from "${bundleAssetUrl("/main.js")}";`;
// Prepare the inline script that loads and revives the islands let islandRegistry = ""; for (const island of ENCOUNTERED_ISLANDS) { const randomNonce = crypto.randomUUID().replace(/-/g, ""); if (csp) { csp.directives.scriptSrc = [ ...csp.directives.scriptSrc ?? [], nonce(randomNonce), ]; } const url = bundleAssetUrl(`/island-${island.id}.js`); imports.push([url, randomNonce] as const); script += `import ${island.name} from "${url}";`; islandRegistry += `${island.id}:${island.name},`; } script += `revive({${islandRegistry}}, STATE[0]);`; }
if (state[0].length > 0 || state[1].length > 0) { // Append state to the body bodyHtml += `<script id="__FRSH_STATE" type="application/json">${ htmlEscapeJsonString(JSON.stringify(state)) }</script>`;
// Append the inline script to the body const randomNonce = crypto.randomUUID().replace(/-/g, ""); if (csp) { csp.directives.scriptSrc = [ ...csp.directives.scriptSrc ?? [], nonce(randomNonce), ]; } bodyHtml += `<script type="module" nonce="${randomNonce}">${script}</script>`; }
if (ctx.styles.length > 0) { const node = h("style", { id: "__FRSH_STYLE", dangerouslySetInnerHTML: { __html: ctx.styles.join("\n") }, }); headComponents.splice(0, 0, node); }
for (const style of styleTags) { const node = h("style", { id: style.id, dangerouslySetInnerHTML: { __html: style.cssText }, media: style.media, }); headComponents.splice(0, 0, node); }
const html = template({ bodyHtml, headComponents, imports, preloads: opts.preloads, lang: ctx.lang, });
return [html, csp];}
export interface TemplateOptions { bodyHtml: string; headComponents: ComponentChildren[]; imports: (readonly [string, string])[]; preloads: string[]; lang: string;}
export function template(opts: TemplateOptions): string { const page = h( "html", { lang: opts.lang }, h( "head", null, h("meta", { charSet: "UTF-8" }), h("meta", { name: "viewport", content: "width=device-width, initial-scale=1.0", }), opts.preloads.map((src) => h("link", { rel: "modulepreload", href: src }) ), opts.imports.map(([src, nonce]) => h("script", { src: src, nonce: nonce, type: "module" }) ), opts.headComponents, ), h("body", { dangerouslySetInnerHTML: { __html: opts.bodyHtml } }), ); return "<!DOCTYPE html>" + renderToString(page);}
// Set up a preact option hook to track when vnode with custom functions are// created.const ISLANDS: Island[] = [];const ENCOUNTERED_ISLANDS: Set<Island> = new Set([]);let ISLAND_PROPS: unknown[] = [];const originalHook = options.vnode;let ignoreNext = false;options.vnode = (vnode) => { assetHashingHook(vnode); const originalType = vnode.type as ComponentType<unknown>; if (typeof vnode.type === "function") { const island = ISLANDS.find((island) => island.component === originalType); if (island) { if (ignoreNext) { ignoreNext = false; return; } ENCOUNTERED_ISLANDS.add(island); vnode.type = (props) => { ignoreNext = true; const child = h(originalType, props); ISLAND_PROPS.push(props); return h( `!--frsh-${island.id}:${ISLAND_PROPS.length - 1}--`, null, child, ); }; } } if (originalHook) originalHook(vnode);};
fresh

Version Info

Tagged at
a year ago