@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.
- package/docs/en/apis/app/hooks/src/app.mdx +20 -34
- package/docs/en/apis/app/hooks/src/modern.runtime.mdx +9 -0
- package/docs/en/apis/app/runtime/app/define-config.mdx +6 -0
- package/docs/en/components/enable-micro-frontend.mdx +20 -3
- package/docs/en/components/micro-runtime-config.mdx +12 -13
- package/docs/en/components/reduck-notify.mdx +27 -0
- package/docs/en/configure/app/runtime/0-intro.mdx +63 -91
- package/docs/en/configure/app/usage.mdx +93 -69
- package/docs/en/guides/basic-features/render/_meta.json +1 -1
- package/docs/en/guides/basic-features/render/before-render.mdx +115 -0
- package/docs/en/guides/basic-features/routes.mdx +0 -95
- package/docs/en/guides/topic-detail/micro-frontend/c02-development.mdx +3 -5
- package/docs/en/guides/topic-detail/micro-frontend/c03-main-app.mdx +4 -2
- package/docs/en/guides/topic-detail/model/auto-actions.mdx +0 -4
- package/docs/en/guides/topic-detail/model/computed-state.mdx +0 -5
- package/docs/en/guides/topic-detail/model/define-model.mdx +0 -4
- package/docs/en/guides/topic-detail/model/faq.mdx +0 -5
- package/docs/en/guides/topic-detail/model/manage-effects.mdx +0 -5
- package/docs/en/guides/topic-detail/model/model-communicate.mdx +0 -5
- package/docs/en/guides/topic-detail/model/performance.mdx +0 -4
- package/docs/en/guides/topic-detail/model/quick-start.mdx +5 -7
- package/docs/en/guides/topic-detail/model/redux-integration.mdx +0 -4
- package/docs/en/guides/topic-detail/model/typescript-best-practice.mdx +0 -4
- package/docs/en/guides/topic-detail/model/use-model.mdx +0 -5
- package/docs/en/guides/topic-detail/model/use-out-of-modernjs.mdx +0 -4
- package/docs/zh/apis/app/hooks/src/app.mdx +18 -26
- package/docs/zh/apis/app/hooks/src/modern.runtime.mdx +9 -0
- package/docs/zh/apis/app/runtime/app/define-config.mdx +5 -0
- package/docs/zh/components/enable-micro-frontend.mdx +19 -12
- package/docs/zh/components/micro-runtime-config.mdx +3 -3
- package/docs/zh/components/reduck-notify.mdx +27 -0
- package/docs/zh/configure/app/runtime/0-intro.mdx +67 -86
- package/docs/zh/configure/app/usage.mdx +44 -21
- package/docs/zh/guides/basic-features/render/_meta.json +1 -1
- package/docs/zh/guides/basic-features/render/before-render.mdx +115 -0
- package/docs/zh/guides/basic-features/routes.mdx +0 -95
- package/docs/zh/guides/topic-detail/micro-frontend/c02-development.mdx +3 -5
- package/docs/zh/guides/topic-detail/micro-frontend/c03-main-app.mdx +4 -2
- package/docs/zh/guides/topic-detail/model/auto-actions.mdx +0 -4
- package/docs/zh/guides/topic-detail/model/computed-state.mdx +0 -4
- package/docs/zh/guides/topic-detail/model/define-model.mdx +1 -4
- package/docs/zh/guides/topic-detail/model/faq.mdx +0 -5
- package/docs/zh/guides/topic-detail/model/manage-effects.mdx +0 -4
- package/docs/zh/guides/topic-detail/model/model-communicate.mdx +1 -4
- package/docs/zh/guides/topic-detail/model/performance.mdx +0 -4
- package/docs/zh/guides/topic-detail/model/quick-start.mdx +7 -8
- package/docs/zh/guides/topic-detail/model/redux-integration.mdx +0 -4
- package/docs/zh/guides/topic-detail/model/typescript-best-practice.mdx +0 -4
- package/docs/zh/guides/topic-detail/model/use-model.mdx +0 -5
- package/docs/zh/guides/topic-detail/model/use-out-of-modernjs.mdx +0 -4
- 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
|
-
- 约定式路由
|
18
|
-
- 自控式路由
|
19
|
-
- 其他 (
|
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="
|
39
|
-
|
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: 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,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
|

|
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,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
|
-
|
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
|
-
|
26
|
-
|
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 的创建过程。
|