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:
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user