跳到主要内容

JSX 与 SSR

输出 HTML 用模板还是 JSX?​

  • 少量页面、邮件模板: hono/html 的 html 模板字符串, 插值自动转义, raw() 才关闭;
  • 有组件、props、类型检查: hono/jsx; 纯 JSON API 不引入渲染层;
app.get("/:name", (c) => c.html(html`<h1>Hello! ${c.req.param("name")}</h1>`));

const Layout = (props: { title: string; children: any }) => html`
<html>
<head>
<title>${props.title}</title>
</head>
<body>
${props.children}
</body>
</html>
`;

hono/jsx 要改哪几处配置?​

tsconfig.json
{ "compilerOptions": { "jsx": "react-jsx", "jsxImportSource": "hono/jsx" } }
  • 文件: 改成 .tsx, dev 脚本入口同步改;
  • 不动 tsconfig 时: 加 /** @jsxImportSource hono/jsx */ 文件级注释;
  • 定义组件: FC; 带 children: PropsWithChildren;

JSX 里还有哪些常用写法?​

  • Fragment: <>...</>;
  • 原始 HTML: 用 dangerouslySetInnerHTML;
  • 样式: hono/css 的 css 标签生成类名并在 SSR 时收集;

布局、上下文与错误页怎么组织?​

app.use(
"/page/*",
jsxRenderer(({ children }) => (
<html>
<body>{children}</body>
</html>
)),
);
app.get("/page/a", (c) => c.render(<p>content</p>)); // 整页由布局拼好

const User = () => {
const c = useRequestContext();
return <span>{c.get("userName")}</span>;
};
  • jsxRenderer: 是 c.setRenderer() 的语法糖;
  • 范围: 布局只挂页面路径, 不影响 JSON 接口;
  • 深层组件: 用 useRequestContext() 取 Context;
  • 数据: 仍由 Handler 或中间件放进 c.var;
  • 布局参数: 模块增强 ContextRenderer 后可用 c.render(content, { title });
  • 自动提升: <title> 与 <meta> 会提到 <head>;
  • 约定: 布局与页面不要重复写;

错误页与水合怎么接?​

  • 错误分流: onError 里按 Accept 返回 HTML 或 JSON;
  • 反例: 生产不要把堆栈渲染进页面;
  • 水合: 服务端 renderToString(hono/jsx/dom/server)+ 浏览器 hydrateRoot(hono/jsx/dom/client);