feat: streaming rendering with suspense (#59)

This commit introduces support for streaming server side rendering using
<Suspense>.
This commit is contained in:
Luca Casonato
2021-09-26 01:21:02 +02:00
committed by GitHub
parent 75afa6daca
commit 7cf4ca07ed
11 changed files with 256 additions and 48 deletions
+11 -3
View File
@@ -144,11 +144,12 @@ export class ServerContext {
params: Record<string, string | string[]>,
) => {
if (page.component === undefined) return undefined;
// deno-lint-ignore require-await
const render = async () => {
const preloads = page.runtimeJS
? this.#bundler.getPreloads(bundlePath).map(bundleAssetUrl)
: [];
const body = await internalRender({
const body = internalRender({
page,
imports,
preloads,
@@ -156,7 +157,15 @@ export class ServerContext {
url: new URL(req.url),
params,
});
return new Response(body, {
const bodyStream = new ReadableStream<Uint8Array>({
async start(controller) {
for await (const chunk of body) {
controller.enqueue(new TextEncoder().encode(chunk));
}
controller.close();
},
});
return new Response(bodyStream, {
status: 200,
headers: {
"content-type": "text/html; charset=utf-8",
@@ -243,7 +252,6 @@ const DEFAULT_RENDERER: Renderer = {
render(_ctx, render) {
render();
},
postRender(_ctx) {},
};
/**