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
+33
View File
@@ -0,0 +1,33 @@
import {
ComponentChild,
ComponentChildren,
createContext,
Fragment,
h,
useContext,
} from "./deps.ts";
import { IS_BROWSER } from "./utils.ts";
export const SUSPENSE_CONTEXT = createContext<ComponentChildren[]>([]);
export interface SuspenseProps {
fallback: ComponentChild;
children: ComponentChildren;
}
export function Suspense(props: SuspenseProps) {
if (IS_BROWSER) {
throw new Error(`'<Suspense>' is not supported on the client yet.`);
}
const suspenseData = useContext(SUSPENSE_CONTEXT);
const id = suspenseData.push(props.children);
return h(
Fragment,
null,
h("template", { id: `S:${id}` }),
props.fallback,
h("template", { id: `E:${id}` }),
);
}