@modern-js/main-doc 2.66.0 → 2.66.1-alpha.0

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.
Files changed (51) hide show
  1. package/docs/en/apis/app/hooks/src/app.mdx +20 -34
  2. package/docs/en/apis/app/hooks/src/modern.runtime.mdx +9 -0
  3. package/docs/en/apis/app/runtime/app/define-config.mdx +6 -0
  4. package/docs/en/components/enable-micro-frontend.mdx +20 -3
  5. package/docs/en/components/micro-runtime-config.mdx +12 -13
  6. package/docs/en/components/reduck-notify.mdx +27 -0
  7. package/docs/en/configure/app/runtime/0-intro.mdx +63 -91
  8. package/docs/en/configure/app/usage.mdx +93 -69
  9. package/docs/en/guides/basic-features/render/_meta.json +1 -1
  10. package/docs/en/guides/basic-features/render/before-render.mdx +115 -0
  11. package/docs/en/guides/basic-features/routes.mdx +0 -95
  12. package/docs/en/guides/topic-detail/micro-frontend/c02-development.mdx +3 -5
  13. package/docs/en/guides/topic-detail/micro-frontend/c03-main-app.mdx +4 -2
  14. package/docs/en/guides/topic-detail/model/auto-actions.mdx +0 -4
  15. package/docs/en/guides/topic-detail/model/computed-state.mdx +0 -5
  16. package/docs/en/guides/topic-detail/model/define-model.mdx +0 -4
  17. package/docs/en/guides/topic-detail/model/faq.mdx +0 -5
  18. package/docs/en/guides/topic-detail/model/manage-effects.mdx +0 -5
  19. package/docs/en/guides/topic-detail/model/model-communicate.mdx +0 -5
  20. package/docs/en/guides/topic-detail/model/performance.mdx +0 -4
  21. package/docs/en/guides/topic-detail/model/quick-start.mdx +5 -7
  22. package/docs/en/guides/topic-detail/model/redux-integration.mdx +0 -4
  23. package/docs/en/guides/topic-detail/model/typescript-best-practice.mdx +0 -4
  24. package/docs/en/guides/topic-detail/model/use-model.mdx +0 -5
  25. package/docs/en/guides/topic-detail/model/use-out-of-modernjs.mdx +0 -4
  26. package/docs/zh/apis/app/hooks/src/app.mdx +18 -26
  27. package/docs/zh/apis/app/hooks/src/modern.runtime.mdx +9 -0
  28. package/docs/zh/apis/app/runtime/app/define-config.mdx +5 -0
  29. package/docs/zh/components/enable-micro-frontend.mdx +19 -12
  30. package/docs/zh/components/micro-runtime-config.mdx +3 -3
  31. package/docs/zh/components/reduck-notify.mdx +27 -0
  32. package/docs/zh/configure/app/runtime/0-intro.mdx +67 -86
  33. package/docs/zh/configure/app/usage.mdx +44 -21
  34. package/docs/zh/guides/basic-features/render/_meta.json +1 -1
  35. package/docs/zh/guides/basic-features/render/before-render.mdx +115 -0
  36. package/docs/zh/guides/basic-features/routes.mdx +0 -95
  37. package/docs/zh/guides/topic-detail/micro-frontend/c02-development.mdx +3 -5
  38. package/docs/zh/guides/topic-detail/micro-frontend/c03-main-app.mdx +4 -2
  39. package/docs/zh/guides/topic-detail/model/auto-actions.mdx +0 -4
  40. package/docs/zh/guides/topic-detail/model/computed-state.mdx +0 -4
  41. package/docs/zh/guides/topic-detail/model/define-model.mdx +1 -4
  42. package/docs/zh/guides/topic-detail/model/faq.mdx +0 -5
  43. package/docs/zh/guides/topic-detail/model/manage-effects.mdx +0 -4
  44. package/docs/zh/guides/topic-detail/model/model-communicate.mdx +1 -4
  45. package/docs/zh/guides/topic-detail/model/performance.mdx +0 -4
  46. package/docs/zh/guides/topic-detail/model/quick-start.mdx +7 -8
  47. package/docs/zh/guides/topic-detail/model/redux-integration.mdx +0 -4
  48. package/docs/zh/guides/topic-detail/model/typescript-best-practice.mdx +0 -4
  49. package/docs/zh/guides/topic-detail/model/use-model.mdx +0 -5
  50. package/docs/zh/guides/topic-detail/model/use-out-of-modernjs.mdx +0 -4
  51. package/package.json +1 -1
