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
+125 -13
View File
@@ -1,10 +1,11 @@
/** @jsx h */
import { renderToString } from "./deps.ts";
import { ComponentChild, h } from "../runtime/deps.ts";
import { ComponentChild, ComponentChildren, h } from "../runtime/deps.ts";
import { DATA_CONTEXT } from "../runtime/hooks.ts";
import { Page, Renderer } from "./types.ts";
import { PageProps } from "../runtime/types.ts";
import { SUSPENSE_CONTEXT } from "../runtime/suspense.ts";
export interface RenderOptions {
page: Page;
@@ -20,7 +21,7 @@ export type RenderFn = () => void;
export class RenderContext {
#id: string;
#state: Map<string, unknown> = new Map();
#head: ComponentChild[] = [];
#styles: string[] = [];
#url: URL;
#route: string;
#lang = "en";
@@ -46,10 +47,13 @@ export class RenderContext {
}
/**
* Items to add to the <head> for this render.
* 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 head(): ComponentChild[] {
return this.#head;
get styles(): string[] {
return this.#styles;
}
/** The URL of the page being rendered. */
@@ -74,20 +78,49 @@ export class RenderContext {
const MAX_SUSPENSE_DEPTH = 10;
export async function render(opts: RenderOptions): Promise<string> {
/**
* This function renders out a page. Rendering is asynchronous, and streaming.
* Rendering happens in multiple steps, because of the need to handle suspense.
*
* 1. The page's vnode tree is constructed.
* 2. The page's vnode tree is passed to the renderer.
* - If the rendering throws a promise, the promise is awaited before
* continuing. This allows the renderer to handle async hooks.
* - Once the rendering throws no more promises, the initial render is
* complete and a body string is returned.
* - During rendering, every time a `<Suspense>` is rendered, it, and it's
* attached children are recorded for later rendering.
* 3. Once the inital render is complete, the body string is fitted into the
* HTML wrapper template.
* 4. The full inital render in the template is yielded to be sent to the
* client.
* 5. Now the suspended vnodes are rendered. These are individually rendered
* like described in step 2 above. Once each node is done rendering, it
* wrapped in some boilderplate HTML, and suffixed with some JS, and then
* sent to the client. On the client the HTML will be slotted into the DOM
* at the location of the original `<Suspense>` node.
*/
export async function* render(opts: RenderOptions): AsyncIterable<string> {
const props = { params: opts.params, url: opts.url, route: opts.page.route };
const dataCache = new Map();
const suspenseQueue: ComponentChildren[] = [];
const vnode = h(DATA_CONTEXT.Provider, {
value: dataCache,
children: h(opts.page.component!, props),
children: h(SUSPENSE_CONTEXT.Provider, {
value: suspenseQueue,
children: h(opts.page.component!, props),
}),
});
const ctx = new RenderContext(crypto.randomUUID(), opts.url, opts.page.route);
let suspended = 0;
const renderWithRenderer = (): string | Promise<string> => {
// Clear the suspense queue
suspenseQueue.splice(0, suspenseQueue.length);
if (++suspended > MAX_SUSPENSE_DEPTH) {
throw new Error(
`Reached maximum suspense depth of ${MAX_SUSPENSE_DEPTH}.`,
@@ -122,8 +155,6 @@ export async function render(opts: RenderOptions): Promise<string> {
const bodyHtml = await renderWithRenderer();
opts.renderer.postRender(ctx, bodyHtml);
let templateProps: {
props: PageProps;
data?: [string, unknown][];
@@ -142,18 +173,96 @@ export async function render(opts: RenderOptions): Promise<string> {
bodyHtml,
imports: opts.imports,
preloads: opts.preloads,
head: ctx.head,
styles: ctx.styles,
props: templateProps,
lang: ctx.lang,
});
return html;
let clientStyles = [...ctx.styles];
yield html;
if (suspenseQueue.length > 0) {
// minified client-side JS
yield `<script>(()=>{window.$SR=(e=>{const t=document.getElementById("S:"+e),o=document.getElementById("E:"+e),d=document.getElementById("R:"+e);for(d.parentNode.removeChild(d);t.nextSibling!==o;)t.parentNode.removeChild(t.nextSibling);for(;d.firstChild;)t.parentNode.insertBefore(d.firstChild,o);t.parentNode.removeChild(t),o.parentNode.removeChild(o)});const e=document.getElementById("__FRSH_STYLE"),t=e.childNodes[0].textContent.split("\n");e.removeChild(e.firstChild);for(const o of t)e.append(document.createTextNode(o));window.$ST=(t=>{for(const[o,d]of t)e.insertBefore(document.createTextNode(o),e.childNodes[d])})})();</script>`
.replaceAll("\n", "\\n");
}
// TODO(lucacasonato): parallelize this
for (const [id, children] of suspenseQueue.entries()) {
const fragment = await suspenseRender(opts.renderer, ctx, id + 1, children);
const cssInserts: [string, number][] = [];
for (const [i, style] of ctx.styles.entries()) {
if (!clientStyles.includes(style)) {
cssInserts.push([style, i]);
}
}
clientStyles = [...ctx.styles];
if (cssInserts.length > 0) {
yield `<script>$ST(${JSON.stringify(cssInserts)});</script>`;
}
yield fragment;
}
}
export async function suspenseRender(
renderer: Renderer,
ctx: RenderContext,
id: number,
children: ComponentChildren,
): Promise<string> {
const dataCache = new Map();
const vnode = h(DATA_CONTEXT.Provider, {
value: dataCache,
children,
});
let suspended = 0;
const renderWithRenderer = (): string | Promise<string> => {
if (++suspended > MAX_SUSPENSE_DEPTH) {
throw new Error(
`Reached maximum suspense depth of ${MAX_SUSPENSE_DEPTH}.`,
);
}
let body: string | null = null;
let promise: Promise<unknown> | null = null;
function render() {
try {
body = renderToString(vnode);
} catch (e) {
if (e && e.then) {
promise = e;
return;
}
throw e;
}
}
renderer.render(ctx, render);
if (body !== null) {
return body;
} else if (promise !== null) {
return (promise as Promise<unknown>).then(renderWithRenderer);
} else {
throw new Error("`render` function not called by renderer.");
}
};
const html = await renderWithRenderer();
return `<div hidden id="R:${id}">${html}</div><script>$SR(${id})</script>`;
}
export interface TemplateOptions {
bodyHtml: string;
imports: string[];
head: ComponentChild[];
styles: string[];
preloads: string[];
props: unknown;
lang: string;
@@ -165,7 +274,10 @@ export function template(opts: TemplateOptions): string {
<head>
{opts.preloads.map((src) => <link rel="modulepreload" href={src} />)}
{opts.imports.map((src) => <script src={src} type="module"></script>)}
{opts.head}
<style
id="__FRSH_STYLE"
dangerouslySetInnerHTML={{ __html: opts.styles.join("\n") }}
/>
</head>
<body>
<div dangerouslySetInnerHTML={{ __html: opts.bodyHtml }} id="__FRSH" />