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
+6 -20
View File
@@ -1,32 +1,18 @@
// This module adds twind support.
import {
getStyleTagProperties,
virtualSheet,
} from "https://esm.sh/twind/sheets";
import { h, setup } from "../deps.ts";
import { virtualSheet } from "https://esm.sh/twind/sheets";
import { setup } from "../deps.ts";
import { RenderContext, RenderFn } from "../server_deps.ts";
const sheet = virtualSheet();
const initial = sheet.reset();
sheet.reset();
setup({ sheet });
export function render(ctx: RenderContext, render: RenderFn) {
const snapshot = ctx.state.get("twindSnapshot") as unknown[] | null;
sheet.reset(snapshot || initial);
sheet.reset(snapshot || undefined);
render();
const newSnapshot = sheet.reset(initial);
ctx.styles.splice(0, ctx.styles.length, ...sheet.target);
const newSnapshot = sheet.reset();
ctx.state.set("twindSnapshot", newSnapshot);
}
export function postRender(ctx: RenderContext) {
const snapshot = ctx.state.get("twindSnapshot") as unknown[] | null;
if (snapshot !== null) {
sheet.reset(snapshot);
const { id, textContent } = getStyleTagProperties(sheet);
ctx.head.push(
h("style", { id, dangerouslySetInnerHTML: { __html: textContent } }),
);
}
sheet.reset(initial);
}