@@ -0,0 +1,115 @@
1
+ # 渲染预处理 (Render Preprocessing)
2
+
3
+ 在某些场景下,应用需要在渲染前执行预处理操作。Modern.js 推荐使用 **[Runtime 插件 (Runtime Plugin)](/plugin/introduction.html#runtime-插件)** 来实现这类逻辑。
4
+
5
+ ## 定义 Runtime 插件
6
+
7
+ ```ts
8
+ import type { RuntimePluginFuture } from '@modern-js/runtime';
9
+
10
+ const myRuntimePlugin = (): RuntimePluginFuture => ({
11
+ name: 'my-runtime-plugin',
12
+ setup: (api) => {
13
+ api.onBeforeRender((context) => {
14
+ // 在渲染前执行的逻辑
15
+ console.log('Before rendering:', context);
16
+ });
17
+ },
18
+ });
19
+
20
+ export default myRuntimePlugin;
21
+ ```
22
+
23
+ ## 注册插件
24
+
25
+ 在项目 `src/modern.runtime.ts` 文件中注册插件:
26
+
27
+ ```ts
28
+ import { defineRuntimeConfig } from '@modern-js/runtime';
29
+ import myRuntimePlugin from './plugins/myRuntimePlugin';
30
+
31
+ export default defineRuntimeConfig({
32
+ plugins: [myRuntimePlugin()],
33
+ });
34
+ ```
35
+
36
+ ## 应用场景 -- 全局数据注入
37
+
38
+ 通过 `onBeforeRender` 钩子的 `context` 参数,可以向应用注入全局数据。应用的组件可以通过 `useRuntimeContext` Hook 访问这些数据。
39
+
40
+ :::info
41
+
42
+ 此功能在以下场景中特别有用:
43
+ * 需要页面级前置数据的应用
44
+ * 自定义数据注入流程
45
+ * 框架迁移场景(例如从 Next.js 迁移)
46
+
47
+ :::
48
+
49
+ **定义数据注入插件**
50
+
51
+ ```ts
52
+ import type { RuntimePluginFuture } from '@modern-js/runtime';
53
+
54
+ const dataInjectionPlugin = (): RuntimePluginFuture => ({
55
+ name: 'data-injection-plugin',
56
+ setup: api => {
57
+ api.onBeforeRender(context => {
58
+ // 向 context 中注入数据
59
+ context.message = 'Hello World';
60
+ });
61
+ },
62
+ });
63
+
64
+ export default dataInjectionPlugin;
65
+ ```
66
+
67
+ **在组件中使用注入的数据**
68
+
69
+ ```tsx
70
+ import { useRuntimeContext } from '@modern-js/runtime';
71
+
72
+ export default function MyComponent() {
73
+ const context = useRuntimeContext();
74
+ const { message } = context;
75
+
76
+ return <div>{message}</div>;
77
+ }
78
+ ```
79
+
80
+ **结合 SSR 使用**
81
+
82
+ 在 SSR 场景下,浏览器端可以获取服务端渲染期间通过 `onBeforeRender` 注入的数据。开发者可以根据需求决定是否在浏览器端重新获取数据来覆盖服务端数据。
83
+
84
+ ```ts
85
+ import type { RuntimePluginFuture } from '@modern-js/runtime';
86
+
87
+ const dataInjectionPlugin = (): RuntimePluginFuture => ({
88
+ name: 'data-injection-plugin',
89
+ setup: api => {
90
+ api.onBeforeRender(context => {
91
+ if (process.env.MODERN_TARGET === 'node') {
92
+ // 服务端渲染时设置数据
93
+ context.message = 'Hello World By Server';
94
+ } else {
95
+ // 客户端渲染时检查数据
96
+ if (!context.message) {
97
+ // 如果没有获取到服务端数据,则设置客户端数据
98
+ context.message = 'Hello World By Client';
99
+ }
100
+ }
101
+ });
102
+ },
103
+ });
104
+
105
+ export default dataInjectionPlugin;
106
+ ```
107
+
108
+ ## 兼容性说明
109
+
110
+ 在 Modern.js 的早期版本中,支持通过 `routes/layout.tsx` 中的 `init` 钩子以及 `App.init` 方法来添加渲染预处理逻辑。这些方式目前仍然**被支持**,但我们**强烈推荐**使用 Runtime 插件实现。
111
+
112
+ :::warning
113
+
114
+ 未来版本中,`routes/layout.tsx` 的 `init` 钩子和 `App.init` 方法将逐步**废弃**。建议尽早迁移到 Runtime 插件方案。
115
+ :::
@@ -483,101 +483,6 @@ Modern.js 支持通过 `loading.tsx` 文件来解决这个问题,`routes/` 下
483
483
  :::
484
484
 
485
485
 
486
- ## 运行时配置
487
-
488
- {/* Todo 移到动运行时配置章节 */}
489
-
490
- 在根 `<Layout>` 组件中(`routes/layout.ts`),可以动态地定义运行时配置:
491
-
492
- ```tsx title="src/routes/layout.tsx"
493
- // 定义运行时配置
494
- import type { AppConfig } from '@modern-js/runtime';
495
-
496
- export const config = (): AppConfig => {
497
- return {
498
- router: {
499
- createRoutes() {
500
- return [
501
- {
502
- path: 'modern',
503
- element: <div>modern</div>,
504
- },
505
- ];
506
- },
507
- },
508
- };
509
- };
510
- ```
511
-
512
- ## 渲染前的钩子
513
-
514
- {/* Todo 移到动运行时配置章节 */}
515
-
516
- 在有些场景下,需要在应用渲染前做一些操作,可以在 `routes/layout.tsx` 中定义 `init` 钩子,`init` 在客户端和服务端均会执行,基本使用示例如下:
517
-
518
- ```ts title="src/routes/layout.tsx"
519
- import type { RuntimeContext } from '@modern-js/runtime';
520
-
521
- export const init = (context: RuntimeContext) => {
522
- // do something
523
- };
524
- ```
525
-
526
- 通过 `init` 钩子可以挂载一些全局的数据,在应用的其他地方可以访问 `runtimeContext` 变量:
527
-
528
- :::note
529
- 该功能在应用需要页面前置的数据、自定义数据注入或是框架迁移(如 Next.js)时会非常有用。
530
-
531
- :::
532
-
533
- ```ts title="src/routes/layout.tsx"
534
- import { RuntimeContext } from '@modern-js/runtime';
535
-
536
- type InitialData = {
537
- message: string;
538
- }
539
-
540
- export const init = (context: RuntimeContext): InitialData => {
541
- return {
542
- message: 'Hello World',
543
- };
544
- };
545
- ```
546
-
547
- ```tsx title="src/routes/page.tsx"
548
- import { useRuntimeContext } from '@modern-js/runtime';
549
-
550
- export default () => {
551
- const context = useRuntimeContext();
552
- const { message } = context.initialData as InitialData;
553
-
554
- return <div>{message}</div>;
555
- };
556
- ```
557
-
558
- 配合 SSR 功能时,浏览器端可以获取到 SSR 时 `init` 返回的数据,开发者可以自行判断是否要在浏览器端重新获取数据来覆盖 SSR 数据,例如:
559
-
560
- ```tsx title="src/routes/layout.tsx"
561
- import type { RuntimeContext } from '@modern-js/runtime';
562
-
563
- export const init = (runtimeContext: RuntimeContext) => {
564
- if (process.env.MODERN_TARGET === 'node') {
565
- return {
566
- message: 'Hello World By Server',
567
- };
568
- } else {
569
- const { context } = runtimeContext;
570
- const data = context.getInitData();
571
- // 如果没有获取到期望的数据
572
- if (!data.message) {
573
- return {
574
- message: 'Hello World By Client',
575
- };
576
- }
577
- }
578
- };
579
- ```
580
-
581
486
  import Motivation from '@site-docs/components/convention-routing-motivation';
582
487
 
583
488
  <Motivation />
@@ -14,9 +14,9 @@ title: 体验微前端
14
14
 
15
15
  目前项目的路由模式分为以下三种
16
16
 
17
- - 约定式路由 (设置 `router: true` 并使用文件路由)
18
- - 自控式路由 (设置 `router: true` 并自己创建 `BrowserRouter` 等)
19
- - 其他 (设置 `router: false` 项目内自己安装和使用 `react-router-dom`)
17
+ - [约定式路由](/guides/concept/entries.html#约定式路由)
18
+ - [自控式路由](/guides/concept/entries.html#自控式路由)
19
+ - 其他 (项目自行安装和使用 `react-router-dom`)
20
20
 
21
21
  在本教程中我们会为主应用创建两个子应用 Table 和 Dashboard (Table 为约定式路由,Dashboard 为自控式路由)
22
22
 
@@ -206,7 +206,6 @@ export default defineConfig({
206
206
  },
207
207
  runtime: {
208
208
  router: true,
209
- state: true,
210
209
  },
211
210
  deploy: {
212
211
  microFrontend: true,
@@ -259,7 +258,6 @@ export default defineConfig({
259
258
  },
260
259
  runtime: {
261
260
  router: true,
262
- state: true,
263
261
  },
264
262
  deploy: {
265
263
  microFrontend: true,
@@ -35,8 +35,10 @@ import MicroRuntimeConfig from '@site-docs/components/micro-runtime-config';
35
35
 
36
36
  通过该函数可以拉取远程的子应用列表,并将其注册至运行时框架:
37
37
 
38
- ```ts title="App.tsx"
39
- defineConfig(App, {
38
+ ```ts title="src/modern.runtime.ts"
39
+ import { defineRuntimeConfig } from '@modern-js/runtime';
40
+
41
+ export default defineRuntimeConfig({
40
42
  masterApp: {
41
43
  manifest: {
42
44
  getAppList: async () => {
@@ -1,7 +1,3 @@
1
- ---
2
- sidebar_position: 6
3
- title: 自动生成 Actions
4
- ---
5
1
  # 自动生成 Actions
6
2
 
7
3
  在 [快速上手](/guides/topic-detail/model/quick-start) 中,我们实现最简单的计数器 Model 也需要 10 行代码。
@@ -1,7 +1,3 @@
1
- ---
2
- sidebar_position: 4
3
- title: 衍生状态
4
- ---
5
1
  # 衍生状态
6
2
 
7
3
  一些场景中,组件需要对 Model 中的 State 进行进一步计算,才能在组件中使用,这部分逻辑可以直接写在组件内部,也可以通过 Model 的衍生状态实现。
@@ -1,7 +1,3 @@
1
- ---
2
- sidebar_position: 2
3
- title: 创建 Model
4
- ---
5
1
  # 创建 Model
6
2
 
7
3
  上一节的计数器应用中,我们简单演示了如何创建一个 Model。本节我们将详细介绍 Model 的创建方法。
@@ -24,6 +20,7 @@ const fooModel = model('foo').define({
24
20
  ```
25
21
 
26
22
  :::info
23
+
27
24
  - Model 中的 Action 中不能包含有副作用的逻辑,如请求 HTTP 接口,访问 localStorage 等。
28
25
  - `setValue` 内部直接修改了入参 State,看起来是违反了纯函数的定义,实际上 Reduck 内部使用 [immer](https://github.com/immerjs/immer) 来修改不可变对象,保证了这种写法不会影响对象的不可变性,所以 `setValue` 仍然是一个纯函数。当然,你也可以直接在 Action 中返回一个新对象,不过这样的写法会更加复杂一些。
29
26
 
@@ -1,8 +1,3 @@
1
- ---
2
- sidebar_position: 13
3
- title: 常见问题
4
- ---
5
-
6
1
  # 常见问题
7
2
 
8
3
  ## 浏览器兼容性
@@ -1,7 +1,3 @@
1
- ---
2
- sidebar_position: 5
3
- title: 副作用管理
4
- ---
5
1
  # 副作用管理
6
2
 
7
3
  Model 中的 Action 必须是一个纯函数,执行过程中不会产生任何副作用。但在真实的业务中,我们会遇到很多副作用场景,如:请求 HTTP 接口获取状态数据,或者在更新状态的同时修改 localStorage、发送事件等。在 Reduck 中,是通过 Model 的 Effects 函数管理副作用的。
@@ -1,7 +1,3 @@
1
- ---
2
- sidebar_position: 7
3
- title: Model 通信
4
- ---
5
1
  # Model 通信
6
2
 
7
3
  Model 通信,既指不同 Model 间的通信,也指同一个 Model 内部 Effects、Actions 之间的通信。
@@ -100,6 +96,7 @@ Modern.js 默认开启 [自动生成 actions](./auto-actions.mdx),所以 `step
100
96
  ![communicate-models](https://lf3-static.bytednsdoc.com/obj/eden-cn/aphqeh7uhohpquloj/modern-js/docs/models-communicate.gif)
101
97
 
102
98
  :::info 补充信息
99
+
103
100
  - 本节完整的[示例代码](https://github.com/web-infra-dev/modern-js-examples/tree/main/examples/runtime-api/models-communication)。
104
101
  - 相关 API 的更多介绍,请参考:[model](/apis/app/runtime/model/model_#函数类型)。
105
102
 
@@ -1,7 +1,3 @@
1
- ---
2
- sidebar_position: 8
3
- title: 性能优化
4
- ---
5
1
  # 性能优化
6
2
 
7
3
  Reduck 内部已经做了大量性能优化工作,一般情况下不需要考虑性能问题。不过当对性能比较敏感、或者遇到了性能问题,可以考虑从以下 3 个方面,进行更有针对性的性能优化。
@@ -1,15 +1,14 @@
1
- ---
2
- sidebar_position: 1
3
- title: 快速上手
4
- ---
1
+ # 快速上手
5
2
 
6
3
  :::caution
7
4
  新项目不再推荐使用 Reduck,可以使用社区中的状态管理工具,例如 [Jotai](https://jotai.org/)、[zustand](https://zustand.docs.pmnd.rs/getting-started/introduction)、[valtio](https://valtio.dev/docs/introduction/getting-started) 等。
8
5
  :::
9
6
 
10
- # 快速上手
7
+ import ReduckNotify from '@site-docs/components/reduck-notify';
11
8
 
12
- import ReduckMigration from "@site-docs/components/reduck-migration"
9
+ <ReduckNotify />
10
+
11
+ import ReduckMigration from '@site-docs/components/reduck-migration';
13
12
 
14
13
  <ReduckMigration />
15
14
 
@@ -22,8 +21,8 @@ Reduck 在 MVC 模式中,扮演 M(Model) 的角色,React UI Component 对应
22
21
  Modern.js 的状态管理解决方案,是通过内置 Reduck 实现的。在 Modern.js 中使用 Reduck,不仅免去了手动集成的环节,而且所有 Reduck API 都可以从 Modern.js 的 Runtime 包中直接导入使用,具有更好的一致性体验。
23
22
 
24
23
  :::info
25
- 1. Modern.js 中使用 Reduck API,需要先设置 [runtime.state](/configure/app/runtime/state) 以启用状态管理插件。
26
- 2. Reduck 也可以脱离 Modern.js 作为状态管理库[单独使用](/guides/topic-detail/model/use-out-of-modernjs)。
24
+
25
+ Reduck 也可以脱离 Modern.js 作为状态管理库[单独使用](/guides/topic-detail/model/use-out-of-modernjs)。
27
26
 
28
27
  :::
29
28
 
@@ -1,7 +1,3 @@
1
- ---
2
- sidebar_position: 11
3
- title: Redux 生态集成
4
- ---
5
1
  # Redux 生态集成
6
2
 
7
3
  Reduck 基于 Redux 实现,因此可以使用 Redux [生态的库](https://redux.js.org/introduction/ecosystem),实现功能增强。通过 [`Provider`](/apis/app/runtime/model/Provider) 、[`createApp`](/apis/app/runtime/model/create-app) 和 [`createStore`](/apis/app/runtime/model/create-store) 等 API ,可以设置使用 Redux 的 [中间件](https://redux.js.org/understanding/thinking-in-redux/glossary#middleware) 和 [Store Enhancer](https://redux.js.org/understanding/thinking-in-redux/glossary#store-enhancer);使用 [`createStore`](/apis/app/runtime/model/create-store) 还可以完全掌控 Store 的创建过程。
@@ -1,7 +1,3 @@
1
- ---
2
- sidebar_position: 10
3
- title: TS 最佳实践
4
- ---
5
1
  # TS 最佳实践
6
2
 
7
3
  Reduck 对 TS 提供了良好的支持,大部分使用场景下,无需任何额外工作,就可以直接获得 API 的 TS 类型提示。本节,将对其他的一些使用场景,做补充介绍。
@@ -1,8 +1,3 @@
1
- ---
2
- sidebar_position: 3
3
- title: 使用 Model
4
- ---
5
-
6
1
  # 使用 Model
7
2
 
8
3
  ## 在组件内使用
@@ -1,7 +1,3 @@
1
- ---
2
- sidebar_position: 12
3
- title: 单独使用 Reduck
4
- ---
5
1
  # 单独使用 Reduck
6
2
 
7
3
  在 Modern.js 以外,单独集成 Reduck 使用时,主要需要做以下修改:
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.66.0",
18
+ "version": "2.66.1-alpha.0",
19
19
  "publishConfig": {
20
20
  "registry": "https://registry.npmjs.org/",
21
21
  "access": "public"