-
Notifications
You must be signed in to change notification settings - Fork 488
Expand file tree
/
Copy pathworker.ts
More file actions
32 lines (28 loc) · 834 Bytes
/
worker.ts
File metadata and controls
32 lines (28 loc) · 834 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { Hono } from 'hono'
const app = new Hono<{
Bindings: CloudflareBindings
}>()
app.use(async (c, next) => {
const accept = c.req.header('Accept') || ''
if (accept.includes('text/markdown')) {
const path = c.req.path.replace(/\/$/, '')
if (path === '' || path === '/index') {
return c.env.ASSETS.fetch(
new Request(new URL('/llms.txt', c.req.url), c.req.raw)
)
}
const githubUrl = `https://raw.githubusercontent.com/honojs/website/main${path}.md`
const res = await fetch(githubUrl)
if (res.ok) {
return c.body(await res.arrayBuffer(), {
headers: { 'Content-Type': 'text/markdown; charset=utf-8' },
})
}
}
await next()
})
app.all('*', (c) => {
console.log('Serving asset:', c.req.url)
return c.env.ASSETS.fetch(c.req.raw)
})
export default app