feat: support custom error pages (#86)
This commit is contained in:
+107
-10
@@ -12,6 +12,8 @@ import { ALIVE_URL, INTERNAL_PREFIX, REFRESH_JS_URL } from "./constants.ts";
|
||||
import { JS_PREFIX } from "./constants.ts";
|
||||
import { BUILD_ID } from "./constants.ts";
|
||||
import {
|
||||
ErrorPage,
|
||||
ErrorPageModule,
|
||||
Handler,
|
||||
Middleware,
|
||||
MiddlewareModule,
|
||||
@@ -19,6 +21,8 @@ import {
|
||||
PageModule,
|
||||
Renderer,
|
||||
RendererModule,
|
||||
UnknownPage,
|
||||
UnknownPageModule,
|
||||
} from "./types.ts";
|
||||
import { render as internalRender } from "./render.tsx";
|
||||
import {
|
||||
@@ -45,17 +49,23 @@ export class ServerContext {
|
||||
#bundler: Bundler;
|
||||
#renderer: Renderer;
|
||||
#middleware: Middleware;
|
||||
#notFound: UnknownPage;
|
||||
#error: ErrorPage;
|
||||
|
||||
constructor(
|
||||
pages: Page[],
|
||||
staticFiles: StaticFile[],
|
||||
renderer: Renderer,
|
||||
middleware: Middleware,
|
||||
notFound: UnknownPage,
|
||||
error: ErrorPage,
|
||||
) {
|
||||
this.#pages = pages;
|
||||
this.#staticFiles = staticFiles;
|
||||
this.#renderer = renderer;
|
||||
this.#middleware = middleware;
|
||||
this.#notFound = notFound;
|
||||
this.#error = error;
|
||||
this.#bundler = new Bundler(pages);
|
||||
this.#dev = typeof Deno.env.get("DENO_DEPLOYMENT_ID") !== "string"; // Env var is only set in prod (on Deploy).
|
||||
}
|
||||
@@ -71,6 +81,8 @@ export class ServerContext {
|
||||
const pages: Page[] = [];
|
||||
let renderer: Renderer = DEFAULT_RENDERER;
|
||||
let middleware: Middleware = DEFAULT_MIDDLEWARE;
|
||||
let notFound: UnknownPage = DEFAULT_NOT_FOUND;
|
||||
let error: ErrorPage = DEFAULT_ERROR;
|
||||
for (const [self, module] of Object.entries(routes.pages)) {
|
||||
const url = new URL(self, baseUrl).href;
|
||||
if (!url.startsWith(baseUrl)) {
|
||||
@@ -113,6 +125,45 @@ export class ServerContext {
|
||||
path === "/_middleware.jsx" || path === "/_middleware.js"
|
||||
) {
|
||||
middleware = module as MiddlewareModule;
|
||||
} else if (
|
||||
path === "/_404.tsx" || path === "/_404.ts" ||
|
||||
path === "/_404.jsx" || path === "/_404.js"
|
||||
) {
|
||||
const { default: component, config } = (module as UnknownPageModule);
|
||||
let { handler } = (module as UnknownPageModule);
|
||||
if (component && handler === undefined) {
|
||||
handler = ({ render }) => render!();
|
||||
}
|
||||
|
||||
notFound = {
|
||||
route: pathToRoute(baseRoute),
|
||||
url,
|
||||
name,
|
||||
component,
|
||||
handler: handler ?? ((ctx) => router.defaultOtherHandler(ctx.req)),
|
||||
runtimeJS: Boolean(config?.runtimeJS ?? false),
|
||||
csp: Boolean(config?.csp ?? false),
|
||||
};
|
||||
} else if (
|
||||
path === "/_500.tsx" || path === "/_500.ts" ||
|
||||
path === "/_500.jsx" || path === "/_500.js"
|
||||
) {
|
||||
const { default: component, config } = (module as ErrorPageModule);
|
||||
let { handler } = (module as ErrorPageModule);
|
||||
if (component && handler === undefined) {
|
||||
handler = ({ render }) => render!();
|
||||
}
|
||||
|
||||
error = {
|
||||
route: pathToRoute(baseRoute),
|
||||
url,
|
||||
name,
|
||||
component,
|
||||
handler: handler ??
|
||||
((ctx) => router.defaultErrorHandler(ctx.req, ctx.error)),
|
||||
runtimeJS: Boolean(config?.runtimeJS ?? false),
|
||||
csp: Boolean(config?.csp ?? false),
|
||||
};
|
||||
}
|
||||
}
|
||||
sortRoutes(pages);
|
||||
@@ -151,7 +202,14 @@ export class ServerContext {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
return new ServerContext(pages, staticFiles, renderer, middleware);
|
||||
return new ServerContext(
|
||||
pages,
|
||||
staticFiles,
|
||||
renderer,
|
||||
middleware,
|
||||
notFound,
|
||||
error,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -159,7 +217,7 @@ export class ServerContext {
|
||||
* by fresh, including static files.
|
||||
*/
|
||||
handler(): router.RequestHandler {
|
||||
const inner = router.router(this.#routes());
|
||||
const inner = router.router(...this.#routes());
|
||||
const middleware = this.#middleware;
|
||||
return function handler(req: Request) {
|
||||
// Redirect requests that end with a trailing slash
|
||||
@@ -179,7 +237,7 @@ export class ServerContext {
|
||||
* This function returns all routes required by fresh as an extended
|
||||
* path-to-regex, to handler mapping.
|
||||
*/
|
||||
#routes(): router.Routes {
|
||||
#routes(): [router.Routes, router.RequestHandler, router.ErrorHandler] {
|
||||
const routes: router.Routes = {};
|
||||
|
||||
// Add the static file routes.
|
||||
@@ -192,18 +250,19 @@ export class ServerContext {
|
||||
);
|
||||
}
|
||||
|
||||
for (const page of this.#pages) {
|
||||
const genRender = (page: Page, status: number) => {
|
||||
const bundlePath = `/${page.name}.js`;
|
||||
const imports = page.runtimeJS ? [bundleAssetUrl(bundlePath)] : [];
|
||||
if (this.#dev) {
|
||||
imports.push(REFRESH_JS_URL);
|
||||
}
|
||||
const createRender = (
|
||||
return (
|
||||
req: Request,
|
||||
params: Record<string, string | string[]>,
|
||||
error?: unknown,
|
||||
) => {
|
||||
if (page.component === undefined) return undefined;
|
||||
const render = async (renderData?: Record<string, unknown>) => {
|
||||
return async (renderData?: Record<string, unknown>) => {
|
||||
const preloads = page.runtimeJS
|
||||
? this.#bundler.getPreloads(bundlePath).map(bundleAssetUrl)
|
||||
: [];
|
||||
@@ -215,7 +274,9 @@ export class ServerContext {
|
||||
url: new URL(req.url),
|
||||
params,
|
||||
renderData,
|
||||
error,
|
||||
});
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
"content-type": "text/html; charset=utf-8",
|
||||
};
|
||||
@@ -242,7 +303,7 @@ export class ServerContext {
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Error rendering page", err);
|
||||
return new Response("500 Internal Server Error", {
|
||||
throw new Response("500 Internal Server Error", {
|
||||
status: 500,
|
||||
headers: {
|
||||
"content-type": "text/plain",
|
||||
@@ -265,11 +326,13 @@ export class ServerContext {
|
||||
controller.close();
|
||||
},
|
||||
});
|
||||
return new Response(bodyStream, { status: 200, headers });
|
||||
return new Response(bodyStream, { status, headers });
|
||||
};
|
||||
return render;
|
||||
};
|
||||
};
|
||||
|
||||
for (const page of this.#pages) {
|
||||
const createRender = genRender(page, 200);
|
||||
if (typeof page.handler === "function") {
|
||||
routes[page.route] = (req, match) =>
|
||||
(page.handler as Handler)({
|
||||
@@ -285,6 +348,22 @@ export class ServerContext {
|
||||
}
|
||||
}
|
||||
|
||||
const unknownHandlerRender = genRender(this.#notFound, 404);
|
||||
const unknownHandler = (req: Request) =>
|
||||
(this.#notFound.handler as Handler)({
|
||||
req,
|
||||
match: {},
|
||||
render: unknownHandlerRender(req, {}),
|
||||
});
|
||||
|
||||
const errorHandlerRender = genRender(this.#error, 500);
|
||||
const errorHandler = (req: Request, error: unknown) =>
|
||||
(this.#notFound.handler as Handler)({
|
||||
req,
|
||||
match: {},
|
||||
render: errorHandlerRender(req, {}, error),
|
||||
});
|
||||
|
||||
routes[`${INTERNAL_PREFIX}${JS_PREFIX}/${BUILD_ID}/:path*`] = this
|
||||
.#bundleAssetRoute();
|
||||
|
||||
@@ -321,7 +400,7 @@ export class ServerContext {
|
||||
};
|
||||
}
|
||||
|
||||
return routes;
|
||||
return [routes, unknownHandler, errorHandler];
|
||||
}
|
||||
|
||||
#staticFileHandler(
|
||||
@@ -386,6 +465,24 @@ const DEFAULT_MIDDLEWARE: Middleware = {
|
||||
handler: (_, handle) => handle(),
|
||||
};
|
||||
|
||||
const DEFAULT_NOT_FOUND: UnknownPage = {
|
||||
route: "",
|
||||
url: "",
|
||||
name: "_404",
|
||||
handler: (ctx) => router.defaultOtherHandler(ctx.req),
|
||||
runtimeJS: false,
|
||||
csp: false,
|
||||
};
|
||||
|
||||
const DEFAULT_ERROR: ErrorPage = {
|
||||
route: "",
|
||||
url: "",
|
||||
name: "_500",
|
||||
handler: (ctx) => router.defaultErrorHandler(ctx.req, ctx.error),
|
||||
runtimeJS: false,
|
||||
csp: false,
|
||||
};
|
||||
|
||||
/**
|
||||
* Sort pages by their relative routing priority, based on the parts in the
|
||||
* route matcher
|
||||
|
||||
+13
-5
@@ -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) {
|
||||
|
||||
+55
-1
@@ -1,6 +1,11 @@
|
||||
import { ComponentType } from "../runtime/deps.ts";
|
||||
import { router } from "./deps.ts";
|
||||
import { PageConfig, PageProps } from "../runtime/types.ts";
|
||||
import {
|
||||
ErrorPageProps,
|
||||
PageConfig,
|
||||
PageProps,
|
||||
UnknownPageProps,
|
||||
} from "../runtime/types.ts";
|
||||
import { RenderContext, RenderFn } from "./render.tsx";
|
||||
|
||||
export interface PageModule {
|
||||
@@ -9,13 +14,42 @@ export interface PageModule {
|
||||
config?: PageConfig;
|
||||
}
|
||||
|
||||
export interface UnknownPageModule {
|
||||
default?: ComponentType<UnknownPageProps>;
|
||||
handler?: UnknownHandler;
|
||||
config?: PageConfig;
|
||||
}
|
||||
|
||||
export interface ErrorPageModule {
|
||||
default?: ComponentType<ErrorPageProps>;
|
||||
handler?: ErrorHandler;
|
||||
config?: PageConfig;
|
||||
}
|
||||
|
||||
export interface HandlerContext {
|
||||
req: Request;
|
||||
match: Record<string, string>;
|
||||
render?: (args?: Record<string, unknown>) => Promise<Response>;
|
||||
}
|
||||
|
||||
export interface UnknownHandlerContext {
|
||||
req: Request;
|
||||
render?: () => Promise<Response>;
|
||||
}
|
||||
|
||||
export interface ErrorHandlerContext {
|
||||
req: Request;
|
||||
error: unknown;
|
||||
render?: () => Promise<Response>;
|
||||
}
|
||||
|
||||
export type Handler = (ctx: HandlerContext) => Response | Promise<Response>;
|
||||
export type UnknownHandler = (
|
||||
ctx: UnknownHandlerContext,
|
||||
) => Response | Promise<Response>;
|
||||
export type ErrorHandler = (
|
||||
ctx: ErrorHandlerContext,
|
||||
) => Response | Promise<Response>;
|
||||
|
||||
export type Handlers = {
|
||||
[K in typeof router.METHODS[number]]?: Handler;
|
||||
@@ -31,6 +65,26 @@ export interface Page {
|
||||
csp: boolean;
|
||||
}
|
||||
|
||||
export interface UnknownPage {
|
||||
route: string;
|
||||
url: string;
|
||||
name: string;
|
||||
component?: ComponentType<UnknownPageProps>;
|
||||
handler: UnknownHandler;
|
||||
runtimeJS: boolean;
|
||||
csp: boolean;
|
||||
}
|
||||
|
||||
export interface ErrorPage {
|
||||
route: string;
|
||||
url: string;
|
||||
name: string;
|
||||
component?: ComponentType<ErrorPageProps>;
|
||||
handler: ErrorHandler;
|
||||
runtimeJS: boolean;
|
||||
csp: boolean;
|
||||
}
|
||||
|
||||
export interface RendererModule {
|
||||
render(ctx: RenderContext, render: RenderFn): void;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user