@modern-js/main-doc 2.67.2 → 2.67.3
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/configure/app/runtime/0-intro.mdx +23 -8
- package/docs/en/guides/basic-features/deploy.mdx +60 -9
- package/docs/en/guides/basic-features/routes.mdx +40 -2
- package/docs/zh/configure/app/runtime/0-intro.mdx +7 -3
- package/docs/zh/guides/basic-features/deploy.mdx +56 -8
- package/docs/zh/guides/basic-features/routes.mdx +40 -0
- package/package.json +3 -3
@@ -2,12 +2,20 @@
|
|
2
2
|
|
3
3
|
Modern.js runtime configuration should be centralized in the `src/modern.runtime.ts` file.
|
4
4
|
|
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
|
+
|
5
11
|
:::info
|
12
|
+
|
6
13
|
If this file doesn't exist in your project yet, create it with the following command:
|
7
14
|
|
8
15
|
```bash
|
9
16
|
touch src/modern.runtime.ts
|
10
17
|
```
|
18
|
+
|
11
19
|
:::
|
12
20
|
|
13
21
|
## Basic Configuration
|
@@ -33,7 +41,7 @@ For multi-entry applications, `defineRuntimeConfig` can accept a function that r
|
|
33
41
|
```tsx
|
34
42
|
import { defineRuntimeConfig } from '@modern-js/runtime';
|
35
43
|
|
36
|
-
export default defineRuntimeConfig(
|
44
|
+
export default defineRuntimeConfig(entryName => {
|
37
45
|
if (entryName === 'main') {
|
38
46
|
return {
|
39
47
|
router: {
|
@@ -53,12 +61,11 @@ export default defineRuntimeConfig((entryName) => {
|
|
53
61
|
};
|
54
62
|
});
|
55
63
|
```
|
64
|
+
|
56
65
|
:::tip
|
66
|
+
|
57
67
|
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
|
-
:::
|
59
68
|
|
60
|
-
:::warning
|
61
|
-
Using the `src/modern.runtime.ts` configuration approach requires Modern.js version **MAJOR_VERSION.66.0** or higher.
|
62
69
|
:::
|
63
70
|
|
64
71
|
import RuntimeCliConfig from '@site-docs/components/runtime-cli-config';
|
@@ -79,15 +86,23 @@ To improve maintainability, Modern.js introduced the unified `src/modern.runtime
|
|
79
86
|
```ts
|
80
87
|
// modern.config.ts
|
81
88
|
export default {
|
82
|
-
runtime: {
|
83
|
-
|
89
|
+
runtime: {
|
90
|
+
/* ... */
|
91
|
+
},
|
92
|
+
runtimeByEntries: {
|
93
|
+
/* ... */
|
94
|
+
},
|
84
95
|
};
|
85
96
|
|
86
97
|
// App.config
|
87
|
-
App.config = {
|
98
|
+
App.config = {
|
99
|
+
/* ... */
|
100
|
+
};
|
88
101
|
|
89
102
|
// layout file
|
90
|
-
export const config = () => {
|
103
|
+
export const config = () => {
|
104
|
+
/* Dynamic configuration logic */
|
105
|
+
};
|
91
106
|
```
|
92
107
|
|
93
108
|
### Migration Recommendation
|
@@ -6,7 +6,7 @@ sidebar_position: 15
|
|
6
6
|
|
7
7
|
Currently, Modern.js offers two deployment way:
|
8
8
|
- You can host your application in a container that includes a Node.js environment on your own, which provides flexibility for the deployment of the application.
|
9
|
-
- You can also deploy your application through a platform. Currently, Modern.js supports
|
9
|
+
- You can also deploy your application through a platform. Currently, Modern.js officially supports deployment on [Netlify](https://www.netlify.com/), [Vercel](https://vercel.com/), and [Github pages](https://pages.github.com/).
|
10
10
|
|
11
11
|
:::info
|
12
12
|
Currently, Modern.js only supports running in a Node.js environment. Support for more runtime environments will be provided in the future.
|
@@ -110,6 +110,11 @@ Add the following content to `netlify.toml`:
|
|
110
110
|
command = "modern deploy"
|
111
111
|
```
|
112
112
|
|
113
|
+
:::info
|
114
|
+
You can refer to the [deployment project example](https://github.com/web-infra-dev/modern-js-examples/tree/main/examples/modern-js-deploy-csr).
|
115
|
+
|
116
|
+
:::
|
117
|
+
|
113
118
|
Now, add a project to the Netlify platform and deploy it!
|
114
119
|
|
115
120
|
### Full Stack Project
|
@@ -129,7 +134,8 @@ Full-stack projects refer to projects that use Custom Web Server, SSR or BFF. Th
|
|
129
134
|
```
|
130
135
|
|
131
136
|
:::info
|
132
|
-
Currently, Modern.js does not support deployment on Netlify Edge Functions. We will support it in future versions.
|
137
|
+
1. Currently, Modern.js does not support deployment on Netlify Edge Functions. We will support it in future versions.
|
138
|
+
2. You can refer to the [deployment project example](https://github.com/web-infra-dev/modern-js-examples/tree/main/examples/modern-js-deploy-ssr).
|
133
139
|
:::
|
134
140
|
|
135
141
|
|
@@ -213,6 +219,11 @@ Commit your project to git, select Framework Preset as `Other` on the Vercel pla
|
|
213
219
|
|
214
220
|
<img src="https://sf16-sg.tiktokcdn.com/obj/eden-sg/lmeh7nuptpfnuhd/vercel-framework-preset.png" />
|
215
221
|
|
222
|
+
:::info
|
223
|
+
You can refer to the [deployment project examples](https://github.com/web-infra-dev/modern-js-examples/tree/main/examples/modern-js-deploy-csr).
|
224
|
+
|
225
|
+
:::
|
226
|
+
|
216
227
|
### Full Stack Project
|
217
228
|
|
218
229
|
Full-stack projects refer to projects that use Custom Web Server, SSR or BFF. These projects need to be deployed on **Vercel Functions**.
|
@@ -220,14 +231,12 @@ Full-stack projects refer to projects that use Custom Web Server, SSR or BFF. Th
|
|
220
231
|
In addition to configuring `vercel.json` in the same way as a [pure front-end project](#pure-front-end-project), there are two points to note for full-stack projects:
|
221
232
|
|
222
233
|
1. Currently, Modern.js does not support deploying BFF projects on the Vercel platform. We will support it in future versions.
|
223
|
-
2.
|
224
|
-
see [Serverless Function contains invalid runtime error](https://vercel.com/guides/serverless-function-contains-invalid-runtime-error), you can modify `package.json` to specify the version:
|
225
|
-
```json title="package.json"
|
226
|
-
"engines": {
|
227
|
-
"node": "18.x"
|
228
|
-
}
|
229
|
-
```
|
234
|
+
2. The Node.js version for function execution is determined by the project configuration on the Vercel platform.
|
230
235
|
|
236
|
+
:::info
|
237
|
+
You can refer to the [deployment project examples](https://github.com/web-infra-dev/modern-js-examples/tree/main/examples/modern-js-deploy-ssr).
|
238
|
+
|
239
|
+
:::
|
231
240
|
|
232
241
|
### Monorepo
|
233
242
|
|
@@ -284,6 +293,48 @@ Add the following content to the `packages/app/vercel.json` file:
|
|
284
293
|
|
285
294
|
Just submit your code and deploy it using the Vercel platform.
|
286
295
|
|
296
|
+
## Github Pages
|
297
|
+
|
298
|
+
If you're creating a GitHub Pages for a repository without a custom domain, the page URL will follow this format: `http://<username>.github.io/<repository-name>`. Therefore, you need to add the following configuration in `modern.config.ts`:
|
299
|
+
|
300
|
+
```
|
301
|
+
import { defineConfig } from '@modern-js/app-tools';
|
302
|
+
|
303
|
+
export default defineConfig({
|
304
|
+
//...
|
305
|
+
server: {
|
306
|
+
baseUrl: "/<repository-name>"
|
307
|
+
},
|
308
|
+
output: {
|
309
|
+
assetPrefix: "/<repository-name>",
|
310
|
+
}
|
311
|
+
});
|
312
|
+
```
|
313
|
+
|
314
|
+
GitHub Pages supports two deployment ways: branch deployment or GitHub Actions deployment.
|
315
|
+
|
316
|
+
For branch deployment, follow these steps:
|
317
|
+
|
318
|
+
1. In the GitHub repository, navigate to Settings > Pages > Source > Deploy from a branch
|
319
|
+
2. Install the `gh-pages` as devDependency
|
320
|
+
3. Add the following script to `package.json`
|
321
|
+
```
|
322
|
+
"scripts": {
|
323
|
+
//...
|
324
|
+
"deploy:gh-pages": "MODERNJS_DEPLOY=ghPages modern deploy && gh-pages -d .output"
|
325
|
+
}
|
326
|
+
```
|
327
|
+
|
328
|
+
4. Run `npm run deploy:gh-pages`
|
329
|
+
|
330
|
+
:::info
|
331
|
+
1. Running `MODERNJS_DEPLOY=ghPages modern deploy` will build the production output for GitHub in the .output directory.
|
332
|
+
2. You can refer to the [project](https://github.com/web-infra-dev/modern-js-examples/tree/main/examples/modern-js-deploy-csr)
|
333
|
+
:::
|
334
|
+
|
335
|
+
For GitHub Actions deployment, select Settings > Pages > Source > GitHub Actions, and add a workflow file to the project. You can refer to the [example](https://github.com/web-infra-dev/modern-js-examples/tree/main/examples/modern-js-deploy-csr).
|
336
|
+
|
337
|
+
|
287
338
|
## Using Self-Built Node.js Server
|
288
339
|
|
289
340
|
Typically, we recommend using the built-in Node.js server of Modern.js to deploy applications. It supports hosting both pure frontend and full-stack projects, ensuring consistent performance in both development and production environments.
|
@@ -7,7 +7,9 @@ sidebar_position: 2
|
|
7
7
|
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.
|
8
8
|
|
9
9
|
:::note
|
10
|
+
|
10
11
|
The routing mentioned in this section all refers to conventional routing.
|
12
|
+
|
11
13
|
:::
|
12
14
|
|
13
15
|
## What is Nested Routing
|
@@ -38,7 +40,9 @@ In the `routes/` directory, subdirectory names are mapped to route URLs. Modern.
|
|
38
40
|
- `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.
|
39
41
|
|
40
42
|
:::tip
|
43
|
+
|
41
44
|
`.ts`, `.js`, `.jsx`, or `.tsx` file extensions can be used for the above convention files.
|
45
|
+
|
42
46
|
:::
|
43
47
|
|
44
48
|
### Page
|
@@ -83,7 +87,9 @@ export default () => {
|
|
83
87
|
```
|
84
88
|
|
85
89
|
:::note
|
90
|
+
|
86
91
|
`<Outlet>` is an API provided by React Router 6. For more details, see [Outlet](https://reactrouter.com/en/main/components/outlet#outlet).
|
92
|
+
|
87
93
|
:::
|
88
94
|
|
89
95
|
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
|
|
219
225
|
</RootLayout>
|
220
226
|
```
|
221
227
|
|
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`.
|
223
229
|
|
224
230
|
Similarly, you can use [useParams](/apis/app/runtime/router/router#useparams) to capture the remaining part of the URL in the `$.tsx` component.
|
225
231
|
|
@@ -480,6 +486,8 @@ The `prefetch` attribute has three optional values:
|
|
480
486
|
|
481
487
|
:::
|
482
488
|
|
489
|
+
import Motivation from '@site-docs-en/components/convention-routing-motivation';
|
490
|
+
|
483
491
|
<Motivation />
|
484
492
|
|
485
493
|
## FAQ
|
@@ -496,4 +504,34 @@ Additionally, when using conventional routing, make sure to use the API from `@m
|
|
496
504
|
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.
|
497
505
|
:::
|
498
506
|
|
499
|
-
|
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
|
+
```
|
@@ -2,6 +2,12 @@
|
|
2
2
|
|
3
3
|
Modern.js 的运行时(Runtime)配置需集中在 `src/modern.runtime.ts` 文件中声明。
|
4
4
|
|
5
|
+
:::warning
|
6
|
+
|
7
|
+
使用 `src/modern.runtime.ts` 配置方式需要 Modern.js 版本 **MAJOR_VERSION.66.0** 或更高版本。
|
8
|
+
|
9
|
+
:::
|
10
|
+
|
5
11
|
:::info
|
6
12
|
如果项目中还没有此文件,请执行以下命令创建:
|
7
13
|
|
@@ -59,9 +65,7 @@ export default defineRuntimeConfig(entryName => {
|
|
59
65
|
使用 `src/modern.runtime.ts` 配置方式不支持导出异步函数,这与 Modern.js 的渲染方式有关。如果需要添加异步逻辑,请使用 **[Runtime 插件 (Runtime Plugin)](/plugin/introduction.html#runtime-插件)**。
|
60
66
|
:::
|
61
67
|
|
62
|
-
|
63
|
-
使用 `src/modern.runtime.ts` 配置方式需要 Modern.js 版本 **MAJOR_VERSION.66.0** 或更高版本。
|
64
|
-
:::
|
68
|
+
|
65
69
|
|
66
70
|
import RuntimeCliConfig from '@site-docs/components/runtime-cli-config';
|
67
71
|
|
@@ -7,7 +7,7 @@ sidebar_position: 15
|
|
7
7
|
目前,Modern.js 提供了两种部署方式:
|
8
8
|
|
9
9
|
- 你可以将应用自行托管在包含 Node.js 环境的容器中,这为应用提供了部署的灵活性。
|
10
|
-
- 你也可以通过平台部署应用,目前 Modern.js 官方支持了 [Netlify](https://www.netlify.com/)
|
10
|
+
- 你也可以通过平台部署应用,目前 Modern.js 官方支持了 [Netlify](https://www.netlify.com/), [Vercel](https://vercel.com/) 和 [Github pages](https://pages.github.com/) 平台。
|
11
11
|
|
12
12
|
:::info
|
13
13
|
目前 Modern.js 仅支持在 Node.js 环境中运行,未来将提供更多运行时环境的支持。
|
@@ -26,6 +26,7 @@ MODERNJS_DEPLOY=netlify npx modern deploy
|
|
26
26
|
在 Modern.js 官方支持的部署平台中部署时,无需指定环境变量。
|
27
27
|
:::
|
28
28
|
|
29
|
+
|
29
30
|
## ModernJS 内置 Node.js 服务器
|
30
31
|
|
31
32
|
### 单仓库项目
|
@@ -107,6 +108,11 @@ Netlify 是一个流行的 Web 开发平台,专为构建、发布和维护现
|
|
107
108
|
command = "modern deploy"
|
108
109
|
```
|
109
110
|
|
111
|
+
:::info
|
112
|
+
你可参考部署[项目示例](https://github.com/web-infra-dev/modern-js-examples/tree/main/examples/modern-js-deploy-csr)。
|
113
|
+
|
114
|
+
:::
|
115
|
+
|
110
116
|
在 Netlify 平台上添加项目,部署即可。
|
111
117
|
|
112
118
|
### 全栈项目
|
@@ -126,10 +132,14 @@ Netlify 是一个流行的 Web 开发平台,专为构建、发布和维护现
|
|
126
132
|
```
|
127
133
|
|
128
134
|
:::info
|
129
|
-
目前 Modern.js 还不支持在 Netlify Edge Functions 进行部署,我们将在后续的版本中支持。
|
135
|
+
1. 目前 Modern.js 还不支持在 Netlify Edge Functions 进行部署,我们将在后续的版本中支持。
|
136
|
+
2. 你可参考部署[项目示例](https://github.com/web-infra-dev/modern-js-examples/tree/main/examples/modern-js-deploy-ssr)。
|
137
|
+
|
130
138
|
:::
|
131
139
|
|
132
140
|
|
141
|
+
|
142
|
+
|
133
143
|
### Monorepo 项目
|
134
144
|
|
135
145
|
:::info
|
@@ -207,6 +217,11 @@ Vercel 是一个面向现代 Web 应用的部署平台,它提供了丰富的
|
|
207
217
|
|
208
218
|
<img src="https://sf16-sg.tiktokcdn.com/obj/eden-sg/lmeh7nuptpfnuhd/vercel-framework-preset.png" />
|
209
219
|
|
220
|
+
:::info
|
221
|
+
你可参考部署[项目示例](https://github.com/web-infra-dev/modern-js-examples/tree/main/examples/modern-js-deploy-csr)。
|
222
|
+
|
223
|
+
:::
|
224
|
+
|
210
225
|
### 全栈项目
|
211
226
|
|
212
227
|
全栈项目是指使用了自定义 Web Server、SSR、BFF 的项目,这些项目需要部署在 **Vercel Functions** 上。
|
@@ -214,12 +229,13 @@ Vercel 是一个面向现代 Web 应用的部署平台,它提供了丰富的
|
|
214
229
|
全栈项目除了按照[纯前端项目](#纯前端项目)的方式配置 `vercel.json` 外,有两点需要注意:
|
215
230
|
|
216
231
|
1. 当前,Modern.js 还不支持在 Vercel 平台上部署 BFF 项目,我们将在后续的版本中支持。
|
217
|
-
2.
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
232
|
+
2. 函数运行的 node.js 版本由项目在 Vercel 平台配置决定。
|
233
|
+
|
234
|
+
|
235
|
+
:::info
|
236
|
+
你可参考部署[项目示例](https://github.com/web-infra-dev/modern-js-examples/tree/main/examples/modern-js-deploy-ssr)。
|
237
|
+
|
238
|
+
:::
|
223
239
|
|
224
240
|
### Monorepo 项目
|
225
241
|
|
@@ -273,6 +289,38 @@ Vercel 是一个面向现代 Web 应用的部署平台,它提供了丰富的
|
|
273
289
|
|
274
290
|
提交你的代码,使用 Vercel 平台部署即可。
|
275
291
|
|
292
|
+
## Github Pages
|
293
|
+
|
294
|
+
如果你要为一个仓库常见 Github 页面,并且你没有自定义域名,则该页面的 URL 将会是以下格式:`http://<username>.github.io/<repository-name>`,所以需要在 `modern.config.ts` 中添加
|
295
|
+
以下配置:
|
296
|
+
```ts
|
297
|
+
import { defineConfig } from '@modern-js/app-tools';
|
298
|
+
|
299
|
+
export default defineConfig({
|
300
|
+
//...
|
301
|
+
server:{
|
302
|
+
baseUrl: "/<repository-name>"
|
303
|
+
},
|
304
|
+
output: {
|
305
|
+
assetPrefix: "/<repository-name>",
|
306
|
+
}
|
307
|
+
});
|
308
|
+
```
|
309
|
+
|
310
|
+
Github Pages 支持两种部署方式,通过分支部署或通过 Github Actions 部署,如果通过分支部署,可以使用以下步骤:
|
311
|
+
1. 在 github 仓库中,选择 `Settings > Pages > Source > Deploy from a branch`。
|
312
|
+
2. 安装 `gh-pages` 依赖作为开发依赖。
|
313
|
+
3. 在 package.json 的 `scripts` 中添加 `"deploy:gh-pages": "MODERNJS_DEPLOY=ghPages modern deploy && gh-pages -d .output"`。
|
314
|
+
4. 执行 `npm run deploy:gh-pages`。
|
315
|
+
|
316
|
+
:::info
|
317
|
+
1. 执行 `MODERNJS_DEPLOY=ghPages modern deploy`,Modern.js 会把可用于 github 部署的产物构建到 `.output` 目录。
|
318
|
+
2. 可以参考项目[示例](https://github.com/web-infra-dev/modern-js-examples/tree/main/examples/modern-js-deploy-csr)。
|
319
|
+
|
320
|
+
:::
|
321
|
+
|
322
|
+
如果通过 Github Actions 部署,可以选择 Settings > Pages > Source > GitHub Actions,并在项目中添加 workflow 文件,可参考[示例](https://github.com/web-infra-dev/modern-js-examples/tree/main/examples/modern-js-deploy-csr)。
|
323
|
+
|
276
324
|
|
277
325
|
## 自建 Node.js 服务器
|
278
326
|
|
@@ -7,7 +7,9 @@ sidebar_position: 2
|
|
7
7
|
Modern.js 的路由基于 [React Router 6](https://reactrouter.com/en/main),提供了基于文件约定的路由能力,并支持了业界流行的约定式路由模式:**嵌套路由**。当入口被识别为 [约定式路由](/guides/concept/entries.html#约定式路由) 时,Modern.js 会自动基于文件系统,生成对应的路由结构。
|
8
8
|
|
9
9
|
:::note
|
10
|
+
|
10
11
|
本小节提到的路由,都是约定式路由。
|
12
|
+
|
11
13
|
:::
|
12
14
|
|
13
15
|
## 什么是嵌套路由
|
@@ -84,7 +86,9 @@ export default () => {
|
|
84
86
|
```
|
85
87
|
|
86
88
|
:::note
|
89
|
+
|
87
90
|
`<Outlet>` 是 React Router 6 中提供的 API,详情可以查看 [Outlet](https://reactrouter.com/en/main/components/outlet#outlet)。
|
91
|
+
|
88
92
|
:::
|
89
93
|
|
90
94
|
不同目录结构下,`<Outlet>` 所代表的组件也不同。为了方便介绍 `<Layout>` 与 `<Outlet>` 的关系,以下面的文件目录举例:
|
@@ -192,11 +196,15 @@ export default Blog;
|
|
192
196
|
如果在某个子目录下存在 `$.tsx` 文件,该文件会作为通配路由组件,当没有匹配的路由时,会渲染该路由组件。
|
193
197
|
|
194
198
|
:::note
|
199
|
+
|
195
200
|
`$.tsx` 可以认为是一种特殊的 `<Page>` 组件,如果路由无法匹配,则 `$.tsx` 会作为 `<Layout>` 的子组件渲染。
|
201
|
+
|
196
202
|
:::
|
197
203
|
|
198
204
|
:::warning
|
205
|
+
|
199
206
|
如果当前目录下不存在 `<Layout>` 组件时,则 `$.tsx` 不会生效。
|
207
|
+
|
200
208
|
:::
|
201
209
|
|
202
210
|
例如以下目录结构:
|
@@ -498,7 +506,39 @@ import Motivation from '@site-docs/components/convention-routing-motivation';
|
|
498
506
|
在使用约定式路由的情况下,务必使用 `@modern-js/runtime/router` 中的 API,不直接使用 React Router 的 API。因为 Modern.js 内部会安装 React Router,如果应用中使用了 React Router 的 API,可能会导致两个版本的 React Router 同时存在,出现不符合预期的行为。
|
499
507
|
|
500
508
|
:::note
|
509
|
+
|
501
510
|
如果应用中必须直接使用 React Router 包的 API,例如部分路由行为被封装在统一的 npm 包中,那应用可以通过设置 [`source.alias`](/configure/app/source/alias),将 `react-router` 和 `react-router-dom` 统一指向项目的依赖,避免两个版本的 React Router 同时存在的问题。
|
511
|
+
|
502
512
|
:::
|
503
513
|
|
514
|
+
2. 关于 `config` 函数和 `init` 函数的说明
|
504
515
|
|
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
|
+
```
|
package/package.json
CHANGED
@@ -15,20 +15,20 @@
|
|
15
15
|
"modern",
|
16
16
|
"modern.js"
|
17
17
|
],
|
18
|
-
"version": "2.67.
|
18
|
+
"version": "2.67.3",
|
19
19
|
"publishConfig": {
|
20
20
|
"registry": "https://registry.npmjs.org/",
|
21
21
|
"access": "public"
|
22
22
|
},
|
23
23
|
"dependencies": {
|
24
24
|
"mermaid": "^11.4.1",
|
25
|
-
"@modern-js/sandpack-react": "2.67.
|
25
|
+
"@modern-js/sandpack-react": "2.67.3"
|
26
26
|
},
|
27
27
|
"devDependencies": {
|
28
28
|
"@rspress/shared": "1.43.11",
|
29
29
|
"@types/fs-extra": "9.0.13",
|
30
30
|
"@types/node": "^16",
|
31
|
-
"classnames": "^2",
|
31
|
+
"classnames": "^2.5.1",
|
32
32
|
"clsx": "^1.2.1",
|
33
33
|
"fs-extra": "^10",
|
34
34
|
"react": "^18.3.1",
|