feat: support custom error pages (#86)

This commit is contained in:
Leo Kettmeir
2022-02-15 01:17:47 +01:00
committed by GitHub
parent 64fbcabf67
commit d334b53320
9 changed files with 243 additions and 26 deletions
+13 -5
View File
@@ -3,8 +3,12 @@
import { renderToString } from "./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 { ErrorPage, Page, Renderer, UnknownPage } from "./types.ts";
import {
ErrorPageProps,
PageProps,
UnknownPageProps,
} from "../runtime/types.ts";
import { SUSPENSE_CONTEXT } from "../runtime/suspense.ts";
import { HEAD_CONTEXT } from "../runtime/head.ts";
import { REFRESH_JS_URL } from "./constants.ts";
@@ -12,13 +16,14 @@ import { CSP_CONTEXT, nonce, NONE, UNSAFE_INLINE } from "../runtime/csp.ts";
import { ContentSecurityPolicy } from "../runtime/csp.ts";
export interface RenderOptions {
page: Page;
page: Page | UnknownPage | ErrorPage;
imports: string[];
preloads: string[];
url: URL;
params: Record<string, string | string[]>;
renderer: Renderer;
renderData?: Record<string, unknown>;
error?: unknown;
}
export type RenderFn = () => void;
@@ -115,12 +120,15 @@ function defaultCsp() {
export async function* render(
opts: RenderOptions,
): AsyncIterable<string | [string, ContentSecurityPolicy | undefined]> {
const props = {
const props: Record<string, unknown> = {
params: opts.params,
url: opts.url,
route: opts.page.route,
renderData: opts.renderData,
};
if (opts.error) {
props.error = opts.error;
}
const csp: ContentSecurityPolicy | undefined = opts.page.csp
? defaultCsp()
@@ -193,7 +201,7 @@ export async function* render(
const bodyHtml = await renderWithRenderer();
let templateProps: {
props: PageProps;
props: PageProps | UnknownPageProps | ErrorPageProps;
data?: [string, unknown][];
} | undefined = { props, data: [...dataCache.entries()] };
if (templateProps.data!.length === 0) {