-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
render.ts
64 lines (58 loc) · 1.65 KB
/
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
import { createElement as h } from "react";
import { RenderToReadableStreamOptions } from "react-dom/server";
import { flushEffectHandler, UltraProvider } from "./provider.ts";
import { fromFileUrl } from "./deps.ts";
import type { Context } from "./types.ts";
import { log } from "./logger.ts";
import { continueFromInitialStream, renderToInitialStream } from "./stream.ts";
import { ImportMap } from "./types.ts";
type RenderToStreamOptions = RenderToReadableStreamOptions & {
importMap: ImportMap | undefined;
assetManifest: Map<string, string> | undefined;
generateStaticHTML?: boolean;
flushEffectsToHead?: boolean;
disableHydration?: boolean;
};
export async function renderToStream(
App: JSX.Element,
context: Context | undefined,
options: RenderToStreamOptions,
) {
const {
generateStaticHTML = false,
disableHydration = false,
flushEffectsToHead = true,
importMap,
assetManifest,
} = options;
/**
* For each bootstrapModule we convert from a file url (file:///project/client.tsx) to
* a path string (/project/client.tsx).
*/
options.bootstrapModules = disableHydration
? []
: options?.bootstrapModules?.map(
(url) => url.startsWith("file://") ? fromFileUrl(url) : url,
);
options.onError = (error) => {
log.error(error);
};
const renderStream = await renderToInitialStream({
element: h(
UltraProvider,
{
context,
assetManifest,
children: App,
},
),
options,
});
return await continueFromInitialStream(renderStream, {
generateStaticHTML,
disableHydration,
flushEffectsToHead,
flushEffectHandler,
importMap,
});
}