Skip to content

Commit 756e875

Browse files
committed
fix: adjust structure
1 parent 404070a commit 756e875

File tree

21 files changed

+594
-337
lines changed

21 files changed

+594
-337
lines changed

apps/website-new/docs/en/guide/basic/data-fetch/index.mdx

Lines changed: 13 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,24 @@ In SSR scenarios, `useEffect` does not execute. This behavior means that under n
1313

1414
To support this functionality, mainstream frameworks typically pre-fetch data using the `data loader` provided by React Router and inject it into the route component. The route component then retrieves the data using [useLoaderData](https://reactrouter.com/api/hooks/useLoaderData#useloaderdata) for rendering.
1515

16-
This approach heavily relies on routing functionality and cannot be used directly with Module Federation.
16+
This approach heavily relies on routing functionality and cannot be used directly with {props.name || 'Module Federation'}.
1717

18-
To solve this problem, Module Federation provides **component-level** data fetching capabilities, allowing developers to fetch data and render components in SSR scenarios.
18+
To solve this problem, {props.name || 'Module Federation'} provides **component-level** data fetching capabilities, allowing developers to fetch data and render components in SSR scenarios.
1919

2020
:::tip What is component-level?
21-
The use of Module Federation can be broadly divided into two parts: components (functions) and applications. The difference between them is whether they include *routing* functionality.
21+
The use of {props.name || 'Module Federation'} can be broadly divided into two parts: components (functions) and applications. The difference between them is whether they include *routing* functionality.
2222
:::
2323

2424
## How to Use
2525

2626
Different actions are required depending on the role.
2727

2828
### Producer
29-
::: info Note
30-
Producers can use [Rslib](../../basic/rsbuild#ssr) and [Modern.js](../../framework/modernjs) to generate components.
3129

32-
However, it's important to note that because the data in "Data Fetching" is injected by the consumer, if a component uses "Data Fetching", its exported non-MF components cannot be isomorphic with the MF components.
33-
:::
30+
import React from 'react';
31+
import ProviderTip from '@components/en/data-fetch/provider-tip';
32+
33+
{props.providerTip || React.createElement(ProviderTip)}
3434

3535
Each exposed module can have a corresponding `.data` file with the same name. These files can export a loader function, which we call a Data Loader. It executes before the corresponding `expose` component renders, providing data to the component. Here is an example:
3636

@@ -84,7 +84,7 @@ export default List;
8484

8585
If you use Modern.js to develop a producer, and that producer's page is also accessed directly, you can use the [Data Loader](https://modernjs.dev/guides/basic-features/data/data-fetch.html) provided by Modern.js to inject data.
8686

87-
Its usage is almost identical to Module Federation's, except for the function name. This makes it easy to consume the Data Loader in the producer. Here's an example:
87+
Its usage is almost identical to {props.name || 'Module Federation'}'s, except for the function name. This makes it easy to consume the Data Loader in the producer. Here's an example:
8888

8989
* Create a `page.data.ts` file in the producer's page directory and export a function named `loader`:
9090

@@ -97,7 +97,7 @@ export const loader = fetchData
9797
export type {Data}
9898
```
9999

100-
* Consume this data on the producer's `page`:
100+
* Consume this data on the producer's `page`:
101101

102102
```tsx title="page.tsx"
103103
import { useLoaderData } from '@modern-js/runtime/router';
@@ -121,48 +121,10 @@ export default Index;
121121
```
122122

123123
### Consumer
124-
::: info Note
125-
In SSR scenarios, this can only be used with [Modern.js](../../framework/modernjs).
126-
:::
127124

128-
In the consumer, we need to use the [createLazyComponent](../../../practice/bridge/react-bridge#createlazycomponent) API to load the remote component and fetch its data.
129-
130-
```tsx
131-
import { getInstance } from '@module-federation/enhanced/runtime';
132-
import { createLazyComponent, ERROR_TYPE, lazyLoadComponentPlugin } from '@module-federation/bridge-react';
133-
134-
const instance = getInstance();
135-
instance.registerPlugins([lazyLoadComponentPlugin()]);
136-
137-
const List = instance.createLazyComponent({
138-
loader: () => {
139-
return import('remote/List');
140-
},
141-
loading: 'loading...',
142-
export: 'default',
143-
fallback: ({error,errorType,dataFetchMapKey}) => {
144-
console.error(error)
145-
if(errorType === ERROR_TYPE.LOAD_REMOTE){
146-
return <div>load remote failed</div>
147-
}
148-
if(errorType === ERROR_TYPE.DATA_FETCH){
149-
return <div>data fetch failed, the dataFetchMapKey key is: {dataFetchMapKey}</div>
150-
}
151-
return <div>error type is unknown</div>;
152-
},
153-
});
154-
155-
const Index = (): JSX.Element => {
156-
return (
157-
<div>
158-
<h1>Basic usage with data fetch</h1>
159-
<List />
160-
</div>
161-
);
162-
};
125+
import Consumer from '@components/en/data-fetch/consumer';
163126

164-
export default Index;
165-
```
127+
{props.consumer || React.createElement(Consumer)}
166128

167129
### Loader Function
168130

@@ -184,7 +146,7 @@ The loader function can be executed on the server or in the browser. A loader fu
184146

185147
In CSR applications, the loader function is executed in the browser, so they are all Client Loaders by default.
186148

187-
In SSR applications, the loader function is only executed on the server, so they are all Server Loaders by default. In SSR, Module Federation directly calls the corresponding loader function on the server. When switching routes in the browser, Module Federation sends an HTTP request to the SSR service, which also triggers the loader function on the server.
149+
In SSR applications, the loader function is only executed on the server, so they are all Server Loaders by default. In SSR, {props.name || 'Module Federation'} directly calls the corresponding loader function on the server. When switching routes in the browser, {props.name || 'Module Federation'} sends an HTTP request to the SSR service, which also triggers the loader function on the server.
188150

189151
:::note NOTE
190152

@@ -203,7 +165,7 @@ By default, in SSR applications, the loader function is only executed on the ser
203165
1. To reduce network consumption in the browser by directly requesting the data source.
204166
2. The application has a data cache in the browser and does not want to request data from the SSR service.
205167

206-
Module Federation supports adding an additional `.data.client` file in SSR applications, which also exports a named loader. In this case, if the Data Loader on the server fails and falls back, or when switching routes in the browser, the application will execute this loader function in the browser like a CSR application, instead of sending another data request to the SSR service.
168+
{props.name || 'Module Federation'} supports adding an additional `.data.client` file in SSR applications, which also exports a named loader. In this case, if the Data Loader on the server fails and falls back, or when switching routes in the browser, the application will execute this loader function in the browser like a CSR application, instead of sending another data request to the SSR service.
207169

208170
```ts title="List.data.client.ts"
209171
import cache from 'my-cache';

apps/website-new/docs/en/guide/basic/data-fetch/prefetch.mdx

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
The `prefetch` function is used to pre-fetch resources and **data** for remote modules, thereby improving application performance and user experience. By pre-loading required content before a user accesses a feature, waiting times can be significantly reduced.
44

5-
> This API requires the [`lazyLoadComponentPlugin` to be registered](../../../practice/bridge/react-bridge#register-lazyloadcomponentplugin) before it can be used.
5+
import React from 'react';
6+
import PrefetchTip from '@components/en/data-fetch/prefetch-tip';
7+
8+
{props.prefetchTip || React.createElement(PrefetchTip)}
69

710
## When to Use
811

@@ -36,39 +39,8 @@ interface FederationHost {
3639

3740
Suppose we have a remote application `shop` that exposes a `Button` component, and this component is associated with a data fetch function.
3841

39-
### Scenario 1: Prefetching Data Only
40-
41-
When a user hovers over a link that will navigate to the shop page, we can prefetch the data needed for that page.
42-
43-
```ts
44-
import { getInstance } from '@module-federation/runtime';
45-
46-
const instance = getInstance();
47-
48-
const handleMouseEnter = () => {
49-
instance.prefetch({
50-
id: 'shop/Button',
51-
dataFetchParams: { productId: '12345' },
52-
});
53-
};
54-
```
42+
import PrefetchDemo from '@components/en/data-fetch/prefetch-demo'
5543

56-
### Scenario 2: Prefetching Data and Preloading Component Resources
57-
58-
For further optimization, we can download the component's JS and CSS files at the same time as prefetching the data.
59-
60-
```ts
61-
import { getInstance } from '@module-federation/runtime';
62-
63-
const instance = getInstance();
64-
65-
const handleMouseEnter = () => {
66-
instance.prefetch({
67-
id: 'shop/Button',
68-
dataFetchParams: { productId: '12345' },
69-
preloadComponentResource: true,
70-
});
71-
};
72-
```
44+
{props.prefetchDemo || React.createElement(PrefetchDemo)}
7345

7446
By using `prefetch` flexibly, you can finely control the timing of resource loading based on your application's specific scenarios and user behavior, thereby optimizing application performance.

apps/website-new/docs/en/guide/troubleshooting/runtime/RUNTIME-008.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ Failed to load resource during the runtime process, which may be caused by netwo
99

1010
## Solutions
1111

12-
check whether the resource address is correct. If the resource address is correct, check whether the network is stable. If the network is unstable, you can add the retry mechanism, refer to [Runtime Retry Mechanism](en/plugin/plugins/retry-plugin.html).
12+
check whether the resource address is correct. If the resource address is correct, check whether the network is stable. If the network is unstable, you can add the retry mechanism, refer to [Runtime Retry Mechanism](/plugin/plugins/retry-plugin.html).

apps/website-new/docs/zh/guide/basic/data-fetch/index.mdx

Lines changed: 19 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# 数据获取
22

3+
import React from 'react';
4+
import ProviderTip from '@components/zh/data-fetch/provider-tip';
5+
import Consumer from '@components/zh/data-fetch/consumer';
6+
37
:::tip
48
Data Loader 同时支持 SSR 和 CSR 两种场景!
59

@@ -13,24 +17,21 @@ SSR 场景中, `useEffect` 不会执行,这一行为导致常规情况下,
1317

1418
为了支持这一功能,主流框架一般会基于 React Router 提供的 `data loader` 预取数据,并注入给路由组件,路由组件通过 [useLoaderData](https://reactrouter.com/api/hooks/useLoaderData#useloaderdata) 获取数据并渲染。
1519

16-
这一行为强依赖路由功能,在 Module Federation 下就无法正常使用。
20+
这一行为强依赖路由功能,在 {props.name || 'Module Federation'} 下就无法正常使用。
1721

1822
为了解决这一问题,Module Federation 提供了**组件**级别数据获取能力,以便开发者可以在 SSR 场景下获取数据并渲染组件。
1923

2024
:::tip 什么是组件级别?
21-
Module Federation 的使用大体分为两种部分:组件(函数)、应用。两者的区别在于是否带有*路由*功能。
25+
{props.name || 'Module Federation'} 的使用大体分为两种部分:组件(函数)、应用。两者的区别在于是否带有*路由*功能。
2226
:::
2327

2428
## 如何使用
2529

2630
根据角色的不同,需要做不同的行为。
2731

2832
### 生产者
29-
::: info 注意
30-
生产者可以使用 [Rslib](../../basic/rsbuild#ssr)[Modern.js](../../framework/modernjs) 来生成组件。
3133

32-
不过需要注意的是,因为「数据获取」中的数据由消费者注入。因此如果在组件中使用了「数据获取」,那么其导出的非 MF 组件无法与 MF 组件保持同构。
33-
:::
34+
{props.providerTip || React.createElement(ProviderTip)}
3435

3536
每个 expose 模块都可以有一个同名的 `.data` 文件。这些文件可以导出一个 loader 函数,我们称为 Data Loader,它会在对应的 `expose` 组件渲染之前执行,为组件提供数据。如下面示例:
3637

@@ -80,11 +81,11 @@ const List = (props: {
8081
export default List;
8182
```
8283

83-
##### 生产者消费自身数据
84+
#### 生产者消费自身数据
8485

8586
如果使用了 Modern.js 来开发生产者,并且该生产者页面也会单独访问。那么可以利用 Modern.js 提供的 [Data Loader](https://modernjs.dev/zh/guides/basic-features/data/data-fetch.html) 来注入数据。
8687

87-
其用法与 Module Federation 除了函数名称外,基本一致。因此你可以很方便的在生产者消费 Data Loader,示例如下:
88+
其用法与 {props.name || 'Module Federation'} 除了函数名称外,基本一致。因此你可以很方便的在生产者消费 Data Loader,示例如下:
8889

8990
* 在生产者页面创建 `page.data.ts` 文件,导出名为 `loader` 的函数:
9091

@@ -120,71 +121,31 @@ export default Index;
120121

121122
```
122123

123-
#### 消费者
124-
::: info 注意
125-
SSR 场景中,只能在 [Modern.js](../../framework/modernjs) 中使用。
126-
:::
127-
128-
在消费者中,我们需要通过 [createLazyComponent](../../../practice/bridge/react-bridge#createlazycomponent) API 来加载远程组件,并获取数据。
129-
130-
```tsx
131-
import { getInstance } from '@module-federation/enhanced/runtime';
132-
import { createLazyComponent, ERROR_TYPE, lazyLoadComponentPlugin } from '@module-federation/bridge-react';
133-
134-
const instance = getInstance();
135-
instance.registerPlugins([lazyLoadComponentPlugin()]);
136-
137-
const List = instance.createLazyComponent({
138-
loader: () => {
139-
return import('remote/List');
140-
},
141-
loading: 'loading...',
142-
export: 'default',
143-
fallback: ({error,errorType,dataFetchMapKey}) => {
144-
console.error(error)
145-
if(errorType === ERROR_TYPE.LOAD_REMOTE){
146-
return <div>load remote failed</div>
147-
}
148-
if(errorType === ERROR_TYPE.DATA_FETCH){
149-
return <div>data fetch failed, the dataFetchMapKey key is: {dataFetchMapKey}</div>
150-
}
151-
return <div>error type is unknown</div>;
152-
},
153-
});
154-
155-
const Index = (): JSX.Element => {
156-
return (
157-
<div>
158-
<h1>Basic usage with data fetch</h1>
159-
<List />
160-
</div>
161-
);
162-
};
124+
### 消费者
163125

164-
export default Index;
165-
```
126+
{props.consumer || React.createElement(Consumer)}
166127

167-
### loader 函数
128+
#### loader 函数
168129

169-
#### 入参
130+
##### 入参
170131

171132
默认会往 loader 函数传递参数,其类型为 `DataFetchParams`,包含以下字段:
172133

173134
- `isDowngrade` (boolean): 表示当前执行上下文是否处于降级模式。例如,在服务端渲染 (SSR) 失败,在客户端渲染(CSR)中会重新往服务端发起请求,调用 loader 函数,此时该值为 `true`
174135

175-
除了默认的参数以外,还可以在 `createLazyComponent` 中传递 [dataFetchParams](../../../practice/bridge/react-bridge#datafetchparams) 字段,该字段会被透传给 loader 函数。
136+
除了默认的参数以外,还可以在 `createLazyComponent` 中传递 `dataFetchParams` 字段,该字段会被透传给 loader 函数。
176137

177-
#### 返回值
138+
##### 返回值
178139

179140
loader 函数的返回值只能是**可序列化的数据对象**
180141

181-
### 在不同环境使用 Data Loader
142+
#### 在不同环境使用 Data Loader
182143

183144
loader 函数可能会在服务端或浏览器端执行。在服务端执行的 loader 函数,我们称为 Server Loader,在浏览器端执行的称为 Client Loader。
184145

185146
在 CSR 应用中,loader 函数会在浏览器端执行,即默认都是 Client Loader。
186147

187-
在 SSR 应用中,loader 函数只会在服务端执行,即默认都是 Server Loader。在 SSR Module Federation 会直接在服务端调用对应的 loader 函数。在浏览器端切换路由时,Module Federation 会发送一个 http 请求到 SSR 服务,同样在服务端触发 loader 函数。
148+
在 SSR 应用中,loader 函数只会在服务端执行,即默认都是 Server Loader。在 SSR {props.name || 'Module Federation'} 会直接在服务端调用对应的 loader 函数。在浏览器端切换路由时,Module Federation 会发送一个 http 请求到 SSR 服务,同样在服务端触发 loader 函数。
188149

189150
:::note NOTE
190151

@@ -196,14 +157,14 @@ SSR 应用的 loader 函数只在服务端执行可以带来以下好处:
196157

197158
:::
198159

199-
#### 在 SSR 应用中使用 Client Loader
160+
##### 在 SSR 应用中使用 Client Loader
200161

201162
默认情况下,在 SSR 应用中,loader 函数只会在服务端执行。但有些场景下,开发者可能期望在浏览器端发送的请求不经过 SSR 服务,直接请求数据源,例如:
202163

203164
1. 在浏览器端希望减少网络消耗,直接请求数据源。
204165
2. 应用在浏览器端有数据缓存,不希望请求 SSR 服务获取数据。
205166

206-
Module Federation 支持在 SSR 应用中额外添加 .data.client 文件,同样具名导出 loader。此时 SSR 应用在服务端执行 Data Loader 报错降级,或浏览器端切换路由时,会像 CSR 应用一样在浏览器端执行该 loader 函数,而不是再向 SSR 服务发送数据请求。
167+
{props.name || 'Module Federation'} 支持在 SSR 应用中额外添加 .data.client 文件,同样具名导出 loader。此时 SSR 应用在服务端执行 Data Loader 报错降级,或浏览器端切换路由时,会像 CSR 应用一样在浏览器端执行该 loader 函数,而不是再向 SSR 服务发送数据请求。
207168

208169
```ts title="List.data.client.ts"
209170
import cache from 'my-cache';

apps/website-new/docs/zh/guide/basic/data-fetch/prefetch.mdx

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
`prefetch` 函数用于预取远程模块的资源和**数据**,从而提升应用的性能和用户体验。通过在用户访问某个功能之前提前加载所需内容,可以显著减少等待时间。
44

5-
> 该 API 需要先[注册 lazyLoadComponentPlugin 插件](../../../practice/bridge/react-bridge#注册-lazyloadcomponentplugin),才可以使用。
5+
import React from 'react';
6+
import PrefetchTip from '@components/zh/data-fetch/prefetch-tip';
7+
8+
{props.prefetchTip || React.createElement(PrefetchTip)}
69

710
## 何时使用
811

@@ -36,39 +39,8 @@ interface FederationHost {
3639

3740
假设我们有一个远程应用 `shop`,它暴露了一个组件 `Button`,并且该组件关联了一个数据获取函数。
3841

39-
### 场景 1: 仅预取数据
40-
41-
当用户鼠标悬停在一个将要导航到购物页面的链接上时,我们可以预取该页面所需的数据。
42-
43-
```ts
44-
import { getInstance } from '@module-federation/runtime';
45-
46-
const instance = getInstance();
47-
48-
const handleMouseEnter = () => {
49-
instance.prefetch({
50-
id: 'shop/Button',
51-
dataFetchParams: { productId: '12345' },
52-
});
53-
};
54-
```
42+
import PrefetchDemo from '@components/zh/data-fetch/prefetch-demo';
5543

56-
### 场景 2: 预取数据并预加载组件资源
57-
58-
为了进一步优化,我们可以在预取数据的同时,也把组件的 JS 和 CSS 文件下载下来。
59-
60-
```ts
61-
import { getInstance } from '@module-federation/runtime';
62-
63-
const instance = getInstance();
64-
65-
const handleMouseEnter = () => {
66-
instance.prefetch({
67-
id: 'shop/Button',
68-
dataFetchParams: { productId: '12345' },
69-
preloadComponentResource: true,
70-
});
71-
};
72-
```
44+
{props.prefetchDemo || React.createElement(PrefetchDemo)}
7345

7446
通过灵活使用 `prefetch`,您可以根据应用的具体场景和用户行为,精细地控制资源的加载时机,从而优化应用性能。

apps/website-new/docs/zh/guide/troubleshooting/runtime/RUNTIME-008.mdx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ Runtime 运行时资源加载失败报错,可能原因为网络不稳定导致
99

1010
## 解决方法
1111

12-
检查资源地址是否正确,若资源地址正确,检查网络是否稳定。网络不稳定时可增加重试机制,参考 [Runtime 重试机制](zh/plugin/plugins/retry-plugin.html)
13-
12+
检查资源地址是否正确,若资源地址正确,检查网络是否稳定。网络不稳定时可增加重试机制,参考 [Runtime 重试机制](/plugin/plugins/retry-plugin.html)

apps/website-new/docs/zh/practice/bridge/react-bridge.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ type ErrorInfo = {
408408
```tsx
409409
import React, { FC, memo, useEffect } from 'react';
410410
import { getInstance } from '@module-federation/enhanced/runtime';
411-
import { ,createLazyComponent, ERROR_TYPE } from '@module-federation/bridge-react';
411+
import { createLazyComponent, ERROR_TYPE } from '@module-federation/bridge-react';
412412

413413
const instance = getInstance();
414414
const LazyComponent = instance.createLazyComponent({

0 commit comments

Comments
 (0)