feat: streaming rendering with suspense (#59)
This commit introduces support for streaming server side rendering using <Suspense>.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { h, Suspense, tw, useData } from "../deps.ts";
|
||||
|
||||
export default function SuspensePage() {
|
||||
return (
|
||||
<Suspense
|
||||
fallback={(
|
||||
<p class={tw`text-red-500 hover:text-purple-500`}>
|
||||
Loading...
|
||||
</p>
|
||||
)}
|
||||
>
|
||||
<SuspendedComponent />
|
||||
</Suspense>
|
||||
);
|
||||
}
|
||||
|
||||
export function SuspendedComponent() {
|
||||
const data = useData("Loaded", delayer);
|
||||
return <p class={tw`text-blue-500 hover:text-green-500`}>{data}!</p>;
|
||||
}
|
||||
|
||||
// A function that returns a promise that resolves after a delay with the given
|
||||
// value.
|
||||
function delayer(key: string) {
|
||||
return new Promise((resolve) => setTimeout(() => resolve(key), 2000));
|
||||
}
|
||||
+10
-8
@@ -2,19 +2,21 @@
|
||||
// This file SHOULD be checked into source version control.
|
||||
// To update this file, run `fresh routes`.
|
||||
|
||||
import * as $0 from "./pages/api/joke.ts";
|
||||
import * as $0 from "./pages/index.tsx";
|
||||
import * as $1 from "./pages/_render.ts";
|
||||
import * as $2 from "./pages/x/[module].tsx";
|
||||
import * as $3 from "./pages/[name].tsx";
|
||||
import * as $4 from "./pages/index.tsx";
|
||||
import * as $2 from "./pages/suspense.tsx";
|
||||
import * as $3 from "./pages/api/joke.ts";
|
||||
import * as $4 from "./pages/[name].tsx";
|
||||
import * as $5 from "./pages/x/[module].tsx";
|
||||
|
||||
const routes = {
|
||||
pages: {
|
||||
"./pages/api/joke.ts": $0,
|
||||
"./pages/index.tsx": $0,
|
||||
"./pages/_render.ts": $1,
|
||||
"./pages/x/[module].tsx": $2,
|
||||
"./pages/[name].tsx": $3,
|
||||
"./pages/index.tsx": $4,
|
||||
"./pages/suspense.tsx": $2,
|
||||
"./pages/api/joke.ts": $3,
|
||||
"./pages/[name].tsx": $4,
|
||||
"./pages/x/[module].tsx": $5,
|
||||
},
|
||||
baseUrl: import.meta.url,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user