Skip to content

Commit

Permalink
Merge pull request #47 from denoland/404s_to_kv
Browse files Browse the repository at this point in the history
track 404s in KV
  • Loading branch information
kwhinnery committed Sep 15, 2023
2 parents 4077356 + 688901e commit 177ee47
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"deno.enable": true,
"deno.lint": true,
"deno.unstable": true,
"editor.formatOnSave": true,
"editor.defaultFormatter": "denoland.vscode-deno"
}
10 changes: 9 additions & 1 deletion src-deno/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Hono } from "./deps.ts";
import { serveStatic } from "./deps.ts";
import configureRedirects from "./redirects.ts";

const kv = await Deno.openKv();
const app = new Hono();

// Configure redirects
Expand All @@ -12,7 +13,14 @@ app.use("*", serveStatic({ root: "./" }));

// 404s
app.notFound(async (c) => {
console.error("404 error returned for path: ", c.req.path);
try {
await kv.set(["404s", c.req.path], c.req.path);
} catch (e) {
console.error(e);
} finally {
console.error("404 error returned for path: ", c.req.path);
}

const f = await Deno.readTextFile("./404.html");
c.status(404);
return c.html(f);
Expand Down

0 comments on commit 177ee47

Please sign in to comment.