Skip to content

Commit f098a7d

Browse files
authored
docs: update runtime doc (#7076)
1 parent 9c9a6ef commit f098a7d

File tree

4 files changed

+110
-13
lines changed

4 files changed

+110
-13
lines changed

packages/document/main-doc/docs/en/configure/app/runtime/0-intro.mdx

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@
22

33
Modern.js runtime configuration should be centralized in the `src/modern.runtime.ts` file.
44

5+
:::warning
6+
7+
Using the `src/modern.runtime.ts` configuration approach requires Modern.js version **MAJOR_VERSION.66.0** or higher.
8+
9+
:::
10+
511
:::info
12+
613
If this file doesn't exist in your project yet, create it with the following command:
714

815
```bash
916
touch src/modern.runtime.ts
1017
```
18+
1119
:::
1220

1321
## Basic Configuration
@@ -33,7 +41,7 @@ For multi-entry applications, `defineRuntimeConfig` can accept a function that r
3341
```tsx
3442
import { defineRuntimeConfig } from '@modern-js/runtime';
3543

36-
export default defineRuntimeConfig((entryName) => {
44+
export default defineRuntimeConfig(entryName => {
3745
if (entryName === 'main') {
3846
return {
3947
router: {
@@ -53,12 +61,11 @@ export default defineRuntimeConfig((entryName) => {
5361
};
5462
});
5563
```
64+
5665
:::tip
66+
5767
Using the `src/modern.runtime.ts` configuration approach does not support exporting asynchronous functions, which is related to the rendering method of Modern.js. If you need to add asynchronous logic, please use the **[Runtime Plugin](/plugin/introduction.html#runtime-plugin)**.
58-
:::
5968

60-
:::warning
61-
Using the `src/modern.runtime.ts` configuration approach requires Modern.js version **MAJOR_VERSION.66.0** or higher.
6269
:::
6370

6471
import RuntimeCliConfig from '@site-docs/components/runtime-cli-config';
@@ -79,15 +86,23 @@ To improve maintainability, Modern.js introduced the unified `src/modern.runtime
7986
```ts
8087
// modern.config.ts
8188
export default {
82-
runtime: { /* ... */ },
83-
runtimeByEntries: { /* ... */ },
89+
runtime: {
90+
/* ... */
91+
},
92+
runtimeByEntries: {
93+
/* ... */
94+
},
8495
};
8596

8697
// App.config
87-
App.config = {/* ... */}
98+
App.config = {
99+
/* ... */
100+
};
88101

89102
// layout file
90-
export const config = () => { /* Dynamic configuration logic */ };
103+
export const config = () => {
104+
/* Dynamic configuration logic */
105+
};
91106
```
92107

93108
### Migration Recommendation

packages/document/main-doc/docs/en/guides/basic-features/routes.mdx

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ sidebar_position: 2
77
Modern.js routing is based on [React Router 6](https://reactrouter.com/en/main), offering file convention-based routing capabilities and supporting the industry-popular **nested routing** pattern. When an entry is recognized as [conventional routing](/guides/concept/entries.html#conventional-routing), Modern.js automatically generates the corresponding routing structure based on the file system.
88

99
:::note
10+
1011
The routing mentioned in this section all refers to conventional routing.
12+
1113
:::
1214

1315
## What is Nested Routing
@@ -38,7 +40,9 @@ In the `routes/` directory, subdirectory names are mapped to route URLs. Modern.
3840
- `layout.tsx`: This is the layout component and controls the layout of all sub-routes in its directory by using `<Outlet>` to represent child components.
3941

4042
:::tip
43+
4144
`.ts`, `.js`, `.jsx`, or `.tsx` file extensions can be used for the above convention files.
45+
4246
:::
4347

4448
### Page
@@ -83,7 +87,9 @@ export default () => {
8387
```
8488

8589
:::note
90+
8691
`<Outlet>` is an API provided by React Router 6. For more details, see [Outlet](https://reactrouter.com/en/main/components/outlet#outlet).
92+
8793
:::
8894

8995
Under different directory structures, the components represented by `<Outlet>` are also different. To illustrate the relationship between `<Layout>` and `<Outlet>`, let's consider the following directory structure:
@@ -219,7 +225,7 @@ When you visit `/blog/a` and no routes match, the page will render the `routes/b
219225
</RootLayout>
220226
```
221227

222-
If you want `/blog` to match the `blog/$.tsx` file as well, you need to remove the `blog.tsx` file from the same directory and ensure there are no other sub-routes under `blog`.
228+
If you want `/blog` to match the `blog/$.tsx` file as well, you need to remove the `blog/layout.tsx` file from the same directory and ensure there are no other sub-routes under `blog`.
223229

224230
Similarly, you can use [useParams](/apis/app/runtime/router/router#useparams) to capture the remaining part of the URL in the `$.tsx` component.
225231

@@ -480,6 +486,8 @@ The `prefetch` attribute has three optional values:
480486

481487
:::
482488

489+
import Motivation from '@site-docs-en/components/convention-routing-motivation';
490+
483491
<Motivation />
484492

485493
## FAQ
@@ -496,4 +504,34 @@ Additionally, when using conventional routing, make sure to use the API from `@m
496504
If you must directly use the React Router package's API (e.g., route behavior wrapped in a unified npm package), you can set [`source.alias`](/configure/app/source/alias) to point `react-router` and `react-router-dom` to the project's dependencies, avoiding the issue of two versions of React Router.
497505
:::
498506

499-
import Motivation from '@site-docs-en/components/convention-routing-motivation';
507+
2. About `config` function and `init` function
508+
509+
:::warning Not Recommended
510+
511+
Modern.js early versions supported runtime configuration and initialization operations through `config` function and `init` function exported in route layout files. These methods are still **supported**, but we **strongly recommend** using the [Runtime Configuration File](/configure/app/runtime/0-intro) and [Runtime Plugin](/plugin/introduction.html#runtime-plugin) to implement the corresponding functions.
512+
513+
:::
514+
515+
**config**
516+
517+
In route components, you can add dynamic Runtime configuration by exporting a `config` function:
518+
519+
```tsx
520+
// routes/layout.tsx
521+
export const config = () => {
522+
return {
523+
// dynamic Runtime configuration
524+
};
525+
};
526+
```
527+
528+
**init**
529+
530+
In route components, you can execute pre-rendering logic by exporting an `init` function:
531+
532+
```tsx
533+
// routes/layout.tsx
534+
export const init = () => {
535+
// initialization logic
536+
};
537+
```

packages/document/main-doc/docs/zh/configure/app/runtime/0-intro.mdx

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

33
Modern.js 的运行时(Runtime)配置需集中在 `src/modern.runtime.ts` 文件中声明。
44

5+
:::warning
6+
7+
使用 `src/modern.runtime.ts` 配置方式需要 Modern.js 版本 **MAJOR_VERSION.66.0** 或更高版本。
8+
9+
:::
10+
511
:::info
612
如果项目中还没有此文件,请执行以下命令创建:
713

@@ -59,9 +65,7 @@ export default defineRuntimeConfig(entryName => {
5965
使用 `src/modern.runtime.ts` 配置方式不支持导出异步函数,这与 Modern.js 的渲染方式有关。如果需要添加异步逻辑,请使用 **[Runtime 插件 (Runtime Plugin)](/plugin/introduction.html#runtime-插件)**
6066
:::
6167

62-
:::warning
63-
使用 `src/modern.runtime.ts` 配置方式需要 Modern.js 版本 **MAJOR_VERSION.66.0** 或更高版本。
64-
:::
68+
6569

6670
import RuntimeCliConfig from '@site-docs/components/runtime-cli-config';
6771

packages/document/main-doc/docs/zh/guides/basic-features/routes.mdx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ sidebar_position: 2
77
Modern.js 的路由基于 [React Router 6](https://reactrouter.com/en/main),提供了基于文件约定的路由能力,并支持了业界流行的约定式路由模式:**嵌套路由**。当入口被识别为 [约定式路由](/guides/concept/entries.html#约定式路由) 时,Modern.js 会自动基于文件系统,生成对应的路由结构。
88

99
:::note
10+
1011
本小节提到的路由,都是约定式路由。
12+
1113
:::
1214

1315
## 什么是嵌套路由
@@ -84,7 +86,9 @@ export default () => {
8486
```
8587

8688
:::note
89+
8790
`<Outlet>` 是 React Router 6 中提供的 API,详情可以查看 [Outlet](https://reactrouter.com/en/main/components/outlet#outlet)
91+
8892
:::
8993

9094
不同目录结构下,`<Outlet>` 所代表的组件也不同。为了方便介绍 `<Layout>``<Outlet>` 的关系,以下面的文件目录举例:
@@ -192,11 +196,15 @@ export default Blog;
192196
如果在某个子目录下存在 `$.tsx` 文件,该文件会作为通配路由组件,当没有匹配的路由时,会渲染该路由组件。
193197

194198
:::note
199+
195200
`$.tsx` 可以认为是一种特殊的 `<Page>` 组件,如果路由无法匹配,则 `$.tsx` 会作为 `<Layout>` 的子组件渲染。
201+
196202
:::
197203

198204
:::warning
205+
199206
如果当前目录下不存在 `<Layout>` 组件时,则 `$.tsx` 不会生效。
207+
200208
:::
201209

202210
例如以下目录结构:
@@ -498,7 +506,39 @@ import Motivation from '@site-docs/components/convention-routing-motivation';
498506
在使用约定式路由的情况下,务必使用 `@modern-js/runtime/router` 中的 API,不直接使用 React RouterAPI。因为 Modern.js 内部会安装 React Router,如果应用中使用了 React RouterAPI,可能会导致两个版本的 React Router 同时存在,出现不符合预期的行为。
499507

500508
:::note
509+
501510
如果应用中必须直接使用 React Router 包的 API,例如部分路由行为被封装在统一的 npm 包中,那应用可以通过设置 [`source.alias`](/configure/app/source/alias),将 `react-router``react-router-dom` 统一指向项目的依赖,避免两个版本的 React Router 同时存在的问题。
511+
502512
:::
503513

514+
2. 关于 `config` 函数和 `init` 函数的说明
504515

516+
:::warning 不推荐使用
517+
518+
Modern.js 早期版本支持在路由 layout 文件中通过导出 `config` 函数和 `init` 函数进行运行时配置和执行初始化操作。这些方式目前仍然**被支持**,但我们**强烈推荐**使用 [Runtime 配置文件](/configure/app/runtime/0-intro) 和 [Runtime 插件](/plugin/introduction.html#runtime-插件) 实现对应功能。
519+
520+
:::
521+
522+
**config**
523+
524+
在路由组件中,你可以通过导出 `config` 函数来添加动态 Runtime 配置:
525+
526+
```tsx
527+
// routes/layout.tsx
528+
export const config = () => {
529+
return {
530+
// 动态 Runtime 配置
531+
};
532+
};
533+
```
534+
535+
**init**
536+
537+
在路由组件中,你可以通过导出 `init` 函数来执行预渲染逻辑:
538+
539+
```tsx
540+
// routes/layout.tsx
541+
export const init = () => {
542+
// 初始化逻辑
543+
};
544+
```

0 commit comments

Comments
 (0)