@modern-js/main-doc 3.0.0 → 3.0.2
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/_nav.json +1 -1
- package/docs/en/apis/app/commands.mdx +1 -1
- package/docs/en/community/blog/_meta.json +7 -1
- package/docs/en/community/blog/v3-release-note.mdx +625 -0
- package/docs/en/components/rsbuild.mdx +1 -1
- package/docs/en/configure/app/builder-plugins.mdx +1 -1
- package/docs/en/configure/app/dev/server.mdx +7 -7
- package/docs/en/configure/app/resolve/alias-strategy.mdx +1 -1
- package/docs/en/configure/app/resolve/dedupe.mdx +1 -1
- package/docs/en/configure/app/security/sri.mdx +1 -1
- package/docs/en/configure/app/server/rsc.mdx +2 -2
- package/docs/en/configure/app/source/decorators.mdx +1 -1
- package/docs/en/configure/app/tools/bundler-chain.mdx +1 -1
- package/docs/en/configure/app/tools/dev-server.mdx +1 -1
- package/docs/en/configure/app/tools/swc.mdx +2 -2
- package/docs/en/guides/advanced-features/compatibility.mdx +1 -1
- package/docs/en/guides/advanced-features/page-performance/optimize-bundle.mdx +1 -1
- package/docs/en/guides/basic-features/css/css.mdx +4 -4
- package/docs/en/guides/basic-features/css/tailwindcss.mdx +2 -2
- package/docs/en/guides/basic-features/debug/proxy.mdx +1 -1
- package/docs/en/guides/basic-features/render/rsc.mdx +137 -8
- package/docs/en/guides/concept/builder.mdx +3 -3
- package/docs/en/guides/get-started/tech-stack.mdx +3 -3
- package/docs/en/guides/topic-detail/module-federation/application.mdx +6 -6
- package/docs/en/guides/topic-detail/module-federation/deploy.mdx +2 -2
- package/docs/en/guides/topic-detail/module-federation/i18n.mdx +12 -12
- package/docs/en/guides/topic-detail/module-federation/introduce.mdx +1 -1
- package/docs/en/guides/topic-detail/module-federation/ssr.mdx +4 -4
- package/docs/en/guides/topic-detail/module-federation/usage.mdx +7 -7
- package/docs/en/guides/troubleshooting/builder.mdx +4 -4
- package/docs/en/guides/upgrade/config.mdx +31 -3
- package/docs/en/plugin/cli-plugins/api.mdx +9 -9
- package/docs/en/plugin/introduction.mdx +5 -5
- package/docs/zh/_nav.json +1 -1
- package/docs/zh/apis/app/commands.mdx +1 -1
- package/docs/zh/community/blog/_meta.json +7 -1
- package/docs/zh/community/blog/v3-release-note.mdx +626 -0
- package/docs/zh/components/rsbuild.mdx +1 -1
- package/docs/zh/configure/app/builder-plugins.mdx +1 -1
- package/docs/zh/configure/app/dev/server.mdx +7 -7
- package/docs/zh/configure/app/resolve/alias-strategy.mdx +1 -1
- package/docs/zh/configure/app/resolve/dedupe.mdx +1 -1
- package/docs/zh/configure/app/security/sri.mdx +1 -1
- package/docs/zh/configure/app/server/rsc.mdx +2 -2
- package/docs/zh/configure/app/source/decorators.mdx +1 -1
- package/docs/zh/configure/app/tools/bundler-chain.mdx +1 -1
- package/docs/zh/configure/app/tools/dev-server.mdx +1 -1
- package/docs/zh/configure/app/tools/swc.mdx +2 -2
- package/docs/zh/guides/advanced-features/compatibility.mdx +1 -1
- package/docs/zh/guides/advanced-features/page-performance/optimize-bundle.mdx +1 -1
- package/docs/zh/guides/basic-features/css/css.mdx +4 -4
- package/docs/zh/guides/basic-features/css/tailwindcss.mdx +2 -2
- package/docs/zh/guides/basic-features/debug/proxy.mdx +1 -1
- package/docs/zh/guides/basic-features/render/rsc.mdx +222 -8
- package/docs/zh/guides/concept/builder.mdx +3 -3
- package/docs/zh/guides/get-started/tech-stack.mdx +3 -3
- package/docs/zh/guides/topic-detail/module-federation/application.mdx +6 -6
- package/docs/zh/guides/topic-detail/module-federation/deploy.mdx +2 -2
- package/docs/zh/guides/topic-detail/module-federation/i18n.mdx +12 -12
- package/docs/zh/guides/topic-detail/module-federation/introduce.mdx +1 -1
- package/docs/zh/guides/topic-detail/module-federation/ssr.mdx +4 -4
- package/docs/zh/guides/topic-detail/module-federation/usage.mdx +7 -7
- package/docs/zh/guides/troubleshooting/builder.mdx +4 -4
- package/docs/zh/guides/upgrade/config.mdx +31 -3
- package/docs/zh/plugin/cli-plugins/api.mdx +9 -9
- package/docs/zh/plugin/introduction.mdx +5 -5
- package/package.json +6 -5
- package/rspress.config.ts +1 -2
- package/src/components/Mermaid/index.tsx +3 -0
- package/src/components/RsbuildLink/index.tsx +1 -1
- package/src/index.ts +1 -1
- package/src/pages/index.tsx +1 -1
|
@@ -28,7 +28,7 @@ export default {
|
|
|
28
28
|
};
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
-
更多详细信息请参考 [Rsbuild - server.compress](https://rsbuild.
|
|
31
|
+
更多详细信息请参考 [Rsbuild - server.compress](https://v2.rsbuild.dev/zh/config/server/compress) 文档。
|
|
32
32
|
|
|
33
33
|
### headers
|
|
34
34
|
|
|
@@ -49,7 +49,7 @@ export default {
|
|
|
49
49
|
};
|
|
50
50
|
```
|
|
51
51
|
|
|
52
|
-
更多详细信息请参考 [Rsbuild - server.headers](https://rsbuild.
|
|
52
|
+
更多详细信息请参考 [Rsbuild - server.headers](https://v2.rsbuild.dev/zh/config/server/headers) 文档。
|
|
53
53
|
|
|
54
54
|
### historyApiFallback
|
|
55
55
|
|
|
@@ -68,7 +68,7 @@ export default {
|
|
|
68
68
|
};
|
|
69
69
|
```
|
|
70
70
|
|
|
71
|
-
更多配置选项请参考 [Rsbuild - server.historyApiFallback](https://rsbuild.
|
|
71
|
+
更多配置选项请参考 [Rsbuild - server.historyApiFallback](https://v2.rsbuild.dev/zh/config/server/history-api-fallback) 文档。
|
|
72
72
|
|
|
73
73
|
### watch
|
|
74
74
|
|
|
@@ -77,7 +77,7 @@ export default {
|
|
|
77
77
|
|
|
78
78
|
是否监听 `mock/`、`server/`、`api/` 等目录的文件变化。
|
|
79
79
|
|
|
80
|
-
更多详细信息请参考 [Rsbuild - dev.watchFiles](https://rsbuild.
|
|
80
|
+
更多详细信息请参考 [Rsbuild - dev.watchFiles](https://v2.rsbuild.dev/zh/config/dev/watch-files) 文档。
|
|
81
81
|
|
|
82
82
|
### cors
|
|
83
83
|
|
|
@@ -100,7 +100,7 @@ const defaultOptions = {
|
|
|
100
100
|
};
|
|
101
101
|
```
|
|
102
102
|
|
|
103
|
-
更多配置选项和详细用法请参考 [Rsbuild - server.cors](https://rsbuild.
|
|
103
|
+
更多配置选项和详细用法请参考 [Rsbuild - server.cors](https://v2.rsbuild.dev/zh/config/server/cors) 文档。
|
|
104
104
|
|
|
105
105
|
### proxy
|
|
106
106
|
|
|
@@ -116,7 +116,7 @@ export default {
|
|
|
116
116
|
proxy: {
|
|
117
117
|
// http://localhost:8080/api -> https://example.com/api
|
|
118
118
|
// http://localhost:8080/api/foo -> https://example.com/api/foo
|
|
119
|
-
'/api': 'https://example.com
|
|
119
|
+
'/api': 'https://example.com',
|
|
120
120
|
},
|
|
121
121
|
},
|
|
122
122
|
},
|
|
@@ -124,5 +124,5 @@ export default {
|
|
|
124
124
|
```
|
|
125
125
|
|
|
126
126
|
:::tip
|
|
127
|
-
该选项与 Rsbuild 的 `server.proxy` 选项一致,更多用法请参考 [Rsbuild - server.proxy](https://v2.rsbuild.
|
|
127
|
+
该选项与 Rsbuild 的 `server.proxy` 选项一致,更多用法请参考 [Rsbuild - server.proxy](https://v2.rsbuild.dev/zh/config/server/proxy)。
|
|
128
128
|
:::
|
|
@@ -7,7 +7,7 @@ title: aliasStrategy
|
|
|
7
7
|
- **类型:** `'prefer-tsconfig' | 'prefer-alias'`
|
|
8
8
|
- **默认值:** `'prefer-tsconfig'`
|
|
9
9
|
|
|
10
|
-
设置路径别名解析的策略,用于控制 `tsconfig.json` 中的 paths 选项与 Rsbuild 的 [resolve.alias](https://rsbuild.
|
|
10
|
+
设置路径别名解析的策略,用于控制 `tsconfig.json` 中的 paths 选项与 Rsbuild 的 [resolve.alias](https://v2.rsbuild.dev/config/resolve/alias) 选项之间的优先级关系。
|
|
11
11
|
|
|
12
12
|
import RsbuildConfig from '@site-docs/components/rsbuild-config-tooltip';
|
|
13
13
|
|
|
@@ -6,7 +6,7 @@ title: dedupe
|
|
|
6
6
|
|
|
7
7
|
- **类型:** `string[]`
|
|
8
8
|
|
|
9
|
-
强制 Rsbuild 从项目根目录解析指定的包,这可以用于移除重复包和减少包大小。对应 Rsbuild 的 [resolve.dedupe](https://rsbuild.
|
|
9
|
+
强制 Rsbuild 从项目根目录解析指定的包,这可以用于移除重复包和减少包大小。对应 Rsbuild 的 [resolve.dedupe](https://v2.rsbuild.dev/config/resolve/dedupe) 配置。
|
|
10
10
|
|
|
11
11
|
|
|
12
12
|
import RsbuildConfig from '@site-docs/components/rsbuild-config-tooltip';
|
|
@@ -18,7 +18,7 @@ type SriOptions = {
|
|
|
18
18
|
为 HTML 所引入的 `<script>` 和 `<link rel="stylesheet">` 标签添加完整性属性 —— `integrity`,使浏览器能够验证引入资源的完整性,以此防止下载的资源被篡改。
|
|
19
19
|
|
|
20
20
|
:::info
|
|
21
|
-
SRI 的具体介绍可以参考 [Rsbuild security.sri](https://rsbuild.
|
|
21
|
+
SRI 的具体介绍可以参考 [Rsbuild security.sri](https://v2.rsbuild.dev/zh/config/security/sri)。
|
|
22
22
|
|
|
23
23
|
该配置类型和 Rsbuild 不完全一致,在构建时将自动进行转换。
|
|
24
24
|
:::
|
|
@@ -21,8 +21,8 @@ export default defineConfig({
|
|
|
21
21
|
|
|
22
22
|
:::tip 前置条件
|
|
23
23
|
在启用 RSC 之前,请确保:
|
|
24
|
-
1. React 和 React DOM 已升级到 19 版本(建议 19.2.
|
|
25
|
-
2. 已安装 `react-server-dom-
|
|
24
|
+
1. React 和 React DOM 已升级到 19 版本(建议 19.2.4 以上版本)
|
|
25
|
+
2. 已安装 `react-server-dom-rspack@0.0.1-beta.0` 依赖
|
|
26
26
|
|
|
27
27
|
:::
|
|
28
28
|
|
|
@@ -21,7 +21,7 @@ const defaultDecorators = {
|
|
|
21
21
|
```
|
|
22
22
|
|
|
23
23
|
:::warning
|
|
24
|
-
该配置项的使用方式与 Rsbuild 一致,但默认值不同。详细信息请参考 [Rsbuild - source.decorators](https://rsbuild.
|
|
24
|
+
该配置项的使用方式与 Rsbuild 一致,但默认值不同。详细信息请参考 [Rsbuild - source.decorators](https://v2.rsbuild.dev/zh/config/source/decorators)。
|
|
25
25
|
:::
|
|
26
26
|
|
|
27
27
|
我们发现大部分的项目仍使用 `legacy` 版本的装饰器语法,因此默认值为 `legacy`。
|
|
@@ -30,4 +30,4 @@ Bundler chain 是基于 Rspack 配置 API 实现的链式配置工具,你可
|
|
|
30
30
|
|
|
31
31
|
> `tools.bundlerChain` 的执行时机早于 tools.rspack,因此会被 tools.rspack 中的修改所覆盖。
|
|
32
32
|
|
|
33
|
-
更多信息可参考 [Rsbuild#tools.bundlerChain](https://rsbuild.
|
|
33
|
+
更多信息可参考 [Rsbuild#tools.bundlerChain](https://v2.rsbuild.dev/zh/config/tools/bundler-chain)。
|
|
@@ -102,7 +102,7 @@ export default {
|
|
|
102
102
|
proxy: {
|
|
103
103
|
// http://localhost:8080/api -> https://example.com/api
|
|
104
104
|
// http://localhost:8080/api/foo -> https://example.com/api/foo
|
|
105
|
-
'/api': 'https://example.com
|
|
105
|
+
'/api': 'https://example.com',
|
|
106
106
|
},
|
|
107
107
|
},
|
|
108
108
|
},
|
|
@@ -35,7 +35,7 @@ export default defineConfig({
|
|
|
35
35
|
});
|
|
36
36
|
```
|
|
37
37
|
|
|
38
|
-
更多用法可参考 [Rsbuild - tools.swc](https://rsbuild.
|
|
38
|
+
更多用法可参考 [Rsbuild - tools.swc](https://v2.rsbuild.dev/zh/config/tools/swc)。
|
|
39
39
|
|
|
40
40
|
### 注册 SWC 插件
|
|
41
41
|
|
|
@@ -66,4 +66,4 @@ export default {
|
|
|
66
66
|
|
|
67
67
|
如果你遇到了以上问题,通常可行的解决方法是将 Modern.js 和 SWC 插件都升级到最新版本。
|
|
68
68
|
|
|
69
|
-
详情可参考 [Rsbuild - SWC 插件版本](https://rsbuild.
|
|
69
|
+
详情可参考 [Rsbuild - SWC 插件版本](https://v2.rsbuild.dev/zh/guide/configuration/swc#swc-%E6%8F%92%E4%BB%B6%E7%89%88%E6%9C%AC)。
|
|
@@ -55,7 +55,7 @@ export default {
|
|
|
55
55
|
大多数场景下,推荐优先使用 `.browserslistrc` 文件,而不是使用 `overrideBrowserslist` 配置。因为 `.browserslistrc` 文件是官方定义的配置文件,通用性更强,可以被社区中的其他库识别。
|
|
56
56
|
|
|
57
57
|
:::tip
|
|
58
|
-
请查看 [Rsbuild - 设置浏览器范围](https://rsbuild.
|
|
58
|
+
请查看 [Rsbuild - 设置浏览器范围](https://v2.rsbuild.dev/zh/guide/advanced/browserslist) 来了解更多内容。
|
|
59
59
|
:::
|
|
60
60
|
|
|
61
61
|
import UpgradeBrowserslist from '@site-docs/components/upgrade-browserslist';
|
|
@@ -98,4 +98,4 @@ export default {
|
|
|
98
98
|
|
|
99
99
|
良好的拆包策略对于提升应用的加载性能是十分重要的,可以充分利用浏览器的缓存机制,减少请求数量,加快页面加载速度。
|
|
100
100
|
|
|
101
|
-
在 Modern.js 中内置了[多种拆包策略](https://rsbuild.
|
|
101
|
+
在 Modern.js 中内置了[多种拆包策略](https://v2.rsbuild.dev/zh/guide/optimization/code-splitting),可以满足大部分应用的需求,你也可以根据自己的业务场景,自定义拆包配置。
|
|
@@ -12,20 +12,20 @@ Modern.js 内置了社区流行的 CSS 预处理器,包括 Less 和 Sass。
|
|
|
12
12
|
|
|
13
13
|
默认情况下,你不需要对 Less 和 Sass 进行任何配置。如果你有自定义 loader 配置的需求,可以通过配置 [tools.less](/configure/app/tools/less)、[tools.sass](/configure/app/tools/sass) 来进行设置。
|
|
14
14
|
|
|
15
|
-
你也可以在 Modern.js 中使用 Stylus,只需要安装 Rsbuild 提供的 Stylus 插件即可,使用方式请参考 [Stylus 插件](https://rsbuild.
|
|
15
|
+
你也可以在 Modern.js 中使用 Stylus,只需要安装 Rsbuild 提供的 Stylus 插件即可,使用方式请参考 [Stylus 插件](https://v2.rsbuild.dev/zh/plugins/list/plugin-stylus)。
|
|
16
16
|
|
|
17
17
|
## 使用 PostCSS
|
|
18
18
|
|
|
19
19
|
Modern.js 内置了 [PostCSS](https://postcss.org/) 来转换 CSS 代码。
|
|
20
20
|
|
|
21
|
-
请阅读 [Rsbuild - 使用 PostCSS](https://rsbuild.
|
|
21
|
+
请阅读 [Rsbuild - 使用 PostCSS](https://v2.rsbuild.dev/zh/guide/styling/css-usage) 了解更多用法。
|
|
22
22
|
|
|
23
23
|
## 使用 Lightning CSS
|
|
24
24
|
|
|
25
25
|
Modern.js 支持使用 [Lightning CSS](https://lightningcss.dev/) 来转换 CSS 代码,可以通过配置 [tools.lightningcssLoader](/configure/app/tools/lightningcss-loader) 来开启此功能。
|
|
26
26
|
|
|
27
|
-
请阅读 [Rsbuild - 使用 Lightning CSS](https://rsbuild.
|
|
27
|
+
请阅读 [Rsbuild - 使用 Lightning CSS](https://v2.rsbuild.dev/zh/guide/styling/css-usage#lightning-css) 了解更多用法。
|
|
28
28
|
|
|
29
29
|
## 使用 Uno CSS
|
|
30
30
|
|
|
31
|
-
请阅读 [Rsbuild - 使用 UnoCSS](https://rsbuild.
|
|
31
|
+
请阅读 [Rsbuild - 使用 UnoCSS](https://v2.rsbuild.dev/zh/guide/styling/unocss) 了解更多用法。
|
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
|
|
7
7
|
在 Modern.js 中使用 [Tailwind CSS](https://tailwindcss.com/),你只需要按照 Rsbuild 操作步骤进行配置,Rsbuild 支持 Tailwind CSS v3 和 v4 版本:
|
|
8
8
|
|
|
9
|
-
- [Tailwind CSS v3](https://rsbuild.
|
|
10
|
-
- [Tailwind CSS v4](https://rsbuild.
|
|
9
|
+
- [Tailwind CSS v3](https://v2.rsbuild.dev/zh/guide/styling/tailwindcss-v3#tailwind-css-v3)
|
|
10
|
+
- [Tailwind CSS v4](https://v2.rsbuild.dev/zh/guide/styling/tailwindcss)
|
|
11
11
|
|
|
12
12
|
## Tailwind CSS 自动补全
|
|
13
13
|
|
|
@@ -21,12 +21,12 @@ React Server Components (RSC) 是一种新的组件类型,允许在服务端
|
|
|
21
21
|
|
|
22
22
|
## 快速开始
|
|
23
23
|
|
|
24
|
-
1. **确保 React 和 React DOM 升级到 19 版本**(建议 19.2.
|
|
24
|
+
1. **确保 React 和 React DOM 升级到 19 版本**(建议 19.2.4 以上版本)
|
|
25
25
|
|
|
26
|
-
2. **安装 `react-server-dom-
|
|
26
|
+
2. **安装 `react-server-dom-rspack@0.0.1-beta.0` 依赖**
|
|
27
27
|
|
|
28
28
|
```bash
|
|
29
|
-
npm install react-server-dom-
|
|
29
|
+
npm install react-server-dom-rspack@0.0.1-beta.0
|
|
30
30
|
```
|
|
31
31
|
|
|
32
32
|
:::warning 注意事项
|
|
@@ -49,8 +49,8 @@ export default defineConfig({
|
|
|
49
49
|
```
|
|
50
50
|
|
|
51
51
|
:::info 旧 CSR 项目迁移
|
|
52
|
-
如果是 CSR 项目,且项目中使用了 Modern.js 的
|
|
53
|
-
|
|
52
|
+
如果是 CSR 项目,且项目中使用了 Modern.js 的 Data Loader,开启 RSC 后,Data Loader 默认会在服务端执行。
|
|
53
|
+
详细的迁移指南请参考 [CSR 项目迁移到 RSC](#csr-项目迁移到-rsc)。
|
|
54
54
|
|
|
55
55
|
:::
|
|
56
56
|
|
|
@@ -233,11 +233,225 @@ export default function DashboardPage() {
|
|
|
233
233
|
|
|
234
234
|
例如,假设 `layout.tsx` 是一个 Client Component(需要客户端交互),你仍然可以将 `page.tsx` 设置为 Server Component(用于数据获取和渲染)。这种方式提供了极大的灵活性,也使非 RSC 项目可以渐进式地迁移至 RSC 项目。
|
|
235
235
|
|
|
236
|
-
##
|
|
236
|
+
## Server Component 与 Data Loader
|
|
237
|
+
|
|
238
|
+
在 RSC 项目中,你有两种方式获取数据:在 Server Component 中直接获取,或使用 [Data Loader](/guides/basic-features/data/data-fetch)。两种方式各有优势,可以根据场景灵活选择。
|
|
239
|
+
|
|
240
|
+
### 两种数据获取方式对比
|
|
241
|
+
|
|
242
|
+
| 特性 | Server Component 中获取 | Data Loader 中获取 |
|
|
243
|
+
| --- | --- | --- |
|
|
244
|
+
| 避免请求瀑布 | 需要手动优化 | ✅ 自动并行执行 |
|
|
245
|
+
| 组件内聚性 | ✅ 数据和 UI 在同一处 | 数据逻辑分离到单独文件 |
|
|
246
|
+
| 维护性 | ✅ 更易理解和维护 | 需要维护额外文件 |
|
|
247
|
+
| 类型安全 | ✅ 天然类型推断 | 需要手动管理类型 |
|
|
248
|
+
|
|
249
|
+
**一般情况下,我们推荐在 Server Component 中获取数据**,因为在服务端瀑布请求对性能的影响较小,这种方式使组件更加内聚,数据获取逻辑和 UI 渲染在同一处,更易于理解和维护。但如果你的页面有多个独立的数据源,且希望完全避免请求瀑布问题,Data Loader 的并行执行特性会更有优势。
|
|
250
|
+
|
|
251
|
+
### RSC 项目中 Data Loader 的执行环境
|
|
252
|
+
|
|
253
|
+
在 RSC 项目中,Data Loader 的执行环境与文件命名有关:
|
|
254
|
+
|
|
255
|
+
- **`*.data.ts`**:只在**服务端**执行,Client Component 和 Server Component 都可以消费其数据
|
|
256
|
+
- **`*.data.client.ts`**:只在**客户端**执行
|
|
257
|
+
|
|
258
|
+
```bash
|
|
259
|
+
.
|
|
260
|
+
└── routes
|
|
261
|
+
└── user
|
|
262
|
+
├── page.tsx # 路由组件(可以是 Server 或 Client Component)
|
|
263
|
+
├── page.data.ts # 服务端执行,数据可被任何组件消费
|
|
264
|
+
└── page.data.client.ts # 客户端执行
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
在 Modern.js RSC 项目中,Server Component 可以通过 `loaderData` prop 接收 Data Loader 返回的数据:
|
|
268
|
+
|
|
269
|
+
```tsx title="routes/user/page.tsx"
|
|
270
|
+
// Server Component 通过 props 接收 loaderData
|
|
271
|
+
export default function UserPage({ loaderData }: { loaderData: { name: string } }) {
|
|
272
|
+
return <div>Welcome, {loaderData.name}</div>;
|
|
273
|
+
}
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
```tsx title="routes/user/page.tsx"
|
|
277
|
+
'use client';
|
|
278
|
+
// Client Component 同样可以通过 props 接收 loaderData
|
|
279
|
+
export default function UserPage({ loaderData }: { loaderData: { name: string } }) {
|
|
280
|
+
return <div>Welcome, {loaderData.name}</div>;
|
|
281
|
+
}
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
### Data Loader 返回 Server Component
|
|
285
|
+
|
|
286
|
+
在 Modern.js RSC 项目中,Data Loader 有一个强大的特性:**可以返回 Server Component**。这对于渐进式迁移非常有帮助,允许你在 Client Component 中渲染服务端生成的内容。
|
|
287
|
+
|
|
288
|
+
```tsx title="routes/user/layout.data.tsx"
|
|
289
|
+
// Server Component 定义在 data loader 文件中
|
|
290
|
+
function UserProfile() {
|
|
291
|
+
return <div>User Profile (Server Rendered)</div>;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export const loader = async () => {
|
|
295
|
+
const userData = await fetchUserData();
|
|
296
|
+
|
|
297
|
+
return {
|
|
298
|
+
user: userData,
|
|
299
|
+
// 返回一个 Server Component 作为数据的一部分
|
|
300
|
+
ProfileComponent: <UserProfile />,
|
|
301
|
+
};
|
|
302
|
+
};
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
```tsx title="routes/user/layout.tsx"
|
|
306
|
+
'use client';
|
|
307
|
+
import { Outlet } from '@modern-js/runtime/router';
|
|
308
|
+
|
|
309
|
+
export default function UserLayout({
|
|
310
|
+
loaderData,
|
|
311
|
+
}: {
|
|
312
|
+
loaderData: { user: any; ProfileComponent: React.ReactNode };
|
|
313
|
+
}) {
|
|
314
|
+
const { user, ProfileComponent } = loaderData;
|
|
315
|
+
|
|
316
|
+
return (
|
|
317
|
+
<div>
|
|
318
|
+
{/* 在 Client Component 中直接渲染 Server Component */}
|
|
319
|
+
{ProfileComponent}
|
|
320
|
+
<div>User: {user.name}</div>
|
|
321
|
+
<Outlet />
|
|
322
|
+
</div>
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
## Server Component API
|
|
328
|
+
|
|
329
|
+
在 RSC 场景下,Modern.js 提供了一系列服务端 API,允许你在 Server Component 中直接操作 HTTP 请求和响应。这些 API 只能在 Server Component 中使用。
|
|
330
|
+
|
|
331
|
+
### getRequest
|
|
332
|
+
|
|
333
|
+
获取当前请求的 `Request` 对象,用于访问请求信息(如 URL、查询参数、请求头等)。
|
|
334
|
+
|
|
335
|
+
```tsx title="routes/page.tsx"
|
|
336
|
+
import { getRequest } from '@modern-js/runtime';
|
|
337
|
+
|
|
338
|
+
export default async function Page() {
|
|
339
|
+
const request = getRequest();
|
|
340
|
+
const url = new URL(request.url);
|
|
341
|
+
const searchParams = url.searchParams;
|
|
342
|
+
const query = searchParams.get('q');
|
|
343
|
+
|
|
344
|
+
return <div>Search query: {query}</div>;
|
|
345
|
+
}
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
### setHeaders
|
|
349
|
+
|
|
350
|
+
设置 HTTP 响应头。可以用于设置自定义响应头,如缓存控制、CORS 等。
|
|
351
|
+
|
|
352
|
+
```tsx title="routes/page.tsx"
|
|
353
|
+
import { setHeaders } from '@modern-js/runtime';
|
|
354
|
+
|
|
355
|
+
export default async function Page() {
|
|
356
|
+
setHeaders({
|
|
357
|
+
'X-Custom-Header': 'custom-value',
|
|
358
|
+
'Cache-Control': 'public, max-age=3600',
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
return <div>Page content</div>;
|
|
362
|
+
}
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
### setStatus
|
|
366
|
+
|
|
367
|
+
设置 HTTP 响应状态码。常用于返回错误状态码(如 404、500)或成功状态码(如 201)。
|
|
368
|
+
|
|
369
|
+
```tsx title="routes/page.tsx"
|
|
370
|
+
import { setStatus } from '@modern-js/runtime';
|
|
371
|
+
|
|
372
|
+
export default async function Page() {
|
|
373
|
+
setStatus(201);
|
|
374
|
+
return <div>Resource created</div>;
|
|
375
|
+
}
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
### redirect
|
|
379
|
+
|
|
380
|
+
执行 HTTP 重定向。可以指定重定向的状态码(默认为 307)和额外的响应头。
|
|
381
|
+
|
|
382
|
+
```tsx title="routes/page.tsx"
|
|
383
|
+
import { redirect } from '@modern-js/runtime';
|
|
384
|
+
|
|
385
|
+
export default async function Page() {
|
|
386
|
+
// 简单重定向,使用默认状态码 307
|
|
387
|
+
redirect('/new-path');
|
|
388
|
+
|
|
389
|
+
// 指定状态码
|
|
390
|
+
redirect('/new-path', 301);
|
|
391
|
+
|
|
392
|
+
// 指定状态码和响应头
|
|
393
|
+
redirect('/new-path', {
|
|
394
|
+
status: 301,
|
|
395
|
+
headers: {
|
|
396
|
+
'X-Redirect-From': '/old-path',
|
|
397
|
+
},
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
return null; // redirect 后不会执行到这里
|
|
401
|
+
}
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
:::warning 注意事项
|
|
405
|
+
|
|
406
|
+
1. 这些 API 只能在 **Server Component** 中使用,在 Client Component 中调用不会产生任何效果
|
|
407
|
+
2. `redirect` 会立即终止当前组件的渲染,后续代码不会执行
|
|
408
|
+
3. 这些 API 应该在组件的顶层调用,而不是在条件分支或循环中调用,以确保行为可预测
|
|
409
|
+
|
|
410
|
+
:::
|
|
411
|
+
|
|
412
|
+
## CSR 项目迁移指南
|
|
237
413
|
|
|
238
|
-
|
|
414
|
+
Modern.js 的 RSC 能力,不仅支持 SSR 项目也支持 CSR 项目。对于现有的 CSR 项目,如果希望渐进式迁移到 RSC,推荐按照以下步骤:
|
|
239
415
|
|
|
240
|
-
|
|
416
|
+
1. **开启 RSC 配置**
|
|
417
|
+
|
|
418
|
+
```ts title="modern.config.ts"
|
|
419
|
+
import { defineConfig } from '@modern-js/app-tools';
|
|
420
|
+
|
|
421
|
+
export default defineConfig({
|
|
422
|
+
server: {
|
|
423
|
+
rsc: true,
|
|
424
|
+
},
|
|
425
|
+
});
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
2. **将所有路由组件标记为 `'use client'`**
|
|
429
|
+
|
|
430
|
+
这确保现有组件的行为不变,它们仍然作为 Client Component 运行。
|
|
431
|
+
|
|
432
|
+
```tsx title="routes/page.tsx"
|
|
433
|
+
'use client';
|
|
434
|
+
|
|
435
|
+
export default function Page() {
|
|
436
|
+
// 现有的客户端逻辑保持不变
|
|
437
|
+
}
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
3. **将所有 `*.data.ts` 重命名为 `*.data.client.ts`**
|
|
441
|
+
|
|
442
|
+
由于 RSC 项目中 `*.data.ts` 默认在服务端执行,为了保持与 CSR 项目一致的行为(Data Loader 在客户端执行),需要将文件重命名。
|
|
443
|
+
|
|
444
|
+
```bash
|
|
445
|
+
# 重命名前
|
|
446
|
+
routes/user/page.data.ts
|
|
447
|
+
|
|
448
|
+
# 重命名后
|
|
449
|
+
routes/user/page.data.client.ts
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
完成以上步骤后,你可以逐步将组件迁移为 Server Component,享受 RSC 带来的性能优势。
|
|
453
|
+
|
|
454
|
+
## 注意事项
|
|
241
455
|
|
|
242
456
|
### 使用 Streaming SSR 的项目
|
|
243
457
|
|
|
@@ -4,7 +4,7 @@ sidebar_position: 2
|
|
|
4
4
|
|
|
5
5
|
# 构建工具
|
|
6
6
|
|
|
7
|
-
Modern.js 构建是基于 [Rsbuild](https://rsbuild.
|
|
7
|
+
Modern.js 构建是基于 [Rsbuild](https://v2.rsbuild.dev/) 实现的,使用 Rspack 作为打包工具。
|
|
8
8
|
|
|
9
9
|
:::tip 什么是 Rsbuild
|
|
10
10
|
Rsbuild 是基于 Rspack 的构建工具,是一个增强版的 Rspack CLI,更易用、开箱即用。
|
|
@@ -40,7 +40,7 @@ flowchart TD
|
|
|
40
40
|
|
|
41
41
|
## 构建文档
|
|
42
42
|
|
|
43
|
-
Rsbuild 的文档地址为:https://rsbuild.
|
|
43
|
+
Rsbuild 的文档地址为:https://v2.rsbuild.dev/
|
|
44
44
|
|
|
45
45
|
在该文档中,你可以了解到 Rsbuild 的详细介绍,同时也可以找到各个构建能力的完整使用指南。
|
|
46
46
|
|
|
@@ -57,4 +57,4 @@ Rsbuild 的文档地址为:https://rsbuild.rs/
|
|
|
57
57
|
|
|
58
58
|
Rsbuild 提供了丰富的构建能力,包括 JavaScript 编译、CSS 编译、静态资源处理、代码热更新、代码压缩、TS 类型检查等几十种能力。
|
|
59
59
|
|
|
60
|
-
我们推荐你阅读 [「Rsbuild - 功能导航」](https://rsbuild.
|
|
60
|
+
我们推荐你阅读 [「Rsbuild - 功能导航」](https://v2.rsbuild.dev/zh/guide/start/features) 来了解 Rsbuild 提供的所有构建能力。
|
|
@@ -12,7 +12,7 @@ Modern.js 框架默认集成了一些社区中流行的库和开发工具。
|
|
|
12
12
|
|
|
13
13
|
Modern.js 使用 [React 19](https://react.dev/) 来构建用户界面,同时也兼容 React 18。
|
|
14
14
|
|
|
15
|
-
Modern.js 底层的 Rsbuild 支持构建 Vue 应用,如果你需要使用 Vue,可以参考 [Rsbuild - Vue](https://rsbuild.
|
|
15
|
+
Modern.js 底层的 Rsbuild 支持构建 Vue 应用,如果你需要使用 Vue,可以参考 [Rsbuild - Vue](https://v2.rsbuild.dev/zh/guide/framework/vue)。
|
|
16
16
|
|
|
17
17
|
## 路由
|
|
18
18
|
|
|
@@ -34,7 +34,7 @@ Modern.js 使用 [Rspack](https://rspack.rs/) 来打包你的 Web 应用。
|
|
|
34
34
|
|
|
35
35
|
Modern.js 使用 [SWC](https://swc.rs/) 作为 JS 转译工具,将 TypeScript 或 JSX 转义为可以在浏览器上运行的 JavaScript 代码,并进行语法降级。
|
|
36
36
|
|
|
37
|
-
在启用 Rspack 构建时,`babel-loader` 默认不会被启用。如需添加 [Babel](https://babeljs.io/) 插件,可通过 [`babel plugin`](https://rsbuild.
|
|
37
|
+
在启用 Rspack 构建时,`babel-loader` 默认不会被启用。如需添加 [Babel](https://babeljs.io/) 插件,可通过 [`babel plugin`](https://v2.rsbuild.dev/plugins/list/plugin-babel#babel-plugin) 配置,此时会产生额外的编译开销,在一定程度上拖慢 Rspack 构建速度。
|
|
38
38
|
|
|
39
39
|
## 压缩工具
|
|
40
40
|
|
|
@@ -53,7 +53,7 @@ Modern.js 支持 [启用 Tailwind CSS](/guides/basic-features/css/tailwindcss)
|
|
|
53
53
|
Modern.js 支持 [Sass](https://sass-lang.com/)、[Less](https://lesscss.org/) 和 [Stylus](https://stylus-lang.com/) 三种 CSS 预处理器:
|
|
54
54
|
|
|
55
55
|
- 默认支持 Sass 和 Less,开箱即用。
|
|
56
|
-
- 可选支持 Stylus,请参考 [Stylus 插件](https://rsbuild.
|
|
56
|
+
- 可选支持 Stylus,请参考 [Stylus 插件](https://v2.rsbuild.dev/zh/plugins/list/plugin-stylus) 来使用。
|
|
57
57
|
|
|
58
58
|
## CSS Modules
|
|
59
59
|
|
|
@@ -18,7 +18,7 @@ Modern.js 提供了运行时 API,支持快速从应用中导出应用级别的
|
|
|
18
18
|
import '@modern-js/runtime/registry/index'; // 这一行必须引入,它会默认导入微前端运行时依赖
|
|
19
19
|
import { render } from '@modern-js/runtime/browser';
|
|
20
20
|
import { createRoot } from '@modern-js/runtime/react';
|
|
21
|
-
import { createBridgeComponent } from '@module-federation/modern-js/react-v19';
|
|
21
|
+
import { createBridgeComponent } from '@module-federation/modern-js-v3/react-v19';
|
|
22
22
|
|
|
23
23
|
const ModernRoot = createRoot();
|
|
24
24
|
export const provider = createBridgeComponent({
|
|
@@ -33,8 +33,8 @@ export default provider;
|
|
|
33
33
|
:::note
|
|
34
34
|
请根据你项目中使用的 React 版本来选择对应的导入路径:
|
|
35
35
|
|
|
36
|
-
- **React 19**:使用 `@module-federation/modern-js/react-v19`
|
|
37
|
-
- **React 18**:使用 `@module-federation/modern-js/react-v18`
|
|
36
|
+
- **React 19**:使用 `@module-federation/modern-js-v3/react-v19`
|
|
37
|
+
- **React 18**:使用 `@module-federation/modern-js-v3/react-v18`
|
|
38
38
|
|
|
39
39
|
你可以通过查看项目的 `package.json` 文件中的 `react` 依赖版本来确定应该使用哪个导入路径。
|
|
40
40
|
:::
|
|
@@ -44,7 +44,7 @@ export default provider;
|
|
|
44
44
|
接下来,我们配置 `module-federation.config.ts`,将导出修改为 `src/export-App.tsx`:
|
|
45
45
|
|
|
46
46
|
```ts title="module-federation.config.ts"
|
|
47
|
-
import { createModuleFederationConfig } from '@module-federation/modern-js';
|
|
47
|
+
import { createModuleFederationConfig } from '@module-federation/modern-js-v3';
|
|
48
48
|
|
|
49
49
|
export default createModuleFederationConfig({
|
|
50
50
|
name: 'remote',
|
|
@@ -77,8 +77,8 @@ export default createModuleFederationConfig({
|
|
|
77
77
|
:::
|
|
78
78
|
|
|
79
79
|
```tsx title="src/routes/remote/$.tsx"
|
|
80
|
-
import { createRemoteAppComponent } from '@module-federation/modern-js/react';
|
|
81
|
-
import { loadRemote } from '@module-federation/modern-js/runtime';
|
|
80
|
+
import { createRemoteAppComponent } from '@module-federation/modern-js-v3/react';
|
|
81
|
+
import { loadRemote } from '@module-federation/modern-js-v3/runtime';
|
|
82
82
|
|
|
83
83
|
const ErrorBoundary = (info?: { error: { message: string } }) => {
|
|
84
84
|
return (
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
例如生产者部署在 `https://my-remote-module` 这个域名下,开发者需要修改消费者的配置文件:
|
|
15
15
|
|
|
16
16
|
```ts title="module-federation.config.ts"
|
|
17
|
-
import { createModuleFederationConfig } from '@module-federation/modern-js';
|
|
17
|
+
import { createModuleFederationConfig } from '@module-federation/modern-js-v3';
|
|
18
18
|
|
|
19
19
|
export default createModuleFederationConfig({
|
|
20
20
|
name: 'host',
|
|
@@ -76,7 +76,7 @@ export default defineConfig({
|
|
|
76
76
|
此时,消费者在配置远程模块时,需要配置以下地址:
|
|
77
77
|
|
|
78
78
|
```ts title="module-federation.config.ts"
|
|
79
|
-
import { createModuleFederationConfig } from '@module-federation/modern-js';
|
|
79
|
+
import { createModuleFederationConfig } from '@module-federation/modern-js-v3';
|
|
80
80
|
|
|
81
81
|
export default createModuleFederationConfig({
|
|
82
82
|
name: 'host',
|