[翻译] Hono 更新 2025年夏季
22 天前(已编辑)
hono-recent-updates-2025-08.pdf 2577475
Yusuke Wada
2025-08-22 / Cloudflare Workers Tech Talks in Niigata #1\n
其他话题
访问后端时使用 fetch
,但是...
=2290x1008
=1642x1208
Proxy Helper 解决方案
=2146x1444
定义各种验证器的模式:
sValidator 可以接受任何验证器的模式(如果支持 StandardValidator):
Hono 应用:
使用 toSSG:
执行后会在 static(默认)目录创建静态文件。 现在可以创建插件
=2526x1290
阻止 AI 机器人
从 Hono Client (hc) 的响应中获取类型化数据的实用函数
常规方法
使用 parseResponse 如果不是 OK,会抛出 DetailedError
使用 Vite 进行构建
vite.config.ts
npm run cf-typegen
=2082x860
\
import { Hono } from 'hono'
const app = new Hono()
app.get('/', () => fetch('https://example.com/'))
export default app
import { Hono } from 'hono'
import { proxy } from 'hono/proxy'
const app = new Hono()
app.get('/', () => proxy('https://example.com/'))
export default app
import { type } from 'arktype'
import * as v from 'valibot'
import * as z from 'zod'
const arktypeSchema = type({
name: 'string',
age: 'number',
})
const valibotSchema = v.object({
name: v.string(),
age: v.number(),
})
const zodSchema = z.object({
name: z.string(),
age: z.number(),
})
import { sValidator } from '@hono/standard-validator'
app.post(
'/author',
sValidator('json', arktypeSchema),
(c) => {
const data = c.req.valid('json')
return c.json({
message: `${data.name} is ${data.age}`,
})
}
)
// index.tsx
import { Hono } from 'hono'
const app = new Hono()
app.get('/about', (c) => {
return c.html(
<html>
<body>
<h1>Hello!</h1>
</body>
</html>
)
})
export default app
// build.ts
import app from './index'
import { toSSG } from 'hono/ssg'
import fs from 'fs/promises'
toSSG(app, fs)
import type { SSGPlugin } from 'hono/ssg'
const logFilesPlugin: SSGPlugin = {
afterGenerateHook: (result) => {
if (result.files) {
result.files.forEach((file) => console.log(file))
}
},
}
// ...
toSSG(app, fs, {
plugins: [logFilesPlugin],
})
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
import { StreamableHTTPTransport } from '@hono/mcp'
import { Hono } from 'hono'
const app = new Hono()
// 您的 MCP 服务器实现
const mcpServer = new McpServer({
name: 'my-mcp-server',
version: '1.0.0',
})
app.all('/mcp', async (c) => {
const transport = new StreamableHTTPTransport()
await mcpServer.connect(transport)
return transport.handleRequest(c)
})
export default app
import { uaBlocker } from '@hono/ua-blocker'
import { Hono } from 'hono'
const app = new Hono()
app.use(
'*',
uaBlocker({
blocklist: ['ForbiddenBot', 'Not You']
})
)
app.get('/', (c) => c.text('Hello World'))
export default app
import { uaBlocker } from '@hono/ua-blocker'
import { aiBots } from '@hono/ua-blocker/ai-bots'
import { Hono } from 'hono'
const app = new Hono()
app.use(
'*',
uaBlocker({
blocklist: aiBots,
})
)
app.get('/', (c) => c.text('Hello World'))
export default app
import { Hono } from 'hono'
const app = new Hono()
const routes = app.get('/api/hello', (c) => {
return c.json({ message: 'hello' })
})
import { hc } from 'hono/client'
const client = hc<typeof routes>('http://example.com/')
const res = await client.api.hello.$get()
if (!res.ok) {
// 错误处理
}
const data = await res.json()
console.log(data.message)
import { hc, parseResponse } from 'hono/client'
const client = hc<typeof routes>('http://example.com/')
const data = await parseResponse(client.api.hello.$get())
console.log(data.message)
import { cloudflare } from '@cloudflare/vite-plugin'
import { defineConfig } from 'vite'
import ssrPlugin from 'vite-ssr-components/plugin'
export default defineConfig({
plugins: [cloudflare(), ssrPlugin()]
})