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
+1
View File
@@ -3,3 +3,4 @@ export * from "./src/runtime/types.ts";
export * from "./src/runtime/suspense.ts";
export * from "./src/runtime/utils.ts";
export * from "./src/runtime/hooks.ts";
export * from "./src/runtime/head.ts";
+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;
}
+9 -2
View File
@@ -159,8 +159,15 @@ export class ServerContext {
});
const bodyStream = new ReadableStream<Uint8Array>({
async start(controller) {
for await (const chunk of body) {
controller.enqueue(new TextEncoder().encode(chunk));
try {
for await (const chunk of body) {
controller.enqueue(new TextEncoder().encode(chunk));
}
} catch (err) {
console.log("Rendering failed:\n", err);
controller.enqueue(
new TextEncoder().encode("500 Internal Server Error"),
);
}
controller.close();
},
+16 -6
View File
@@ -1,11 +1,12 @@
/** @jsx h */
import { renderToString } from "./deps.ts";
import { ComponentChild, ComponentChildren, h } from "../runtime/deps.ts";
import { ComponentChildren, h } from "../runtime/deps.ts";
import { DATA_CONTEXT } from "../runtime/hooks.ts";
import { Page, Renderer } from "./types.ts";
import { PageProps } from "../runtime/types.ts";
import { SUSPENSE_CONTEXT } from "../runtime/suspense.ts";
import { HEAD_CONTEXT } from "../runtime/head.ts";
export interface RenderOptions {
page: Page;
@@ -105,12 +106,16 @@ export async function* render(opts: RenderOptions): AsyncIterable<string> {
const dataCache = new Map();
const suspenseQueue: ComponentChildren[] = [];
const headComponents: ComponentChildren[] = [];
const vnode = h(DATA_CONTEXT.Provider, {
value: dataCache,
children: h(SUSPENSE_CONTEXT.Provider, {
value: suspenseQueue,
children: h(opts.page.component!, props),
const vnode = h(HEAD_CONTEXT.Provider, {
value: headComponents,
children: h(DATA_CONTEXT.Provider, {
value: dataCache,
children: h(SUSPENSE_CONTEXT.Provider, {
value: suspenseQueue,
children: h(opts.page.component!, props),
}),
}),
});
@@ -120,6 +125,8 @@ export async function* render(opts: RenderOptions): AsyncIterable<string> {
const renderWithRenderer = (): string | Promise<string> => {
// Clear the suspense queue
suspenseQueue.splice(0, suspenseQueue.length);
// Clear the head components
headComponents.splice(0, headComponents.length);
if (++suspended > MAX_SUSPENSE_DEPTH) {
throw new Error(
@@ -171,6 +178,7 @@ export async function* render(opts: RenderOptions): AsyncIterable<string> {
const html = template({
bodyHtml,
headComponents,
imports: opts.imports,
preloads: opts.preloads,
styles: ctx.styles,
@@ -261,6 +269,7 @@ export async function suspenseRender(
export interface TemplateOptions {
bodyHtml: string;
headComponents: ComponentChildren[];
imports: string[];
styles: string[];
preloads: string[];
@@ -281,6 +290,7 @@ export function template(opts: TemplateOptions): string {
id="__FRSH_STYLE"
dangerouslySetInnerHTML={{ __html: opts.styles.join("\n") }}
/>
{opts.headComponents}
</head>
<body>
<div dangerouslySetInnerHTML={{ __html: opts.bodyHtml }} id="__FRSH" />
+4 -1
View File
@@ -1,11 +1,14 @@
/** @jsx h */
/** @jsxFrag Fragment */
import { Fragment, h, tw } from "../deps.ts";
import { Fragment, h, Head, tw } from "../deps.ts";
export default function MainPage() {
return (
<>
<Head>
<title>fresh - The next-gen web framework.</title>
</Head>
<Hero />
<NavigationBar active="/" />
<Intro />