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
+28
View File
@@ -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));
}