From 3431555f9547a88998f9de906fdd5d6b4e97ede2 Mon Sep 17 00:00:00 2001 From: Michael Pfister <63919069+michael-pfister@users.noreply.github.com> Date: Tue, 16 Aug 2022 16:52:06 +0200 Subject: [PATCH] Add 404 and 500 page #446 (#624) * Add 404 and 500 page Co-authored-by: hashrock --- src/server/default_error_page.tsx | 24 +++++++++---------- www/fresh.gen.ts | 20 +++++++++------- www/routes/_404.tsx | 38 +++++++++++++++++++++++++++++++ www/routes/_500.tsx | 8 +++++++ www/routes/docs/[...slug].tsx | 4 +--- 5 files changed, 70 insertions(+), 24 deletions(-) create mode 100644 www/routes/_404.tsx create mode 100644 www/routes/_500.tsx diff --git a/src/server/default_error_page.tsx b/src/server/default_error_page.tsx index 360091bd..b9307161 100644 --- a/src/server/default_error_page.tsx +++ b/src/server/default_error_page.tsx @@ -44,20 +44,18 @@ export default function DefaultErrorPage(props: ErrorPageProps) { > An error occured during route handling or page rendering.

- {message && ( -
+        {message && 
             {message}
-          
- )} +
} ); diff --git a/www/fresh.gen.ts b/www/fresh.gen.ts index 87a7d2d0..5ff10304 100644 --- a/www/fresh.gen.ts +++ b/www/fresh.gen.ts @@ -2,20 +2,24 @@ // This file SHOULD be checked into source version control. // This file is automatically updated during development when running `dev.ts`. -import * as $0 from "./routes/docs/[...slug].tsx"; -import * as $1 from "./routes/gfm.css.ts"; -import * as $2 from "./routes/index.tsx"; -import * as $3 from "./routes/raw.ts"; +import * as $0 from "./routes/_404.tsx"; +import * as $1 from "./routes/_500.tsx"; +import * as $2 from "./routes/docs/[...slug].tsx"; +import * as $3 from "./routes/gfm.css.ts"; +import * as $4 from "./routes/index.tsx"; +import * as $5 from "./routes/raw.ts"; import * as $$0 from "./islands/CopyArea.tsx"; import * as $$1 from "./islands/Counter.tsx"; import * as $$2 from "./islands/LemonDrop.tsx"; const manifest = { routes: { - "./routes/docs/[...slug].tsx": $0, - "./routes/gfm.css.ts": $1, - "./routes/index.tsx": $2, - "./routes/raw.ts": $3, + "./routes/_404.tsx": $0, + "./routes/_500.tsx": $1, + "./routes/docs/[...slug].tsx": $2, + "./routes/gfm.css.ts": $3, + "./routes/index.tsx": $4, + "./routes/raw.ts": $5, }, islands: { "./islands/CopyArea.tsx": $$0, diff --git a/www/routes/_404.tsx b/www/routes/_404.tsx new file mode 100644 index 00000000..474c3c69 --- /dev/null +++ b/www/routes/_404.tsx @@ -0,0 +1,38 @@ +/** @jsx h */ +/** @jsxFrag Fragment */ +import { Fragment, h } from "preact"; +import LemonDrop from "../islands/LemonDrop.tsx"; + +export function ServerCodePage( + props: { serverCode: number; codeDescription: string }, +) { + return ( + +
+
+ +
+
+

+ {props.serverCode} +

+ +

+ {props.codeDescription} +

+ +

+ Back to the Homepage +

+
+
+
+ ); +} + +export default function PageNotFound() { + return ServerCodePage({ + serverCode: 404, + codeDescription: "Couldn't find what you're looking for.", + }); +} diff --git a/www/routes/_500.tsx b/www/routes/_500.tsx new file mode 100644 index 00000000..8bd82eb0 --- /dev/null +++ b/www/routes/_500.tsx @@ -0,0 +1,8 @@ +import { ServerCodePage } from "./_404.tsx"; + +export default function InternalServerError() { + return ServerCodePage({ + serverCode: 500, + codeDescription: "Oops! Something went wrong.", + }); +} diff --git a/www/routes/docs/[...slug].tsx b/www/routes/docs/[...slug].tsx index c5583324..34da20ed 100644 --- a/www/routes/docs/[...slug].tsx +++ b/www/routes/docs/[...slug].tsx @@ -34,9 +34,7 @@ export const handler: Handlers = { } const entry = TABLE_OF_CONTENTS[slug]; if (!entry) { - return new Response("404 Page not found", { - status: 404, - }); + return ctx.renderNotFound(); } const url = new URL(`../../../${entry.file}`, import.meta.url); const fileContent = await Deno.readTextFile(url);