Skip to content

WIP feat: server config doc #7092

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

WIP feat: server config doc #7092

wants to merge 1 commit into from

Conversation

keepview
Copy link
Contributor

Summary

Related Links

Checklist

  • I have added changeset via pnpm run change.
  • I have updated the documentation.
  • I have added tests to cover my changes.

Copy link

changeset-bot bot commented Apr 27, 2025

⚠️ No Changeset found

Latest commit: bc4a79f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

netlify bot commented Apr 27, 2025

Deploy Preview for modernjs-byted ready!

Name Link
🔨 Latest commit 3147393
🔍 Latest deploy log https://app.netlify.com/sites/modernjs-byted/deploys/680df7b9c0ac1d0008f8f870
😎 Deploy Preview https://deploy-preview-7092--modernjs-byted.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 77 (🔴 down 1 from production)
Accessibility: 90 (no change from production)
Best Practices: 100 (no change from production)
SEO: 91 (no change from production)
PWA: -
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.


项目目录下创建 `server/modern.server.ts` 文件,可以在这个文件中配置**中间件**、**渲染中间件**与**服务端插件**来扩展 Server。

中间件的执行顺序是: Middleware => PluginMiddleware => RenderMiddleware => PluginRenderMiddleware。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

逗号中英文哈。另外这里可以直接有个图,PluginRenderMiddleware 对于用户来说可能不好理解,这里也还没有讲到 plugin 能做什么,感觉可以表述成在 plugin 中定义的 renderMiddleware

method?: 'options' | 'get' | 'post' | 'put' | 'delete' | 'patch' | 'all';
handler: MiddlewareHandler | MiddlewareHandler[];
before?: Array<MiddlewareObj['name']>;
order?: MiddlewareOrder;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

order / before 可以先不透露出去。

Middleware 支持在 Modern.js 服务的**请求处理**与**页面路由**的流程前后,执行自定义逻辑。

:::note
BFF 场景只有运行时框架为 Hono 时,BFF 路由才会经过 Middleware。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里最好能说明什么是「运行时框架为 Hono」。或者等 Hono BFF 那部分文档有了,下掉 express 和 koa 之后就不用提了。


### RenderMiddleware

Modern.js 支持为 Server 添加渲染中间件,支持在处理页面路由的前后执行自定义逻辑
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

少了个标点:)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

另外感觉这部分描述和前面中间件的描述,没有办法很好的区分开来。我理解 RenderMiddleware 更像是一个语法糖,减少用户对页面路由做匹配。是不是可以从功能上说明下两者的差异:

  1. 如果中间件的能力既要用在接口路由(BFF)上,也要用在页面路由上,可以用 middleware
  2. 如果只需要页面渲染,我们也准备了 xxx

import { defer } from '@modern-js/runtime/router';

export default () => {
const ctx = useHonoContext();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里例子现在直接能用的了吗,不用开启 bff.enableHandleWeb 了是吗

# 自定义 Web Server(不推荐)

:::warning
自定义 Web Server 兼容但不再推荐使用,扩展 Server 能力请移步 [自定义 Server](/zh/guides/advanced-features/custom-server.html),迁移指南参考 [迁移至新版自定义 Server](/zh/guides/advanced-features/web-server.html#%E8%BF%81%E7%A7%BB%E8%87%B3%E6%96%B0%E7%89%88%E8%87%AA%E5%AE%9A%E4%B9%89-server)。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这链接不用加 /zh 也能自动跳转的~这样写内部跳转不了了

| :----------------------- | :---------------------------- | :------------------------------------------------------------------------ |
| `c.request` | `c.req.raw` | 参考 [HonoRequest raw](https://hono.dev/docs/api/request#raw) 文档 |
| `c.response` | `c.res` | 参考 [Hono Context res](https://hono.dev/docs/api/context#res) 文档 |
| `c.route` | `c.get('route')` | 获取应用上下文信息。 |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

就他多个句号

| UnstableMiddleware | Hono | 说明 |
| :----------------------- | :---------------------------- | :----------------------------- |
| `router.redirect` | `c.redirect` | 参考 [Hono Context redirect](https://hono.dev/docs/api/context#redirect) 文档 |
| `router.rewrite` | - | 暂时没有提供对应的能力 |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mark 一下,这个后续还要提供对应的能力。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants