diff --git a/runtime.ts b/runtime.ts index bc2962f8..fd307424 100644 --- a/runtime.ts +++ b/runtime.ts @@ -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"; diff --git a/src/runtime/head.ts b/src/runtime/head.ts new file mode 100644 index 00000000..b90445b1 --- /dev/null +++ b/src/runtime/head.ts @@ -0,0 +1,20 @@ +import { ComponentChildren, createContext, useContext } from "./deps.ts"; + +export interface HeadProps { + children: ComponentChildren; +} + +export const HEAD_CONTEXT = createContext([]); + +export function Head(props: HeadProps) { + let context: ComponentChildren[]; + try { + context = useContext(HEAD_CONTEXT); + } catch { + throw new Error( + " component is not supported in the browser, or during suspense renders.", + ); + } + context.push(props.children); + return null; +} diff --git a/src/server/context.ts b/src/server/context.ts index 6adeddaf..3aeb6cb5 100644 --- a/src/server/context.ts +++ b/src/server/context.ts @@ -159,8 +159,15 @@ export class ServerContext { }); const bodyStream = new ReadableStream({ 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(); }, diff --git a/src/server/render.tsx b/src/server/render.tsx index 8df01b48..3223b214 100644 --- a/src/server/render.tsx +++ b/src/server/render.tsx @@ -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 { 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 { const renderWithRenderer = (): string | Promise => { // 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 { 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}
diff --git a/www/pages/index.tsx b/www/pages/index.tsx index fc5d1ccd..b00619e5 100644 --- a/www/pages/index.tsx +++ b/www/pages/index.tsx @@ -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 ( <> + + fresh - The next-gen web framework. +