@modern-js/main-doc 2.67.5 → 2.67.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/en/apis/app/hooks/server/server.mdx +10 -0
- package/docs/en/apis/app/hooks/src/routes.mdx +3 -73
- package/docs/en/apis/app/runtime/bff/use-hono-context.mdx +30 -0
- package/docs/en/components/enable-bff.mdx +1 -27
- package/docs/en/components/rsbuild-config-tooltip.mdx +2 -2
- package/docs/en/components/tech-stack-node-framework.mdx +1 -1
- package/docs/en/configure/app/dev/asset-prefix.mdx +2 -3
- package/docs/en/configure/app/dev/client.mdx +2 -3
- package/docs/en/configure/app/dev/hmr.mdx +2 -3
- package/docs/en/configure/app/dev/live-reload.mdx +2 -3
- package/docs/en/configure/app/dev/progress-bar.mdx +2 -3
- package/docs/en/configure/app/dev/setup-middlewares.mdx +2 -3
- package/docs/en/configure/app/dev/watch-files.mdx +2 -3
- package/docs/en/configure/app/dev/write-to-disk.mdx +2 -3
- package/docs/en/configure/app/html/app-icon.mdx +2 -3
- package/docs/en/configure/app/html/crossorigin.mdx +2 -3
- package/docs/en/configure/app/html/favicon.mdx +2 -3
- package/docs/en/configure/app/html/inject.mdx +2 -3
- package/docs/en/configure/app/html/meta.mdx +2 -3
- package/docs/en/configure/app/html/mount-id.mdx +2 -3
- package/docs/en/configure/app/html/output-structure.mdx +2 -3
- package/docs/en/configure/app/html/script-loading.mdx +2 -3
- package/docs/en/configure/app/html/tags.mdx +2 -3
- package/docs/en/configure/app/html/template-parameters.mdx +2 -3
- package/docs/en/configure/app/html/template.mdx +2 -3
- package/docs/en/configure/app/html/title.mdx +2 -3
- package/docs/en/configure/app/output/asset-prefix.mdx +2 -3
- package/docs/en/configure/app/output/charset.mdx +2 -3
- package/docs/en/configure/app/output/copy.mdx +2 -3
- package/docs/en/configure/app/output/css-modules.mdx +2 -3
- package/docs/en/configure/app/output/data-uri-limit.mdx +2 -3
- package/docs/en/configure/app/output/dist-path.mdx +2 -4
- package/docs/en/configure/app/output/externals.mdx +2 -3
- package/docs/en/configure/app/output/filename-hash.mdx +2 -3
- package/docs/en/configure/app/output/filename.mdx +2 -3
- package/docs/en/configure/app/output/inject-styles.mdx +2 -3
- package/docs/en/configure/app/output/inline-scripts.mdx +2 -3
- package/docs/en/configure/app/output/inline-styles.mdx +2 -3
- package/docs/en/configure/app/output/legal-comments.mdx +2 -3
- package/docs/en/configure/app/output/minify.mdx +2 -3
- package/docs/en/configure/app/output/override-browserslist.mdx +2 -3
- package/docs/en/configure/app/output/polyfill.mdx +2 -3
- package/docs/en/configure/app/output/source-map.mdx +2 -3
- package/docs/en/configure/app/performance/build-cache.mdx +2 -3
- package/docs/en/configure/app/performance/bundle-analyze.mdx +2 -3
- package/docs/en/configure/app/performance/chunk-split.mdx +2 -3
- package/docs/en/configure/app/performance/dns-prefetch.mdx +2 -3
- package/docs/en/configure/app/performance/preconnect.mdx +2 -3
- package/docs/en/configure/app/performance/prefetch.mdx +2 -3
- package/docs/en/configure/app/performance/preload.mdx +2 -3
- package/docs/en/configure/app/performance/print-file-size.mdx +2 -3
- package/docs/en/configure/app/performance/profile.mdx +2 -3
- package/docs/en/configure/app/performance/remove-console.mdx +2 -3
- package/docs/en/configure/app/performance/remove-moment-locale.mdx +2 -3
- package/docs/en/configure/app/performance/transform-lodash.mdx +4 -1
- package/docs/en/configure/app/security/nonce.mdx +2 -3
- package/docs/en/configure/app/security/sri.mdx +0 -1
- package/docs/en/configure/app/server/port.mdx +2 -3
- package/docs/en/configure/app/source/alias-strategy.mdx +2 -3
- package/docs/en/configure/app/source/alias.mdx +2 -3
- package/docs/en/configure/app/source/decorators.mdx +2 -3
- package/docs/en/configure/app/source/define.mdx +2 -3
- package/docs/en/configure/app/source/exclude.mdx +2 -3
- package/docs/en/configure/app/source/include.mdx +2 -3
- package/docs/en/configure/app/source/module-scopes.mdx +4 -1
- package/docs/en/configure/app/source/pre-entry.mdx +2 -3
- package/docs/en/configure/app/source/transform-import.mdx +2 -3
- package/docs/en/configure/app/tools/css-extract.mdx +2 -3
- package/docs/en/configure/app/tools/css-loader.mdx +2 -2
- package/docs/en/configure/app/tools/html-plugin.mdx +7 -3
- package/docs/en/configure/app/tools/lightningcss-loader.mdx +2 -3
- package/docs/en/configure/app/tools/postcss.mdx +2 -3
- package/docs/en/configure/app/tools/rspack.mdx +2 -3
- package/docs/en/configure/app/tools/style-loader.mdx +2 -3
- package/docs/en/configure/app/tools/swc.mdx +1 -1
- package/docs/en/configure/app/tools/terser.mdx +4 -2
- package/docs/en/configure/app/tools/ts-loader.mdx +4 -1
- package/docs/en/configure/app/tools/webpack-chain.mdx +4 -1
- package/docs/en/configure/app/tools/webpack.mdx +4 -1
- package/docs/en/guides/advanced-features/_meta.json +0 -1
- package/docs/en/guides/advanced-features/bff/extend-server.mdx +33 -82
- package/docs/en/guides/advanced-features/bff/frameworks.mdx +12 -68
- package/docs/en/guides/advanced-features/page-performance/_meta.json +1 -1
- package/docs/en/guides/advanced-features/page-performance/react-compiler.mdx +44 -0
- package/docs/en/guides/advanced-features/web-server.mdx +223 -20
- package/docs/en/guides/basic-features/data/data-cache.mdx +1 -1
- package/docs/en/guides/basic-features/deploy.mdx +3 -3
- package/docs/en/guides/basic-features/output-files.mdx +0 -28
- package/docs/en/guides/basic-features/render/ssr.mdx +1 -1
- package/docs/en/guides/basic-features/render/streaming-ssr.mdx +2 -2
- package/docs/en/guides/basic-features/routes.mdx +72 -0
- package/docs/en/guides/basic-features/static-assets/wasm-assets.mdx +1 -1
- package/docs/en/guides/topic-detail/module-federation/deploy.mdx +83 -7
- package/docs/en/guides/topic-detail/module-federation/usage.mdx +2 -4
- package/docs/en/plugin/cli-plugins/api.mdx +1 -1
- package/docs/en/tutorials/first-app/c04-routes.mdx +4 -2
- package/docs/en/tutorials/first-app/c05-loader.mdx +4 -1
- package/docs/zh/apis/app/hooks/server/server.mdx +10 -0
- package/docs/zh/apis/app/hooks/src/routes.mdx +3 -3
- package/docs/zh/apis/app/runtime/bff/use-hono-context.mdx +31 -0
- package/docs/zh/components/enable-bff.mdx +2 -27
- package/docs/zh/components/rsbuild-config-tooltip.mdx +2 -2
- package/docs/zh/components/tech-stack-node-framework.mdx +1 -1
- package/docs/zh/configure/app/dev/asset-prefix.mdx +2 -3
- package/docs/zh/configure/app/dev/client.mdx +2 -3
- package/docs/zh/configure/app/dev/hmr.mdx +2 -3
- package/docs/zh/configure/app/dev/live-reload.mdx +2 -3
- package/docs/zh/configure/app/dev/progress-bar.mdx +2 -3
- package/docs/zh/configure/app/dev/setup-middlewares.mdx +2 -3
- package/docs/zh/configure/app/dev/watch-files.mdx +2 -3
- package/docs/zh/configure/app/dev/write-to-disk.mdx +2 -3
- package/docs/zh/configure/app/html/app-icon.mdx +2 -3
- package/docs/zh/configure/app/html/crossorigin.mdx +2 -3
- package/docs/zh/configure/app/html/favicon.mdx +2 -3
- package/docs/zh/configure/app/html/inject.mdx +2 -3
- package/docs/zh/configure/app/html/meta.mdx +2 -3
- package/docs/zh/configure/app/html/mount-id.mdx +2 -3
- package/docs/zh/configure/app/html/output-structure.mdx +2 -3
- package/docs/zh/configure/app/html/script-loading.mdx +2 -3
- package/docs/zh/configure/app/html/tags.mdx +2 -3
- package/docs/zh/configure/app/html/template-parameters.mdx +2 -3
- package/docs/zh/configure/app/html/template.mdx +2 -3
- package/docs/zh/configure/app/html/title.mdx +2 -3
- package/docs/zh/configure/app/output/asset-prefix.mdx +2 -3
- package/docs/zh/configure/app/output/charset.mdx +2 -3
- package/docs/zh/configure/app/output/copy.mdx +2 -3
- package/docs/zh/configure/app/output/css-modules.mdx +2 -3
- package/docs/zh/configure/app/output/data-uri-limit.mdx +2 -3
- package/docs/zh/configure/app/output/dist-path.mdx +2 -4
- package/docs/zh/configure/app/output/externals.mdx +2 -3
- package/docs/zh/configure/app/output/filename-hash.mdx +2 -3
- package/docs/zh/configure/app/output/filename.mdx +2 -3
- package/docs/zh/configure/app/output/inject-styles.mdx +2 -3
- package/docs/zh/configure/app/output/inline-scripts.mdx +2 -3
- package/docs/zh/configure/app/output/inline-styles.mdx +2 -3
- package/docs/zh/configure/app/output/legal-comments.mdx +2 -3
- package/docs/zh/configure/app/output/minify.mdx +2 -3
- package/docs/zh/configure/app/output/override-browserslist.mdx +2 -3
- package/docs/zh/configure/app/output/polyfill.mdx +2 -3
- package/docs/zh/configure/app/output/source-map.mdx +2 -3
- package/docs/zh/configure/app/performance/build-cache.mdx +2 -3
- package/docs/zh/configure/app/performance/bundle-analyze.mdx +2 -3
- package/docs/zh/configure/app/performance/chunk-split.mdx +2 -3
- package/docs/zh/configure/app/performance/dns-prefetch.mdx +2 -3
- package/docs/zh/configure/app/performance/preconnect.mdx +2 -3
- package/docs/zh/configure/app/performance/prefetch.mdx +2 -3
- package/docs/zh/configure/app/performance/preload.mdx +2 -3
- package/docs/zh/configure/app/performance/print-file-size.mdx +2 -3
- package/docs/zh/configure/app/performance/profile.mdx +2 -3
- package/docs/zh/configure/app/performance/remove-console.mdx +2 -3
- package/docs/zh/configure/app/performance/remove-moment-locale.mdx +2 -3
- package/docs/zh/configure/app/performance/transform-lodash.mdx +4 -1
- package/docs/zh/configure/app/security/nonce.mdx +2 -3
- package/docs/zh/configure/app/security/sri.mdx +0 -1
- package/docs/zh/configure/app/server/port.mdx +2 -3
- package/docs/zh/configure/app/source/alias-strategy.mdx +2 -3
- package/docs/zh/configure/app/source/alias.mdx +2 -3
- package/docs/zh/configure/app/source/decorators.mdx +2 -3
- package/docs/zh/configure/app/source/define.mdx +2 -3
- package/docs/zh/configure/app/source/exclude.mdx +2 -3
- package/docs/zh/configure/app/source/include.mdx +2 -3
- package/docs/zh/configure/app/source/module-scopes.mdx +4 -1
- package/docs/zh/configure/app/source/pre-entry.mdx +2 -3
- package/docs/zh/configure/app/source/transform-import.mdx +2 -3
- package/docs/zh/configure/app/tools/css-extract.mdx +2 -3
- package/docs/zh/configure/app/tools/css-loader.mdx +2 -3
- package/docs/zh/configure/app/tools/html-plugin.mdx +6 -3
- package/docs/zh/configure/app/tools/lightningcss-loader.mdx +2 -3
- package/docs/zh/configure/app/tools/postcss.mdx +2 -3
- package/docs/zh/configure/app/tools/rspack.mdx +2 -3
- package/docs/zh/configure/app/tools/style-loader.mdx +2 -3
- package/docs/zh/configure/app/tools/swc.mdx +1 -1
- package/docs/zh/configure/app/tools/terser.mdx +4 -2
- package/docs/zh/configure/app/tools/ts-loader.mdx +4 -1
- package/docs/zh/configure/app/tools/webpack-chain.mdx +4 -1
- package/docs/zh/configure/app/tools/webpack.mdx +4 -1
- package/docs/zh/guides/advanced-features/_meta.json +0 -1
- package/docs/zh/guides/advanced-features/bff/extend-server.mdx +28 -76
- package/docs/zh/guides/advanced-features/bff/frameworks.mdx +6 -66
- package/docs/zh/guides/advanced-features/page-performance/_meta.json +1 -1
- package/docs/zh/guides/advanced-features/page-performance/react-compiler.mdx +44 -0
- package/docs/zh/guides/advanced-features/web-server.mdx +221 -24
- package/docs/zh/guides/basic-features/data/data-cache.mdx +1 -1
- package/docs/zh/guides/basic-features/deploy.mdx +4 -3
- package/docs/zh/guides/basic-features/output-files.mdx +0 -28
- package/docs/zh/guides/basic-features/render/ssr.mdx +1 -1
- package/docs/zh/guides/basic-features/render/streaming-ssr.mdx +1 -1
- package/docs/zh/guides/basic-features/static-assets/wasm-assets.mdx +1 -1
- package/docs/zh/guides/topic-detail/module-federation/deploy.mdx +83 -7
- package/docs/zh/guides/topic-detail/module-federation/usage.mdx +2 -4
- package/docs/zh/plugin/cli-plugins/api.mdx +1 -1
- package/docs/zh/tutorials/first-app/c04-routes.mdx +4 -2
- package/docs/zh/tutorials/first-app/c05-loader.mdx +4 -1
- package/package.json +7 -4
- package/rspress.config.ts +16 -1
- package/src/components/RsbuildLink/index.tsx +2 -2
- package/src/index.ts +1 -5
- package/src/pages/index.tsx +3 -3
- package/docs/en/apis/app/hooks/api/middleware.mdx +0 -11
- package/docs/en/apis/app/runtime/bff/hook.mdx +0 -44
- package/docs/en/apis/app/runtime/bff/use-context.mdx +0 -38
- package/docs/en/configure/app/bff/enable-handle-web.mdx +0 -24
- package/docs/en/guides/advanced-features/custom-server.mdx +0 -218
- package/docs/zh/apis/app/hooks/api/middleware.mdx +0 -11
- package/docs/zh/apis/app/runtime/bff/hook.mdx +0 -44
- package/docs/zh/apis/app/runtime/bff/use-context.mdx +0 -38
- package/docs/zh/configure/app/bff/enable-handle-web.mdx +0 -24
- package/docs/zh/guides/advanced-features/custom-server.mdx +0 -216
@@ -0,0 +1,44 @@
|
|
1
|
+
# React Compiler
|
2
|
+
|
3
|
+
React Compiler 是 React 19 引入的一个实验性编译器,它可以自动优化你的 React 代码。
|
4
|
+
|
5
|
+
在开始使用 React Compiler 之前,建议阅读 [React Compiler 文档](https://zh-hans.react.dev/learn/react-compiler),以了解 React Compiler 的功能、当前状态和使用方法。
|
6
|
+
|
7
|
+
## 如何使用
|
8
|
+
|
9
|
+
在 Modern.js 中使用 React Compiler 的步骤如下:
|
10
|
+
|
11
|
+
1. Modern.js 目前项目还未正式支持 React 19,可以在 React 17 或 18 项目中安装 `react-compiler-runtime@rc`,以允许编译后的代码在 19 之前的版本上运行。
|
12
|
+
|
13
|
+
2. 目前 React Compiler 仅提供了 Babel 插件,你需要安装 `babel-plugin-react-compiler`。
|
14
|
+
|
15
|
+
3. 在你的 Modern.js 配置文件中注册 Babel 插件:
|
16
|
+
|
17
|
+
```ts title="modern.config.ts"
|
18
|
+
import { appTools, defineConfig } from '@modern-js/app-tools';
|
19
|
+
|
20
|
+
export default defineConfig({
|
21
|
+
runtime: {
|
22
|
+
router: true,
|
23
|
+
},
|
24
|
+
tools: {
|
25
|
+
babel(_, { addPlugins }) {
|
26
|
+
addPlugins([
|
27
|
+
[
|
28
|
+
'babel-plugin-react-compiler',
|
29
|
+
{
|
30
|
+
target: '18',
|
31
|
+
},
|
32
|
+
],
|
33
|
+
]);
|
34
|
+
},
|
35
|
+
},
|
36
|
+
plugins: [
|
37
|
+
appTools({
|
38
|
+
bundler: 'rspack',
|
39
|
+
}),
|
40
|
+
],
|
41
|
+
});
|
42
|
+
```
|
43
|
+
|
44
|
+
> 详细代码可以参考:[Modern.js & React Compiler 示例项目](https://github.com/web-infra-dev/modern-js-examples/tree/main/examples/react-compiler)
|
@@ -2,22 +2,16 @@
|
|
2
2
|
sidebar_position: 16
|
3
3
|
---
|
4
4
|
|
5
|
-
# 自定义 Web Server
|
6
|
-
|
7
|
-
:::warning
|
8
|
-
自定义 Web Server 兼容但不再推荐使用,扩展 Server 能力请移步 [自定义 Server](/guides/advanced-features/custom-server.html),迁移指南参考 [迁移至新版自定义 Server](/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)。
|
9
|
-
:::
|
5
|
+
# 自定义 Web Server
|
10
6
|
|
11
7
|
Modern.js 将大部分项目需要的服务端能力都进行了封装,通常项目无需进行服务端开发。但在有些开发场景下,例如用户鉴权、请求预处理、添加页面渲染骨架等,项目仍需要对服务端进行定制。
|
12
8
|
|
13
|
-
|
9
|
+
## 开启自定义 Web Server
|
14
10
|
|
15
|
-
:::
|
16
|
-
|
11
|
+
:::info
|
12
|
+
必须确保 Modern.js 版本是 x.67.5 及以上。
|
17
13
|
:::
|
18
14
|
|
19
|
-
## 开启自定义 Web Server
|
20
|
-
|
21
15
|
开发者可以在项目根目录执行 `pnpm run new` 命令,开启「自定义 Web Server」功能:
|
22
16
|
|
23
17
|
```bash
|
@@ -25,19 +19,226 @@ Middleware 与 Hook 只会在用户请求页面路由时生效,BFF 路由不
|
|
25
19
|
? 请选择创建元素类型 新建「自定义 Web Server」源码目录
|
26
20
|
```
|
27
21
|
|
28
|
-
|
22
|
+
执行命令后,项目目录下会自动创建 `server/modern.server.ts` 文件,可以在这个文件中编写自定义逻辑。
|
23
|
+
|
24
|
+
|
25
|
+
## 自定义 Web Server 能力
|
26
|
+
|
27
|
+
`server/modern.server.ts` 文件中添加如下配置来扩展 Server:
|
28
|
+
- **中间件(Middleware)**
|
29
|
+
- **渲染中间件(RenderMiddleware)**
|
30
|
+
- **服务端插件(Plugin)**
|
29
31
|
|
30
|
-
|
31
|
-
import { serverPlugin } from '@modern-js/plugin-server';
|
32
|
+
其中 **Plugin** 中可以定义 **Middleware** 与 **RenderMiddleware**。 中间件加载流程如下图所示:
|
32
33
|
|
33
|
-
|
34
|
-
|
34
|
+
<img
|
35
|
+
src="https://lf3-static.bytednsdoc.com/obj/eden-cn/10eh7nuhpenuhog/server-md-wf.png"
|
36
|
+
style={{ width: '100%', maxWidth: '540px' }}
|
37
|
+
/>
|
38
|
+
|
39
|
+
### 基本配置
|
40
|
+
|
41
|
+
```ts title="server/modern.server.ts"
|
42
|
+
import { defineServerConfig } from '@modern-js/server-runtime';
|
43
|
+
|
44
|
+
export default defineServerConfig({
|
45
|
+
middlewares: [], // 中间件
|
46
|
+
renderMiddlewares: [], // 渲染中间件
|
47
|
+
plugins: [], // 插件
|
35
48
|
});
|
36
49
|
```
|
37
50
|
|
38
|
-
开启功能后,项目目录下会自动创建 `server/index.ts` 文件,可以在这个文件中编写自定义逻辑。
|
39
51
|
|
40
|
-
|
52
|
+
### 类型定义
|
53
|
+
|
54
|
+
`defineServerConfig` 类型定义如下:
|
55
|
+
|
56
|
+
```ts
|
57
|
+
import type { MiddlewareHandler } from 'hono';
|
58
|
+
|
59
|
+
type MiddlewareObj = {
|
60
|
+
name: string;
|
61
|
+
path?: string;
|
62
|
+
method?: 'options' | 'get' | 'post' | 'put' | 'delete' | 'patch' | 'all';
|
63
|
+
handler: MiddlewareHandler | MiddlewareHandler[];
|
64
|
+
};
|
65
|
+
type ServerConfig = {
|
66
|
+
middlewares?: MiddlewareObj[];
|
67
|
+
renderMiddlewares?: MiddlewareObj[];
|
68
|
+
plugins?: (ServerPlugin | ServerPluginLegacy)[];
|
69
|
+
}
|
70
|
+
```
|
71
|
+
|
72
|
+
|
73
|
+
### Middleware
|
74
|
+
|
75
|
+
Middleware 支持在 Modern.js 服务的**请求处理**与**页面路由**的流程前后,执行自定义逻辑。
|
76
|
+
即自定义逻辑既要处理接口路由,也要作用于页面路由,那么 Middleware 是不二选择。
|
77
|
+
|
78
|
+
:::note
|
79
|
+
如果仅需要处理 BFF 接口路由,可以通过检查 `req.path` 是否以 BFF `prefix` 开头,来判断是否为 BFF 接口请求。
|
80
|
+
:::
|
81
|
+
|
82
|
+
使用姿势如下:
|
83
|
+
|
84
|
+
```ts title="server/modern.server.ts"
|
85
|
+
import { defineServerConfig, type MiddlewareHandler } from '@modern-js/server-runtime';
|
86
|
+
|
87
|
+
export const handler: MiddlewareHandler = async (c, next) => {
|
88
|
+
const monitors = c.get('monitors');
|
89
|
+
const start = Date.now();
|
90
|
+
|
91
|
+
await next();
|
92
|
+
|
93
|
+
const end = Date.now();
|
94
|
+
// 上报耗时
|
95
|
+
monitors.timing('request_timing', end - start);
|
96
|
+
};
|
97
|
+
|
98
|
+
export default defineServerConfig({
|
99
|
+
middlewares: [
|
100
|
+
{
|
101
|
+
name: 'request-timing',
|
102
|
+
handler,
|
103
|
+
},
|
104
|
+
],
|
105
|
+
});
|
106
|
+
```
|
107
|
+
|
108
|
+
:::warning
|
109
|
+
必须执行 `next` 函数才会执行后续的 Middleware。
|
110
|
+
:::
|
111
|
+
|
112
|
+
|
113
|
+
### RenderMiddleware
|
114
|
+
|
115
|
+
如果只需要处理页面渲染的前后执行逻辑,modern.js 也提供了渲染中间件,使用姿势如下:
|
116
|
+
|
117
|
+
```ts title="server/modern.server.ts"
|
118
|
+
import { defineServerConfig, type MiddlewareHandler } from '@modern-js/server-runtime';
|
119
|
+
|
120
|
+
// 注入 render 性能指标
|
121
|
+
const renderTiming: MiddlewareHandler = async (c, next) => {
|
122
|
+
const start = Date.now();
|
123
|
+
|
124
|
+
await next();
|
125
|
+
|
126
|
+
const end = Date.now();
|
127
|
+
c.res.headers.set('server-timing', `render; dur=${end - start}`);
|
128
|
+
};
|
129
|
+
|
130
|
+
// 修改响应体
|
131
|
+
const modifyResBody: MiddlewareHandler = async (c, next) => {
|
132
|
+
await next();
|
133
|
+
|
134
|
+
const { res } = c;
|
135
|
+
const text = await res.text();
|
136
|
+
const newText = text.replace('<body>', '<body> <h3>bytedance</h3>');
|
137
|
+
|
138
|
+
c.res = c.body(newText, {
|
139
|
+
status: res.status,
|
140
|
+
headers: res.headers,
|
141
|
+
});
|
142
|
+
};
|
143
|
+
|
144
|
+
export default defineServerConfig({
|
145
|
+
renderMiddlewares: [
|
146
|
+
{
|
147
|
+
name: 'render-timing',
|
148
|
+
handler: renderTiming,
|
149
|
+
},
|
150
|
+
{
|
151
|
+
name: 'modify-res-body',
|
152
|
+
handler: modifyResBody,
|
153
|
+
},
|
154
|
+
],
|
155
|
+
});
|
156
|
+
```
|
157
|
+
|
158
|
+
|
159
|
+
### Plugin
|
160
|
+
|
161
|
+
Modern.js 支持在自定义插件中为 Server 添加上述 Middleware 及 RenderMiddleware,使用姿势如下:
|
162
|
+
|
163
|
+
|
164
|
+
```ts title="server/plugins/server.ts"
|
165
|
+
import type { ServerPluginLegacy } from '@modern-js/server-runtime';
|
166
|
+
|
167
|
+
export default (): ServerPluginLegacy => ({
|
168
|
+
name: 'serverPlugin',
|
169
|
+
setup(api) {
|
170
|
+
return {
|
171
|
+
prepare(serverConfig) {
|
172
|
+
const { middlewares, renderMiddlewares } = api.useAppContext();
|
173
|
+
|
174
|
+
// 注入服务端数据,供页面 dataLoader 消费
|
175
|
+
middlewares?.push({
|
176
|
+
name: 'server-plugin-middleware',
|
177
|
+
handler: async (c, next) => {
|
178
|
+
c.set('message', 'hi modern.js');
|
179
|
+
await next();
|
180
|
+
// ...
|
181
|
+
},
|
182
|
+
});
|
183
|
+
|
184
|
+
// 重定向
|
185
|
+
renderMiddlewares?.push({
|
186
|
+
name: 'server-plugin-render-middleware',
|
187
|
+
handler: async (c, next) => {
|
188
|
+
const user = getUser(c.req);
|
189
|
+
if (!user) {
|
190
|
+
return c.redirect('/login');
|
191
|
+
}
|
192
|
+
|
193
|
+
await next();
|
194
|
+
},
|
195
|
+
});
|
196
|
+
return serverConfig;
|
197
|
+
},
|
198
|
+
};
|
199
|
+
},
|
200
|
+
});
|
201
|
+
```
|
202
|
+
|
203
|
+
|
204
|
+
```ts title="server/modern.server.ts"
|
205
|
+
import { defineServerConfig } from '@modern-js/server-runtime';
|
206
|
+
import serverPlugin from './plugins/serverPlugin';
|
207
|
+
|
208
|
+
export default defineServerConfig({
|
209
|
+
plugins: [serverPlugin()],
|
210
|
+
});
|
211
|
+
```
|
212
|
+
|
213
|
+
|
214
|
+
```ts title="src/routes/page.data.ts"
|
215
|
+
import { useHonoContext } from '@modern-js/server-runtime';
|
216
|
+
import { defer } from '@modern-js/runtime/router';
|
217
|
+
|
218
|
+
export default () => {
|
219
|
+
const ctx = useHonoContext();
|
220
|
+
// SSR 场景消费服务端注入的数据
|
221
|
+
const message = ctx.get('message');
|
222
|
+
|
223
|
+
// ...
|
224
|
+
};
|
225
|
+
|
226
|
+
```
|
227
|
+
|
228
|
+
|
229
|
+
## 旧版 API(废弃)
|
230
|
+
|
231
|
+
:::warning
|
232
|
+
旧版 API 兼容但不再推荐使用,扩展 Server 能力请移步 [自定义 Web Server](/guides/advanced-features/web-server.html),迁移指南参考 [迁移至新版自定义 Web Server](/guides/advanced-features/web-server.html#迁移至新版自定义-web-server)。
|
233
|
+
:::
|
234
|
+
|
235
|
+
### 开启
|
236
|
+
开启自定义 Web Server 功能,需要执行以下步骤:
|
237
|
+
1. devDependencies 增加 `@modern-js/plugin-server`、`tsconfig-paths` 及 `ts-node`依赖并安装。
|
238
|
+
2. `tsconfig` 的 `include` 中添加 `server`。
|
239
|
+
3. `modern.config.ts` 中注册 `@modern-js/plugin-server` 插件。
|
240
|
+
4. 项目目录下创建 `server/index.ts` 文件,可以在这个文件中编写自定义逻辑。
|
241
|
+
|
41
242
|
|
42
243
|
### Unstable Middleware
|
43
244
|
|
@@ -68,10 +269,6 @@ export const unstableMiddleware: UnstableMiddleware[] = [time];
|
|
68
269
|
|
69
270
|
### Hook
|
70
271
|
|
71
|
-
:::warning
|
72
|
-
我们推荐使用 UnstableMiddleware 代替 Hook。
|
73
|
-
:::
|
74
|
-
|
75
272
|
Modern.js 提供的 Hook 用于控制 Web Server 中的特定逻辑,所有的页面请求都会经过 Hook。
|
76
273
|
|
77
274
|
目前提供了两种 Hook,分别是 `AfterMatch` 和 `AfterRender`,开发者可以在 `server/index.ts` 中这样写:
|
@@ -102,12 +299,12 @@ export const afterRender: AfterRenderHook = (ctx, next) => {
|
|
102
299
|
:::
|
103
300
|
|
104
301
|
|
105
|
-
## 迁移至新版自定义 Server
|
302
|
+
## 迁移至新版自定义 Web Server
|
106
303
|
|
107
304
|
### 迁移背景
|
108
305
|
|
109
306
|
Modern.js Server 在不断演进,为了提供更强大的功能,我们对中间件和 Server 插件的定义和使用方式进行了优化。
|
110
|
-
|
307
|
+
虽然旧版 API 仍被兼容,但我们强烈建议您按照本指南进行迁移,以充分利用新版的优势。
|
111
308
|
|
112
309
|
### 迁移步骤
|
113
310
|
|
@@ -152,7 +349,7 @@ UnstableMiddleware `Context` 和 Hono `Context` 的具体差异:
|
|
152
349
|
| `c.request` | `c.req.raw` | 参考 [HonoRequest raw](https://hono.dev/docs/api/request#raw) 文档 |
|
153
350
|
| `c.response` | `c.res` | 参考 [Hono Context res](https://hono.dev/docs/api/context#res) 文档 |
|
154
351
|
| `c.route` | `c.get('route')` | 获取应用上下文信息 |
|
155
|
-
| `loaderContext.get` | `honoContext.get` | 通过 `c.set` 注入数据后 dataLoader 中消费:旧版通过 `loaderContext.get` 获取,新版参考 [Plugin](/guides/advanced-features/
|
352
|
+
| `loaderContext.get` | `honoContext.get` | 通过 `c.set` 注入数据后 dataLoader 中消费:旧版通过 `loaderContext.get` 获取,新版参考 [Plugin](/guides/advanced-features/web-server.html#使用姿势-2) 示例 |
|
156
353
|
|
157
354
|
#### Middleware
|
158
355
|
|
@@ -159,7 +159,7 @@ const getComplexStatistics = cache(
|
|
159
159
|
}
|
160
160
|
);
|
161
161
|
|
162
|
-
revalidateTag('dashboard
|
162
|
+
revalidateTag('dashboard'); // 会使 getDashboardStats 函数和 getComplexStatistics 函数的缓存都失效
|
163
163
|
```
|
164
164
|
|
165
165
|
#### `getKey` 参数
|
@@ -110,7 +110,6 @@ Netlify 是一个流行的 Web 开发平台,专为构建、发布和维护现
|
|
110
110
|
|
111
111
|
:::info
|
112
112
|
你可参考部署[项目示例](https://github.com/web-infra-dev/modern-js-examples/tree/main/examples/modern-js-deploy-csr)。
|
113
|
-
|
114
113
|
:::
|
115
114
|
|
116
115
|
在 Netlify 平台上添加项目,部署即可。
|
@@ -197,6 +196,7 @@ Vercel 是一个面向现代 Web 应用的部署平台,它提供了丰富的
|
|
197
196
|
### 纯前端项目
|
198
197
|
|
199
198
|
在当前项目的根目录添加 `vercel.json` 文件:
|
199
|
+
|
200
200
|
```bash
|
201
201
|
./
|
202
202
|
├── src
|
@@ -206,6 +206,7 @@ Vercel 是一个面向现代 Web 应用的部署平台,它提供了丰富的
|
|
206
206
|
```
|
207
207
|
|
208
208
|
在 `vercel.json` 中添加以下内容:
|
209
|
+
|
209
210
|
```json title="vercel.json"
|
210
211
|
{
|
211
212
|
"buildCommand": "modern deploy",
|
@@ -219,7 +220,6 @@ Vercel 是一个面向现代 Web 应用的部署平台,它提供了丰富的
|
|
219
220
|
|
220
221
|
:::info
|
221
222
|
你可参考部署[项目示例](https://github.com/web-infra-dev/modern-js-examples/tree/main/examples/modern-js-deploy-csr)。
|
222
|
-
|
223
223
|
:::
|
224
224
|
|
225
225
|
### 全栈项目
|
@@ -314,6 +314,7 @@ Github Pages 支持两种部署方式,通过分支部署或通过 Github Actio
|
|
314
314
|
4. 执行 `npm run deploy:gh-pages`。
|
315
315
|
|
316
316
|
:::info
|
317
|
+
|
317
318
|
1. 执行 `MODERNJS_DEPLOY=ghPages modern deploy`,Modern.js 会把可用于 github 部署的产物构建到 `.output` 目录。
|
318
319
|
2. 可以参考项目[示例](https://github.com/web-infra-dev/modern-js-examples/tree/main/examples/modern-js-deploy-csr)。
|
319
320
|
|
@@ -393,7 +394,7 @@ Nginx 是一个高性能的 HTTP 和反向代理服务器,它可以处理静
|
|
393
394
|
|
394
395
|
如果你的项目是纯前端项目,也可以通过 Nginx 来部署应用,以下提供一个 Nginx 配置的示例来演示如何托管一个纯前端项目的产物。
|
395
396
|
|
396
|
-
```
|
397
|
+
```nginx title="nginx.conf"
|
397
398
|
# user [user] [group];
|
398
399
|
worker_processes 1;
|
399
400
|
|
@@ -96,34 +96,6 @@ dist
|
|
96
96
|
└── qux.[hash].mp4
|
97
97
|
```
|
98
98
|
|
99
|
-
## Node.js 产物目录
|
100
|
-
|
101
|
-
当你在 Modern.js 中开启了 SSR 或 SSG 等服务端功能时,Modern.js 会在构建后生成一份 Node.js 产物,并输出到 `bundles` 目录下:
|
102
|
-
|
103
|
-
```bash
|
104
|
-
dist
|
105
|
-
├── bundles
|
106
|
-
│ └── [name].js
|
107
|
-
├── static
|
108
|
-
└── html
|
109
|
-
```
|
110
|
-
|
111
|
-
Node.js 产物通常只包含 JS 文件,不包含 HTML、CSS 等文件。此外,Node 产物的 JS 文件名称也不会自动生成哈希值。
|
112
|
-
|
113
|
-
你可以通过 [output.distPath.server](/configure/app/output/dist-path) 配置项来修改 Node 产物的输出路径。
|
114
|
-
|
115
|
-
比如,将 Node.js 产物输出到 `server` 目录:
|
116
|
-
|
117
|
-
```ts
|
118
|
-
export default {
|
119
|
-
output: {
|
120
|
-
distPath: {
|
121
|
-
server: 'server',
|
122
|
-
},
|
123
|
-
},
|
124
|
-
};
|
125
|
-
```
|
126
|
-
|
127
99
|
## 扁平化产物目录
|
128
100
|
|
129
101
|
有时候你不想产物目录有太多层级,可以将目录设置为空字符串,使生成的产物目录扁平化。
|
@@ -222,7 +222,7 @@ document.addEventListener('load', () => {
|
|
222
222
|
export const doSomething = () => {}
|
223
223
|
```
|
224
224
|
|
225
|
-
如果直接引用到组件中,会造成
|
225
|
+
如果直接引用到组件中,会造成 SSR 加载报错,即使你已经使用环境变量进行判断,但仍然无法移除依赖中副作用的执行。
|
226
226
|
|
227
227
|
```tsx title="routes/page.tsx"
|
228
228
|
import { doSomething } from 'packageA';
|
@@ -72,7 +72,7 @@ export const loader = ({ params }: LoaderFunctionArgs) => {
|
|
72
72
|
另外,`defer` 还可以同时接收异步数据和同步数据。在下述例子中,我们等待部分耗时较短的请求,在响应后通过对象数据返回,而耗时较长时间的请求,则通过 Promise 返回:
|
73
73
|
|
74
74
|
```ts title="user/[id]/page.data.ts"
|
75
|
-
export const loader = ({ params }: LoaderFunctionArgs) => {
|
75
|
+
export const loader = async ({ params }: LoaderFunctionArgs) => {
|
76
76
|
const userId = params.id;
|
77
77
|
|
78
78
|
const user = new Promise<User>(resolve => {
|
@@ -1,8 +1,17 @@
|
|
1
1
|
# 部署
|
2
2
|
|
3
|
-
通常情况下,部署 Module Federation
|
3
|
+
通常情况下,部署 Module Federation 应用,需要注意两点:
|
4
4
|
|
5
|
-
|
5
|
+
1. 保证消费者配置文件中的远程模块地址无误,消费者能够正确访问到生产者的 `manifest` 文件。
|
6
|
+
2. 保证生产者 `manifest` 文件中各个资源能被正确访问到。
|
7
|
+
|
8
|
+
我们推荐使用 Modern.js 的 [Node 服务](/guides/basic-features/deploy.html#modernjs-内置-nodejs-服务器)来部署 Module Federation 应用,以获得开箱即用的体验。
|
9
|
+
|
10
|
+
## 消费者
|
11
|
+
|
12
|
+
对于 Module Federation 的消费者来说,它与生产者的联系就是在配置文件中的远程模块地址。
|
13
|
+
|
14
|
+
例如生产者部署在 `https://my-remote-module` 这个域名下,开发者需要修改消费者的配置文件:
|
6
15
|
|
7
16
|
```ts title="module-federation.config.ts"
|
8
17
|
import { createModuleFederationConfig } from '@module-federation/modern-js';
|
@@ -10,7 +19,7 @@ import { createModuleFederationConfig } from '@module-federation/modern-js';
|
|
10
19
|
export default createModuleFederationConfig({
|
11
20
|
name: 'host',
|
12
21
|
remotes: {
|
13
|
-
remote: 'remote@
|
22
|
+
remote: 'remote@https://my-remote-module/static/mf-manifest.json',
|
14
23
|
},
|
15
24
|
shared: {
|
16
25
|
react: { singleton: true },
|
@@ -19,10 +28,77 @@ export default createModuleFederationConfig({
|
|
19
28
|
});
|
20
29
|
```
|
21
30
|
|
22
|
-
|
31
|
+
此时,消费者将加载远程模块生产环境的 `manifest` 配置文件。
|
32
|
+
|
33
|
+
:::note
|
34
|
+
上述代码中,远程模块的地址是 `/static/mf-manifest.json`,这只是以 Modern.js 默认的产物路径举例。在实际项目中,开发者需要根据实际的访问路径进行配置。
|
35
|
+
:::
|
36
|
+
|
37
|
+
## 生产者
|
38
|
+
|
39
|
+
对于 Module Federation 的生产者,开发者需要正确的配置 [`output.assetPrefix`](/configure/app/output/asset-prefix) 配置,它会影响到:
|
40
|
+
|
41
|
+
1. `mf-manifest.json` 中定义的 `publicPath`,它决定了远程模块其他资源的访问路径。
|
42
|
+
2. 通过 Modern.js 服务直接托管产物时,`mf-manifest.json` 文件的访问路径。
|
43
|
+
|
44
|
+
在生产环境,开发者需要将 `output.assetPrefix` 配置为生产环境的访问路径。例如我们将生产者部署在 `https://my-remote-module` 这个域名下,需要将 `output.assetPrefix` 配置为 `https://my-remote-module`。
|
45
|
+
|
46
|
+
```ts title="modern.config.ts"
|
47
|
+
import { defineConfig } from '@modern-js/app-tools';
|
48
|
+
|
49
|
+
export default defineConfig({
|
50
|
+
output: {
|
51
|
+
assetPrefix: 'https://my-remote-module',
|
52
|
+
},
|
53
|
+
});
|
54
|
+
```
|
55
|
+
|
56
|
+
此时,生产者构建产物 `mf-manifest.json` 中定义的 `publicPath` 为 `https://my-remote-module`,例如:
|
57
|
+
|
58
|
+
```json
|
59
|
+
{
|
60
|
+
"id": "remote",
|
61
|
+
"name": "remote",
|
62
|
+
"metaData": {
|
63
|
+
"name": "remote",
|
64
|
+
"publicPath": "https://my-remote-module/"
|
65
|
+
},
|
66
|
+
"shared": [ /* xxx */ ],
|
67
|
+
"remotes": [],
|
68
|
+
"exposes": [ /* xxx */ ]
|
69
|
+
}
|
70
|
+
```
|
71
|
+
|
72
|
+
消费者在访问远程模块时,会自动将 `publicPath` 拼接在远程模块的资源路径前。
|
73
|
+
|
74
|
+
该配置也会影响到生产者 `mf-manifest.json` 的访问路径。例如将这个值设置为 `MyDomain/module-a` 时,`mf-manifest.json` 的托管路径变为 `MyDomain/module-a/static/mf-manifest.json`。
|
75
|
+
|
76
|
+
此时,消费者在配置远程模块时,需要配置以下地址:
|
77
|
+
|
78
|
+
```ts title="module-federation.config.ts"
|
79
|
+
import { createModuleFederationConfig } from '@module-federation/modern-js';
|
80
|
+
|
81
|
+
export default createModuleFederationConfig({
|
82
|
+
name: 'host',
|
83
|
+
remotes: {
|
84
|
+
remote: 'remote@MyDomain/module-a/static/mf-manifest.json',
|
85
|
+
},
|
86
|
+
});
|
87
|
+
```
|
88
|
+
|
89
|
+
## 本地验证部署
|
23
90
|
|
24
|
-
|
91
|
+
Modern.js 提供了 `modern deploy` 命令,可以方便生成可运行在 Node.js 环境的产物。
|
25
92
|
|
26
|
-
|
93
|
+
```bash
|
94
|
+
modern deploy
|
95
|
+
```
|
96
|
+
|
97
|
+
执行命令后,可以在控制台看到以下输出:
|
98
|
+
|
99
|
+
```bash
|
100
|
+
Static directory: .output/static
|
101
|
+
You can preview this build by node .output/index
|
102
|
+
```
|
27
103
|
|
28
|
-
|
104
|
+
此时,开发者只需要运行 `node .output/index` 即可在本地预览部署后的效果。无论是 CSR 应用或是 SSR 应用,所有的 Module Federation 产物都能够被正确的访问。
|
@@ -189,8 +189,6 @@ export default Index;
|
|
189
189
|
import { appTools, defineConfig } from '@modern-js/app-tools';
|
190
190
|
import { moduleFederationPlugin } from '@module-federation/modern-js';
|
191
191
|
|
192
|
-
const isLocal = process.env.IS_LOCAL === 'true';
|
193
|
-
|
194
192
|
// https://modernjs.dev/en/configure/app/usage
|
195
193
|
export default defineConfig({
|
196
194
|
server: {
|
@@ -203,7 +201,7 @@ export default defineConfig({
|
|
203
201
|
// Now this configuration is only used in the local when you run modern serve command.
|
204
202
|
// If you want to deploy the application to the platform, use your own domain name.
|
205
203
|
// Module federation will automatically write it to mf-manifest.json, which influences consumer to fetch remoteEntry.js.
|
206
|
-
assetPrefix:
|
204
|
+
assetPrefix: 'http://127.0.0.1:3051',
|
207
205
|
},
|
208
206
|
plugins: [
|
209
207
|
appTools({
|
@@ -214,7 +212,7 @@ export default defineConfig({
|
|
214
212
|
});
|
215
213
|
```
|
216
214
|
|
217
|
-
现在,在生产者中运行 `
|
215
|
+
现在,在生产者中运行 `modern build && modern serve`,在消费者中运行 `modern build && modern serve`,即可在本地模拟生产环境,访问到远程模块。
|
218
216
|
|
219
217
|
上述用例可以参考:[Modern.js & Module Federation 基础用法示例](https://github.com/web-infra-dev/modern-js-examples/tree/main/examples/module-federation/base)。
|
220
218
|
|
@@ -118,8 +118,9 @@ import { Radio } from 'antd';
|
|
118
118
|
|
119
119
|
然后将 UI 最顶部进行修改,增加一组单选框
|
120
120
|
|
121
|
-
```tsx
|
121
|
+
```tsx
|
122
122
|
export default function Layout() {
|
123
|
+
// [!code highlight:8]
|
123
124
|
return (
|
124
125
|
<div>
|
125
126
|
<div className="h-16 p-2 flex items-center justify-center">
|
@@ -146,8 +147,9 @@ import { Outlet, useLocation, useNavigate } from '@modern-js/runtime/router';
|
|
146
147
|
|
147
148
|
最后在 Layout 组件里增加局部状态和相关逻辑:
|
148
149
|
|
149
|
-
```tsx
|
150
|
+
```tsx
|
150
151
|
export default function Layout() {
|
152
|
+
// [!code highlight:8]
|
151
153
|
const navigate = useNavigate();
|
152
154
|
const location = useLocation();
|
153
155
|
const [currentList, setList] = useState(location.pathname || '/');
|
@@ -56,11 +56,13 @@ Data Loader 并非只为 SSR 工作。在 CSR 项目中,Data Loader 也可以
|
|
56
56
|
|
57
57
|
Modern.js 也提供了一个叫 `useLoaderData` 的 hooks API,我们修改 `src/routes/page.tsx` 导出的组件:
|
58
58
|
|
59
|
-
```tsx
|
59
|
+
```tsx
|
60
|
+
// [!code highlight:2]
|
60
61
|
import { useLoaderData } from '@modern-js/runtime/router';
|
61
62
|
import type { LoaderData } from './page.data';
|
62
63
|
|
63
64
|
function Index() {
|
65
|
+
// [!code highlight:1]
|
64
66
|
const { data } = useLoaderData() as LoaderData;
|
65
67
|
|
66
68
|
return (
|
@@ -68,6 +70,7 @@ function Index() {
|
|
68
70
|
<Helmet>
|
69
71
|
<title>All</title>
|
70
72
|
</Helmet>
|
73
|
+
// [!code highlight:1]
|
71
74
|
<List
|
72
75
|
dataSource={data}
|
73
76
|
renderItem={info => <Item key={info.name} info={info} />}
|