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