feat: add support for <Head> component (#67)

This component only works during SSR, during the inital render. It does
not work on renders inside a suspense boundary, or client renders.
This commit is contained in:
Luca Casonato
2021-10-04 11:35:01 +02:00
committed by GitHub
parent 4a6b57de89
commit f343e8bac7
5 changed files with 50 additions and 9 deletions
+20
View File
@@ -0,0 +1,20 @@
import { ComponentChildren, createContext, useContext } from "./deps.ts";
export interface HeadProps {
children: ComponentChildren;
}
export const HEAD_CONTEXT = createContext<ComponentChildren[]>([]);
export function Head(props: HeadProps) {
let context: ComponentChildren[];
try {
context = useContext(HEAD_CONTEXT);
} catch {
throw new Error(
"<Head> component is not supported in the browser, or during suspense renders.",
);
}
context.push(props.children);
return null;
}