Skip to content

Commit 93e5d76

Browse files
authored
Negotiate text/markdown content type on homepage (#51)
* Negotiate text/markdown content type on homepage * Use hono/accepts to negotiate content
1 parent e92df24 commit 93e5d76

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

src/index.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { StreamableHTTPTransport } from "@hono/mcp"
22
import { Hono } from "hono"
3+
import { accepts } from "hono/accepts"
34
import { cache } from "hono/cache"
45
import { cors } from "hono/cors"
56
import { HTTPException } from "hono/http-exception"
@@ -76,6 +77,33 @@ app.all("/mcp", async (c) => {
7677
return transport.handleRequest(c)
7778
})
7879

80+
app.get("/", async (c) => {
81+
const accepted = accepts(c, {
82+
header: "Accept",
83+
supports: ["text/markdown", "text/html"],
84+
default: "text/html",
85+
})
86+
87+
if (accepted === "text/markdown") {
88+
const llmsUrl = new URL("/llms.txt", c.req.url)
89+
const llmsResponse = await c.env.ASSETS.fetch(new Request(llmsUrl.toString()))
90+
91+
if (!llmsResponse.ok) {
92+
throw new HTTPException(500, {
93+
message: "Failed to load llms.txt",
94+
})
95+
}
96+
97+
const markdown = await llmsResponse.text()
98+
return c.text(markdown, 200, {
99+
"Content-Type": "text/markdown; charset=utf-8",
100+
"Cache-Control": "public, max-age=300, s-maxage=600",
101+
})
102+
}
103+
104+
return c.env.ASSETS.fetch(c.req.raw)
105+
})
106+
79107
app.get("/bot", (c) => c.redirect("/#bot", 302))
80108

81109
app.get("/search", async (c) => {

wrangler.jsonc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"compatibility_flags": ["nodejs_compat"],
1717
"assets": {
1818
"binding": "ASSETS",
19-
"directory": "./public"
19+
"directory": "./public",
20+
"run_worker_first": ["/"]
2021
},
2122
"observability": {
2223
"enabled": true

0 commit comments

Comments
 (0)