Skip to content

Commit befd0bf

Browse files
committed
feat: 404 page
1 parent 43ba750 commit befd0bf

File tree

4 files changed

+68
-8
lines changed

4 files changed

+68
-8
lines changed

src/server/index.tsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { Hono } from "hono";
44
import { renderToString } from "react-dom/server";
55
import { getShareData } from "./blob";
66
import { trimTrailingSlash } from "hono/trailing-slash";
7-
import { BaseHeader, getAssetPath, HmrScript } from "./utils";
7+
import { BaseHeader, defaultCode, getAssetPath, HmrScript } from "./utils";
8+
import { notFound } from "./routes/404";
89

910
// This must be exported for the dev server to work
1011
export const app = new Hono();
@@ -24,7 +25,7 @@ app.use(trimTrailingSlash());
2425
app.get("/", (c) => c.redirect("/parameters"));
2526

2627
// Serves the main web application. This must come after the API route.
27-
app.get("/parameters/:shareId?/:example?", async (c) => {
28+
app.get("/parameters/:shareId?/:example?", async (c, next) => {
2829
const getExampleCode = async (): Promise<string | null> => {
2930
const { shareId, example } = c.req.param();
3031

@@ -33,14 +34,17 @@ app.get("/parameters/:shareId?/:example?", async (c) => {
3334
return shareData?.code ?? null;
3435
}
3536

36-
if (!example) {
37-
return null;
37+
if (example) {
38+
return examples[example] ?? null;
3839
}
3940

40-
return examples[example] ?? null;
41+
return defaultCode;
4142
};
4243

4344
const exampleCode = await getExampleCode();
45+
if (!exampleCode) {
46+
return notFound(c, next);
47+
}
4448

4549
return c.html(
4650
[
@@ -55,13 +59,13 @@ app.get("/parameters/:shareId?/:example?", async (c) => {
5559
</head>
5660
<body>
5761
<div id="root"></div>
58-
{exampleCode ? (
5962
<script type="module">{`window.CODE = ${JSON.stringify(exampleCode)}`}</script>
60-
) : null}
6163
<script type="module" src={getAssetPath("/src/client/index.tsx")}></script>
6264
</body>
6365
</html>,
6466
),
6567
].join("\n"),
6668
);
6769
});
70+
71+
app.get("*", notFound);

src/server/routes/404.tsx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import type { Handler } from "hono";
2+
import { BaseHeader } from "../utils";
3+
import { renderToString } from "react-dom/server";
4+
import type { FC } from "react";
5+
import { ArrowRightIcon } from "lucide-react";
6+
7+
export const notFound: Handler = (c) => {
8+
return c.html(
9+
["<!doctype html>", renderToString(<NotFound />)].join("\n"),
10+
404,
11+
);
12+
};
13+
14+
const NotFound: FC = () => {
15+
return (
16+
<html lang="en">
17+
<head>
18+
<title>Not Found</title>
19+
<BaseHeader />
20+
</head>
21+
<body>
22+
<main className="flex items-center justify-center w-screen h-dvh">
23+
<div className="flex flex-col gap-2 md:gap-4 items-center">
24+
<div className="flex flex-col items-center gap-1 md:gap-2">
25+
<p className="font-mono text-sky-700">404</p>
26+
<h1 className="text-3xl md:text-6xl font-semibold text-content-primary text-center">
27+
Page not found
28+
</h1>
29+
</div>
30+
<p className="text text-content-secondary md:text-lg text-center">
31+
Sorry, we couldn't find this page
32+
</p>
33+
<a
34+
href="/parameters"
35+
className="flex text-center hover:underline gap-1 text-blue-700 md:text-content-primary hover:text-blue-700 transition-colors group underline md:no-underline"
36+
>
37+
Return home{" "}
38+
<ArrowRightIcon
39+
className="group-hover:translate-x-1 transform transition-transform"
40+
width={16}
41+
/>
42+
</a>
43+
</div>
44+
</main>
45+
</body>
46+
</html>
47+
);
48+
};

src/server/routes/api.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { Hono } from "hono";
33
import * as v from "valibot";
44
import { putShareData } from "@/server/blob";
55

6-
76
const parameters = new Hono().post(
87
"/",
98
vValidator(

src/server/utils.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,12 @@ export const BaseHeader = () => {
3838
</>
3939
);
4040
};
41+
42+
export const defaultCode = `terraform {
43+
required_providers {
44+
coder = {
45+
source = "coder/coder"
46+
version = "2.5.3"
47+
}
48+
}
49+
}`;

0 commit comments

Comments
 (0)