SSG 助手
SSG 助手从你的 Hono 应用程序生成静态站点。它将检索注册路由的内容并将它们保存为静态文件。
用法
手动
如果你有一个如下简单的 Hono 应用程序:
// index.tsx
const app = new Hono()
app.get('/', (c) => c.html('Hello, World!'))
app.use('/about', async (c, next) => {
c.setRenderer((content) => {
return c.html(
<html>
<head />
<body>
<p>{content}</p>
</body>
</html>
)
})
await next()
})
app.get('/about', (c) => {
return c.render(
<>
<title>Hono SSG Page</title>Hello!
</>
)
})
export default app对于 Node.js,创建如下构建脚本:
// build.ts
import app from './index'
import { toSSG } from 'hono/ssg'
import fs from 'fs/promises'
toSSG(app, fs)通过执行脚本,文件将按如下方式输出:
ls ./static
about.html index.htmlVite 插件
使用 @hono/vite-ssg Vite 插件,你可以轻松处理该过程。
更多详情,请参阅此处:
https://github.com/honojs/vite-plugins/tree/main/packages/ssg
toSSG
toSSG 是生成静态站点的主函数,接受应用程序和文件系统模块作为参数。它基于以下内容:
输入
toSSG 的参数在 ToSSGInterface 中指定。
export interface ToSSGInterface {
(
app: Hono,
fsModule: FileSystemModule,
options?: ToSSGOptions
): Promise<ToSSGResult>
}app指定带有注册路由的new Hono()。fs指定以下对象,假设为node:fs/promise。
export interface FileSystemModule {
writeFile(path: string, data: string | Uint8Array): Promise<void>
mkdir(
path: string,
options: { recursive: boolean }
): Promise<void | string>
}使用 Deno 和 Bun 的适配器
如果你想在 Deno 或 Bun 上使用 SSG,则为每个文件系统提供了 toSSG 函数。
对于 Deno:
import { toSSG } from 'hono/deno'
toSSG(app) // 第二个参数是类型为 `ToSSGOptions` 的选项。对于 Bun:
import { toSSG } from 'hono/bun'
toSSG(app) // 第二个参数是类型为 `ToSSGOptions` 的选项。选项
选项在 ToSSGOptions 接口中指定。
export interface ToSSGOptions {
dir?: string
concurrency?: number
extensionMap?: Record<string, string>
plugins?: SSGPlugin[]
}dir是静态文件的输出目的地。默认值为./static。concurrency是同时生成的文件并发数。默认值为2。extensionMap是一个映射,包含作为键的Content-Type和作为值的扩展名字符串。这用于确定输出文件的文件扩展名。plugins是扩展静态站点生成过程功能的 SSG 插件数组。
输出
toSSG 以下述 Result 类型返回结果。
export interface ToSSGResult {
success: boolean
files: string[]
error?: Error
}生成文件
路由和文件名
以下规则适用于注册的路由信息和生成的文件名。默认 ./static 的行为如下:
/->./static/index.html/path->./static/path.html/path/->./static/path/index.html
文件扩展名
文件扩展名取决于每个路由返回的 Content-Type。例如,来自 c.html 的响应保存为 .html。
如果你想自定义文件扩展名,设置 extensionMap 选项。
import { toSSG, defaultExtensionMap } from 'hono/ssg'
// 将 `application/x-html` 内容保存为 `.html`
toSSG(app, fs, {
extensionMap: {
'application/x-html': 'html',
...defaultExtensionMap,
},
})注意,以斜杠结尾的路径无论扩展名如何都保存为 index.ext。
// 保存到 ./static/html/index.html
app.get('/html/', (c) => c.html('html'))
// 保存到 ./static/text/index.txt
app.get('/text/', (c) => c.text('text'))中间件
介绍支持 SSG 的内置中间件。
ssgParams
你可以使用像 Next.js 的 generateStaticParams 这样的 API。
示例:
app.get(
'/shops/:id',
ssgParams(async () => {
const shops = await getShops()
return shops.map((shop) => ({ id: shop.id }))
}),
async (c) => {
const shop = await getShop(c.req.param('id'))
if (!shop) {
return c.notFound()
}
return c.render(
<div>
<h1>{shop.name}</h1>
</div>
)
}
)isSSGContext
isSSGContext 是一个辅助函数,如果当前应用程序在由 toSSG 触发的 SSG 上下文中运行,则返回 true。
app.get('/page', (c) => {
if (isSSGContext(c)) {
return c.text('This is generated by SSG')
}
return c.text('This is served dynamically')
})disableSSG
设置了 disableSSG 中间件的路由被 toSSG 排除在静态文件生成之外。
app.get('/api', disableSSG(), (c) => c.text('an-api'))onlySSG
设置了 onlySSG 中间件的路由将在 toSSG 执行后被 c.notFound() 覆盖。
app.get('/static-page', onlySSG(), (c) => c.html(<h1>Welcome to my site</h1>))插件
插件允许你扩展静态站点生成过程的功能。它们使用 hooks 在不同阶段自定义生成过程。
默认插件
默认情况下,toSSG 使用 defaultPlugin,它跳过非 200 状态响应(如重定向、错误或 404)。这防止为不成功的响应生成文件。
import { toSSG, defaultPlugin } from 'hono/ssg'
// 未指定插件时自动应用 defaultPlugin
toSSG(app, fs)
// 等同于:
toSSG(app, fs, { plugins: [defaultPlugin] })如果你指定了自定义插件,defaultPlugin 不会自动包含。为了在添加自定义插件时保持默认行为,请显式包含它:
toSSG(app, fs, {
plugins: [defaultPlugin, myCustomPlugin],
})重定向插件
redirectPlugin 为返回 HTTP 重定向响应(301, 302, 303, 307, 308)的路由生成 HTML 重定向页面。生成的 HTML 包含 <meta http-equiv="refresh"> 标签和规范链接。
import { toSSG, redirectPlugin, defaultPlugin } from 'hono/ssg'
toSSG(app, fs, {
plugins: [redirectPlugin(), defaultPlugin()],
})例如,如果你的应用程序有:
app.get('/old', (c) => c.redirect('/new'))redirectPlugin 将在 /old.html 生成一个带有 meta 刷新重定向到 /new 的 HTML 文件。
NOTE
当与 defaultPlugin 一起使用时,请将 redirectPlugin 放在 defaultPlugin 之前。由于 defaultPlugin 跳过非 200 响应,将其放在前面会阻止 redirectPlugin 处理重定向响应。
Hook 类型
插件可以使用以下 hooks 来自定义 toSSG 过程:
export type BeforeRequestHook = (req: Request) => Request | false
export type AfterResponseHook = (res: Response) => Response | false
export type AfterGenerateHook = (
result: ToSSGResult
) => void | Promise<void>- BeforeRequestHook: 在处理每个请求之前调用。返回
false以跳过路由。 - AfterResponseHook: 在接收每个响应之后调用。返回
false以跳过文件生成。 - AfterGenerateHook: 在整个生成过程完成后调用。
插件接口
export interface SSGPlugin {
beforeRequestHook?: BeforeRequestHook | BeforeRequestHook[]
afterResponseHook?: AfterResponseHook | AfterResponseHook[]
afterGenerateHook?: AfterGenerateHook | AfterGenerateHook[]
}基础插件示例
仅过滤 GET 请求:
const getOnlyPlugin: SSGPlugin = {
beforeRequestHook: (req) => {
if (req.method === 'GET') {
return req
}
return false
},
}按状态码过滤:
const statusFilterPlugin: SSGPlugin = {
afterResponseHook: (res) => {
if (res.status === 200 || res.status === 500) {
return res
}
return false
},
}记录生成的文件:
const logFilesPlugin: SSGPlugin = {
afterGenerateHook: (result) => {
if (result.files) {
result.files.forEach((file) => console.log(file))
}
},
}高级插件示例
这是一个创建生成 sitemap.xml 文件的 sitemap 插件的示例:
// plugins.ts
import fs from 'node:fs/promises'
import path from 'node:path'
import type { SSGPlugin } from 'hono/ssg'
import { DEFAULT_OUTPUT_DIR } from 'hono/ssg'
export const sitemapPlugin = (baseURL: string): SSGPlugin => {
return {
afterGenerateHook: (result, fsModule, options) => {
const outputDir = options?.dir ?? DEFAULT_OUTPUT_DIR
const filePath = path.join(outputDir, 'sitemap.xml')
const urls = result.files.map((file) =>
new URL(file, baseURL).toString()
)
const siteMapText = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${urls.map((url) => `<url><loc>${url}</loc></url>`).join('\n')}
</urlset>`
fsModule.writeFile(filePath, siteMapText)
},
}
}应用插件:
import app from './index'
import { toSSG } from 'hono/ssg'
import { sitemapPlugin } from './plugins'
// 应用插件:
toSSG(app, fs, {
plugins: [
getOnlyPlugin,
statusFilterPlugin,
logFilesPlugin,
sitemapPlugin('https://example.com'),
],
})