@modern-js/main-doc 2.17.1 → 2.17.2-alpha.1
Sign up to get free protection for your applications and to get access to all the features.
- package/docs/en/apis/app/hooks/src/index_.mdx +1 -1
- package/docs/en/apis/app/runtime/core/bootstrap.mdx +1 -1
- package/docs/en/components/convention-routing-movitation.mdx +0 -0
- package/docs/en/components/routes-practice.mdx +0 -0
- package/docs/en/configure/app/output/ssg.mdx +3 -1
- package/docs/en/configure/app/source/entries.mdx +2 -0
- package/docs/en/configure/app/tools/tailwindcss.mdx +30 -2
- package/docs/en/configure/app/usage.mdx +35 -0
- package/docs/en/guides/advanced-features/rspack-start.mdx +1 -2
- package/docs/en/guides/advanced-features/ssg.mdx +1 -1
- package/docs/en/guides/advanced-features/testing.mdx +1 -1
- package/docs/en/guides/basic-features/routes.mdx +20 -1
- package/docs/en/guides/concept/entries.mdx +35 -13
- package/docs/en/guides/topic-detail/framework-plugin/hook-list.mdx +4 -4
- package/docs/zh/apis/app/hooks/src/index_.mdx +1 -1
- package/docs/zh/apis/app/runtime/core/bootstrap.mdx +1 -1
- package/docs/zh/components/convention-routing-movitation.mdx +0 -0
- package/docs/zh/components/routes-practice.mdx +0 -0
- package/docs/zh/configure/app/output/ssg.mdx +3 -1
- package/docs/zh/configure/app/source/entries.mdx +2 -0
- package/docs/zh/configure/app/tools/tailwindcss.mdx +31 -3
- package/docs/zh/configure/app/usage.mdx +35 -0
- package/docs/zh/guides/advanced-features/rspack-start.mdx +1 -2
- package/docs/zh/guides/advanced-features/ssg.mdx +1 -1
- package/docs/zh/guides/advanced-features/testing.mdx +1 -1
- package/docs/zh/guides/basic-features/data-fetch.mdx +9 -0
- package/docs/zh/guides/basic-features/routes.mdx +19 -17
- package/docs/zh/guides/concept/entries.mdx +41 -21
- package/docs/zh/guides/topic-detail/framework-plugin/hook-list.mdx +4 -4
- package/package.json +1 -1
@@ -4,7 +4,7 @@ sidebar_position: 4
|
|
4
4
|
---
|
5
5
|
# index.[tj]s
|
6
6
|
|
7
|
-
Entry identifier if App want use custom
|
7
|
+
Entry identifier if App want use custom bootstrap. In most case, [`App.[tj]sx`](/apis/app/hooks/src/app) hook file can already meet our needs.
|
8
8
|
|
9
9
|
When we need to add custom behavior before `bootstrap` or completely take over the webpack entry, we can place `index.[tj]s` in `src/` or entry directory. The following are discussed in two cases:
|
10
10
|
|
@@ -3,7 +3,7 @@ title: bootstrap
|
|
3
3
|
---
|
4
4
|
# bootstrap
|
5
5
|
|
6
|
-
Used to start and mount App, usually without manual calls. This API is only required when using [Custom
|
6
|
+
Used to start and mount App, usually without manual calls. This API is only required when using [Custom Bootstrap](/guides/concept/entries#custom-bootstrap).
|
7
7
|
|
8
8
|
## Usage
|
9
9
|
|
File without changes
|
File without changes
|
@@ -13,6 +13,7 @@ type Entries = Record<
|
|
13
13
|
| {
|
14
14
|
entry: string;
|
15
15
|
disableMount?: boolean;
|
16
|
+
customBootstrap?: string;
|
16
17
|
}
|
17
18
|
>;
|
18
19
|
```
|
@@ -81,6 +82,7 @@ When the value is `Object`, the following properties can be configured:
|
|
81
82
|
|
82
83
|
- `entry`: `string`, entry file path.
|
83
84
|
- `disableMount`: `boolean = false`, turn off the entry-scanning behavior of Modern.js.
|
85
|
+
- `customBootstrap`: `string = ''`, [Custom Bootstrap](/guides/concept/entries#custom-bootstrap) file path。
|
84
86
|
|
85
87
|
```ts title="modern.config.ts"
|
86
88
|
import { defineConfig } from '@modern-js/app-tools';
|
@@ -24,9 +24,37 @@ const tailwind = {
|
|
24
24
|
};
|
25
25
|
```
|
26
26
|
|
27
|
-
|
27
|
+
### Function Type
|
28
28
|
|
29
|
-
When
|
29
|
+
When `tools.tailwindcss`'s type is Function, the default tailwindcss config will be passed in as the first parameter, the config object can be modified directly, or a value can be returned as the final result.
|
30
|
+
|
31
|
+
```ts title="modern.config.ts"
|
32
|
+
export default {
|
33
|
+
tools: {
|
34
|
+
tailwindcss(config) {
|
35
|
+
config.content.push('./some-folder/**/*.{js,ts}');
|
36
|
+
},
|
37
|
+
},
|
38
|
+
};
|
39
|
+
```
|
40
|
+
|
41
|
+
### Object Type
|
42
|
+
|
43
|
+
When `tools.tailwindcss`'s type is `Object`, the config will be shallow merged with default config by `Object.assign`.
|
44
|
+
|
45
|
+
```ts title="modern.config.ts"
|
46
|
+
export default {
|
47
|
+
tools: {
|
48
|
+
tailwindcss: {
|
49
|
+
plugins: [
|
50
|
+
require('@tailwindcss/forms'),
|
51
|
+
require('@tailwindcss/aspect-ratio'),
|
52
|
+
require('@tailwindcss/typography'),
|
53
|
+
],
|
54
|
+
},
|
55
|
+
},
|
56
|
+
};
|
57
|
+
```
|
30
58
|
|
31
59
|
### Limitations
|
32
60
|
|
@@ -43,6 +43,15 @@ export default defineConfig({
|
|
43
43
|
});
|
44
44
|
```
|
45
45
|
|
46
|
+
When using Rspack as the bundler, due to some differences in configuration types between webpack and Rspack, you need to specify `<'rspack'>` generic type for `defineConfig`:
|
47
|
+
|
48
|
+
```diff title=modern.config.ts
|
49
|
+
- export default defineConfig({
|
50
|
+
+ export default defineConfig<'rspack'>({
|
51
|
+
//...
|
52
|
+
});
|
53
|
+
```
|
54
|
+
|
46
55
|
### modern.config.js
|
47
56
|
|
48
57
|
If you are developing a non-TypeScript project, you can use the configuration file in .js format:
|
@@ -242,3 +251,29 @@ const mergedConfig = {
|
|
242
251
|
},
|
243
252
|
};
|
244
253
|
```
|
254
|
+
|
255
|
+
## Configuration Type
|
256
|
+
|
257
|
+
Modern.js exports `AppUserConfig` type, which corresponds to the type of Modern.js configuration object:
|
258
|
+
|
259
|
+
```ts title="modern.config.ts"
|
260
|
+
import type { AppUserConfig } from '@modern-js/app-tools';
|
261
|
+
|
262
|
+
const config: AppUserConfig = {
|
263
|
+
tools: {
|
264
|
+
webpack: {},
|
265
|
+
},
|
266
|
+
};
|
267
|
+
```
|
268
|
+
|
269
|
+
When using Rspack as the bundler, due to some differences in configuration types between webpack and Rspack, you need to specify `<'rspack'>` generic type for `defineConfig`:
|
270
|
+
|
271
|
+
```ts title="modern.config.ts"
|
272
|
+
import type { AppUserConfig } from '@modern-js/app-tools';
|
273
|
+
|
274
|
+
const config: AppUserConfig<'rspack'> = {
|
275
|
+
tools: {
|
276
|
+
rspack: {},
|
277
|
+
},
|
278
|
+
};
|
279
|
+
```
|
@@ -27,8 +27,7 @@ When using Rspack as the bundler, the following Features are temporarily unavail
|
|
27
27
|
|
28
28
|
- Micro Frontend
|
29
29
|
- Storybook Devtool
|
30
|
-
-
|
31
|
-
- Static Site Generation + Conventional Routing
|
30
|
+
- The usage of [useLoader](/guides/basic-features/data-fetch.html) in Client Side Rendering
|
32
31
|
|
33
32
|
:::
|
34
33
|
|
@@ -86,7 +86,7 @@ After executing `pnpm run serve` to start the project, visit the page in the Net
|
|
86
86
|
|
87
87
|
### Self-controlled Routing
|
88
88
|
|
89
|
-
**Self-controlled routing** is a
|
89
|
+
**Self-controlled routing** is a routing through component code, which requires the application to run to obtain accurate routing information. Therefore, the SSG function cannot be used out of the box. At this time, the user needs to inform the Modern.js framework in advance which routes need to enable the SSG.
|
90
90
|
|
91
91
|
For example, there is the following code, which contains multiple routes. When setting `output.ssg` to `true`, only the entry route '/' will be rendered by default:
|
92
92
|
|
@@ -4,7 +4,7 @@ title: Testing
|
|
4
4
|
---
|
5
5
|
# Testing
|
6
6
|
|
7
|
-
Modern.js
|
7
|
+
Modern.js integrates the testing capabilities of [Jest](https://jestjs.io/) by default.
|
8
8
|
|
9
9
|
First need to execute `pnpm run new` enable [unit test/integration test] features:
|
10
10
|
|
@@ -367,6 +367,22 @@ To further improve the user experience and reduce time of loading, Modern.js sup
|
|
367
367
|
- `intent`, the value we recommend for most scenarios, will automatically start loading the corresponding resources and the data defined in the data loader when you mouse over the Link, and will automatically unload it when the mouse is moved away. In our tests, even a direct click can reduce the loading time by about 200ms.
|
368
368
|
- `render`, when the Link component renders, it will load the corresponding resources and the data defined in the data loader.
|
369
369
|
|
370
|
+
#### FAQ
|
371
|
+
|
372
|
+
1. What is the difference between using `render` and not split chunks based on routes?
|
373
|
+
|
374
|
+
- With `render` you can specify which routes are loaded on the first screen, and you can control the rendering so that Link components are rendered only when they are in the visible area.
|
375
|
+
- With `render`, static resources are loaded only when they are idle and do not hog the network with first-screen static resources.
|
376
|
+
- When using server side rendering, data is also prefetched.
|
377
|
+
|
378
|
+
import Motivation from '@site-docs/components/convention-routing-movitation'
|
379
|
+
|
380
|
+
<Motivation/>
|
381
|
+
|
382
|
+
import Practice from '@site-docs-en/components/routes-practice'
|
383
|
+
|
384
|
+
<Practice/>
|
385
|
+
|
370
386
|
## Self-controlled routing
|
371
387
|
|
372
388
|
With `src/App.tsx` as the agreed entry, Modern.js will not do additional operations with multiple routes, developers can use the React Router 6 API for development by themselves, for example:
|
@@ -388,9 +404,12 @@ export default () => {
|
|
388
404
|
|
389
405
|
:::note
|
390
406
|
Modern.js has a series of resource loading and rendering optimizations to the default convention-based routing, and provides out-of-the-box SSR capabilities, when using self-directed routing, need to be packaged by the developer, and it is recommended that developers use convention-based routing.
|
391
|
-
|
392
407
|
:::
|
393
408
|
|
409
|
+
use self-controller routing, if the developer turns off the [`runtime.router`](/configure/app/runtime/router) configuration and uses `react-router-dom` directly, then you need to wrap the `Provider` according to the React Router documentation.
|
410
|
+
|
411
|
+
```tsx title="src/App.tsx"
|
412
|
+
|
394
413
|
## Other
|
395
414
|
|
396
415
|
By default, Modern.js turn on the built-in routing scheme, React Router.
|
@@ -98,27 +98,49 @@ Framework mode refers to the need to use the capabilities of the Modern.js frame
|
|
98
98
|
|
99
99
|
#### Conventional Routing
|
100
100
|
|
101
|
-
If there is a `routes/` directory in the entry, Modern.js will scan the files under `routes/` at startup, and automatically generate client-side routes (react-router) based on file conventions.
|
101
|
+
If there is a `routes/` directory in the entry, Modern.js will scan the files under `routes/` at startup, and automatically generate client-side routes (react-router) based on file conventions. For example:
|
102
102
|
|
103
|
-
|
103
|
+
```bash
|
104
|
+
.
|
105
|
+
├── src
|
106
|
+
│ └── routes
|
107
|
+
│ ├── layout.tsx
|
108
|
+
│ └── page.tsx
|
109
|
+
```
|
104
110
|
|
105
|
-
|
111
|
+
For details, please refer to [routing](/guides/basic-features/routes#conventional-routing).
|
112
|
+
|
113
|
+
#### Self-controlled Routing
|
106
114
|
|
107
115
|
If there is an `App.[jt]sx?` file in the entry, the developer can freely set the client route in this file, or not set the client route.
|
108
116
|
|
109
|
-
|
117
|
+
```tsx
|
118
|
+
import { BrowserRouter, Route, Routes } from '@modern-js/runtime/router';
|
119
|
+
|
120
|
+
export default () => {
|
121
|
+
return (
|
122
|
+
<BrowserRouter>
|
123
|
+
<Routes>
|
124
|
+
<Route index element={<div>index</div>} />
|
125
|
+
<Route path="about" element={<div>about</div>} />
|
126
|
+
</Routes>
|
127
|
+
</BrowserRouter>
|
128
|
+
);
|
129
|
+
};
|
130
|
+
```
|
110
131
|
|
111
|
-
|
132
|
+
For details, please refer to [routing](/guides/basic-features/routes#self-controlled-routing).
|
112
133
|
|
113
|
-
|
134
|
+
#### Custom Bootstrap
|
114
135
|
|
115
|
-
|
116
|
-
import ReactDOM from 'react-dom/client';
|
117
|
-
import { bootstrap } from '@modern-js/runtime';
|
136
|
+
If there is an `index.[jt]sx` file in the entry, and when the file defaults to exporting functions, Modern.js will pass the default bootstrap function as an imported parameter, and replace the default bootstrap with the exported function, so that developers can customize Mounting components to DOM nodes, or adding custom behavior before mounting. E.g:
|
118
137
|
|
119
|
-
|
138
|
+
```tsx
|
139
|
+
export default (App: React.ComponentType, bootstrap: () => void) => {
|
120
140
|
// do something before bootstrap...
|
121
|
-
|
141
|
+
initSomething().then(() => {
|
142
|
+
bootstrap();
|
143
|
+
})
|
122
144
|
};
|
123
145
|
```
|
124
146
|
|
@@ -170,9 +192,9 @@ import App from './App';
|
|
170
192
|
ReactDOM.render(<App />, document.getElementById('root'));
|
171
193
|
```
|
172
194
|
|
173
|
-
Modern.js **not recommended** to use this method, this method loses some capabilities of the framework, such as the `runtime` configuration in the **`modern.config.js` file will no longer take effect**. But this method will be very useful when the project is migrated from other frameworks to Modern.js, such as CRA, or webpack that is manually built by yourself.
|
195
|
+
Modern.js **not recommended** new project to use this method, this method loses some capabilities of the framework, such as the `runtime` configuration in the **`modern.config.js` file will no longer take effect**. But this method will be very useful when the project is migrated from other frameworks to Modern.js, such as CRA, or webpack that is manually built by yourself.
|
174
196
|
|
175
|
-
##
|
197
|
+
## Specify Entry Using Configuration
|
176
198
|
|
177
199
|
Most existing projects are not built according to the directory convention of Modern.js. If you want to change to the directory structure agreed by Modern.js, there will be a certain migration cost.
|
178
200
|
|
@@ -611,7 +611,7 @@ export default (): CliPlugin => ({
|
|
611
611
|
|
612
612
|
This adds a new Script tag to the HTML template.
|
613
613
|
|
614
|
-
|
614
|
+
{/* ## Server
|
615
615
|
|
616
616
|
:::note
|
617
617
|
The Server plugin is currently not fully opened, and the API is not guaranteed to be stable. Use with caution.
|
@@ -691,7 +691,7 @@ export default (): ServerPlugin => ({
|
|
691
691
|
};
|
692
692
|
},
|
693
693
|
});
|
694
|
-
|
694
|
+
```*/}
|
695
695
|
|
696
696
|
## Runtime
|
697
697
|
|
@@ -762,7 +762,7 @@ export default (): Plugin => ({
|
|
762
762
|
});
|
763
763
|
```
|
764
764
|
|
765
|
-
|
765
|
+
{/* ### `provide`
|
766
766
|
|
767
767
|
- Function: Modifies the Elements that need to be rendered.
|
768
768
|
- Execution Stage: Rendering (SSR/CSR).
|
@@ -834,4 +834,4 @@ export default (): Plugin => ({
|
|
834
834
|
};
|
835
835
|
},
|
836
836
|
});
|
837
|
-
```
|
837
|
+
``` */}
|
File without changes
|
File without changes
|
@@ -13,6 +13,7 @@ type Entries = Record<
|
|
13
13
|
| {
|
14
14
|
entry: string;
|
15
15
|
disableMount?: boolean;
|
16
|
+
customBootstrap?: string;
|
16
17
|
}
|
17
18
|
>;
|
18
19
|
```
|
@@ -83,6 +84,7 @@ export default defineConfig({
|
|
83
84
|
|
84
85
|
- `entry`:`string`,入口文件路径。
|
85
86
|
- `disableMount`:`boolean = false`,关闭 Modern.js 自动生成入口代码的行为。
|
87
|
+
- `customBootstrap`: `string = ''`,指定[自定义 Bootstrap](/guides/concept/entries#自定义-bootstrap) 的文件路径。
|
86
88
|
|
87
89
|
```ts title="modern.config.ts"
|
88
90
|
import { defineConfig } from '@modern-js/app-tools';
|
@@ -26,12 +26,40 @@ const tailwind = {
|
|
26
26
|
|
27
27
|
对应 [TailwindCSS](https://tailwindcss.com/docs/configuration) 的配置。
|
28
28
|
|
29
|
-
|
29
|
+
### Function 类型
|
30
30
|
|
31
|
-
|
31
|
+
当 `tools.tailwindcss` 为 Function 类型时,默认配置会作为第一个参数传入,你可以直接修改配置对象,也可以返回一个值作为最终结果:
|
32
|
+
|
33
|
+
```ts title="modern.config.ts"
|
34
|
+
export default {
|
35
|
+
tools: {
|
36
|
+
tailwindcss(config) {
|
37
|
+
config.content.push('./some-folder/**/*.{js,ts}');
|
38
|
+
},
|
39
|
+
},
|
40
|
+
};
|
41
|
+
```
|
42
|
+
|
43
|
+
### Object 类型
|
44
|
+
|
45
|
+
当 `tools.tailwindcss` 的值为 `Object` 类型时,会与默认配置通过 `Object.assign` 浅合并。
|
46
|
+
|
47
|
+
```ts title="modern.config.ts"
|
48
|
+
export default {
|
49
|
+
tools: {
|
50
|
+
tailwindcss: {
|
51
|
+
plugins: [
|
52
|
+
require('@tailwindcss/forms'),
|
53
|
+
require('@tailwindcss/aspect-ratio'),
|
54
|
+
require('@tailwindcss/typography'),
|
55
|
+
],
|
56
|
+
},
|
57
|
+
},
|
58
|
+
};
|
59
|
+
```
|
32
60
|
|
33
61
|
### 限制
|
34
62
|
|
35
63
|
注意,该配置中不允许使用 `theme` 配置项,否则会构建失败。在 Modern.js 中,请使用 [source.designSystem](/configure/app/source/design-system) 作为 `Tailwind CSS Theme` 配置。
|
36
64
|
|
37
|
-
|
65
|
+
其他配置的使用方式和 Tailwind CSS 一致,请参考 [tailwindcss - Configuration](https://tailwindcss.com/docs/configuration)。
|
@@ -43,6 +43,15 @@ export default defineConfig({
|
|
43
43
|
});
|
44
44
|
```
|
45
45
|
|
46
|
+
当你使用 Rspack 作为打包工具时,由于 webpack 和 Rspack 的配置类型存在一些差异,需要为 `defineConfig` 指定 `<'rspack'>` 泛型:
|
47
|
+
|
48
|
+
```diff title=modern.config.ts
|
49
|
+
- export default defineConfig({
|
50
|
+
+ export default defineConfig<'rspack'>({
|
51
|
+
// ...
|
52
|
+
});
|
53
|
+
```
|
54
|
+
|
46
55
|
### modern.config.js
|
47
56
|
|
48
57
|
如果你在开发一个非 TypeScript 项目,可以使用 .js 格式的配置文件:
|
@@ -242,3 +251,29 @@ const mergedConfig = {
|
|
242
251
|
},
|
243
252
|
};
|
244
253
|
```
|
254
|
+
|
255
|
+
## 配置类型定义
|
256
|
+
|
257
|
+
Modern.js 导出了 `AppUserConfig` 类型,对应 Modern.js 配置对象的类型:
|
258
|
+
|
259
|
+
```ts title="modern.config.ts"
|
260
|
+
import type { AppUserConfig } from '@modern-js/app-tools';
|
261
|
+
|
262
|
+
const config: AppUserConfig = {
|
263
|
+
tools: {
|
264
|
+
webpack: {},
|
265
|
+
},
|
266
|
+
};
|
267
|
+
```
|
268
|
+
|
269
|
+
当你使用 Rspack 作为打包工具时,由于 webpack 和 Rspack 的配置类型存在一些差异,需要为 `AppUserConfig` 指定 `<'rspack'>` 泛型:
|
270
|
+
|
271
|
+
```ts title="modern.config.ts"
|
272
|
+
import type { AppUserConfig } from '@modern-js/app-tools';
|
273
|
+
|
274
|
+
const config: AppUserConfig<'rspack'> = {
|
275
|
+
tools: {
|
276
|
+
rspack: {},
|
277
|
+
},
|
278
|
+
};
|
279
|
+
```
|
@@ -86,7 +86,7 @@ export default defineConfig({
|
|
86
86
|
|
87
87
|
### 在自控式路由中使用
|
88
88
|
|
89
|
-
|
89
|
+
**自控式路由**是通过组件代码定义路由,需要应用运行起来才能获取准确的路由信息。因此,无法开箱即用的使用 SSG 功能。此时需要用户提前告知 Modern.js 框架,哪些路由需要开启 SSG 功能。
|
90
90
|
|
91
91
|
例如有以下代码,包含多条路由,设置 `output.ssg` 为 `true` 时,默认只会渲染入口路由即 `/`:
|
92
92
|
|
@@ -339,6 +339,15 @@ export default async (): Promise<ProfileData> => {
|
|
339
339
|
|
340
340
|
4. 在服务端运行时,`loader` 函数会被打包为一个统一的 bundle,所以我们不推荐服务端的代码使用 `__filename` 和 `__dirname`。
|
341
341
|
|
342
|
+
### 常见问题
|
343
|
+
|
344
|
+
1. loader 和 bff 的关系
|
345
|
+
|
346
|
+
在 CSR 项目中,loader 在客户端执行,在 loader 可以直接调用 bff 函数进行接口请求。
|
347
|
+
|
348
|
+
在 SSR 项目中,每个 loader 也是一个服务端接口,我们推荐使用 loader 替代 http method 为 `get` 的 BFF 函数,作为接口层,避免多一层转发和执行。
|
349
|
+
|
350
|
+
|
342
351
|
## useLoader(旧版)
|
343
352
|
|
344
353
|
**`useLoader`** 是 Modern.js 老版本中的 API。该 API 是一个 React Hook,专门提供给 SSR 应用使用,让开发者能同构的在组件中获取数据。
|
@@ -444,7 +444,7 @@ export const init = (context: RuntimeContext) => {
|
|
444
444
|
};
|
445
445
|
```
|
446
446
|
|
447
|
-
###
|
447
|
+
### 预加载
|
448
448
|
|
449
449
|
在约定式路由下, Modern.js 会根据路由,自动地对路由进行分片,当用户访问具体的路由时,会自动加载对应的分片,这样可以有效地减少首屏加载的时间。但这也带来了一个问题,当用户访问一个路由时,如果该路由对应的分片还未加载完成,就会出现白屏的情况。
|
450
450
|
这种情况下你可以通过定义 `loading` 组件,在静态资源加载完成前,展示一个自定义的 `loading` 组件。
|
@@ -462,9 +462,24 @@ export const init = (context: RuntimeContext) => {
|
|
462
462
|
:::
|
463
463
|
|
464
464
|
- `none`, 默认值,不会做 prefetch,没有任何额外的行为。
|
465
|
-
- `intent`,这是我们推荐大多数场景下使用的值,当你把鼠标放在 Link 上时,会自动开始加载对应的分片和 data loader
|
465
|
+
- `intent`,这是我们推荐大多数场景下使用的值,当你把鼠标放在 Link 上时,会自动开始加载对应的分片和 data loader 中定义的数据,当鼠标移开时,会自动取消加载。在我们的测试中,即使是快速点击,也能减少大约 200ms 的加载时间。
|
466
466
|
- `render`,当 Link 组件渲染时,就会加载对应的分片和 data loader 中定义的数据。
|
467
467
|
|
468
|
+
#### 常见问题
|
469
|
+
|
470
|
+
1. 使用 `render` 和不根据路由做静态资源分片的区别?
|
471
|
+
|
472
|
+
- 使用 `render` 可以指定哪些路由在首屏时,进行加载,同时你可以通过对渲染的控制,仅当 Link 组件进入到可视区域时,才对 Link 组件进行渲染。
|
473
|
+
- 使用 `render`,仅在空闲时对静态资源进行加载,不会与首屏静态资源抢占网络。
|
474
|
+
- 在 SSR 场景下,也会对数据进行预取。
|
475
|
+
|
476
|
+
import Motivation from '@site-docs/components/convention-routing-movitation'
|
477
|
+
|
478
|
+
<Motivation/>
|
479
|
+
|
480
|
+
import Practice from '@site-docs/components/routes-practice'
|
481
|
+
|
482
|
+
<Practice/>
|
468
483
|
|
469
484
|
|
470
485
|
## 自控式路由
|
@@ -488,9 +503,10 @@ export default () => {
|
|
488
503
|
|
489
504
|
:::note
|
490
505
|
Modern.js 默认对约定式路由做了一系列资源加载及渲染上的优化,并且提供了开箱即用的 SSR 能力,而这些能力,在使用自控路由时,都需要开发者自行封装,推荐开发者使用约定式路由。
|
491
|
-
|
492
506
|
:::
|
493
507
|
|
508
|
+
使用自控式路由时,如果开发者关闭了 [`runtime.router`](/configure/app/runtime/router) 配置,并直接使用 `react-router-dom`,那还需要根据 React Router 文档自行包裹 `Provider`。
|
509
|
+
|
494
510
|
## 其他路由方案
|
495
511
|
|
496
512
|
默认情况下,Modern.js 会开启内置的路由方案,即 React Router。
|
@@ -513,19 +529,5 @@ export default defineConfig({
|
|
513
529
|
});
|
514
530
|
```
|
515
531
|
|
516
|
-
### 常见问题
|
517
|
-
|
518
|
-
1. 产物中的代码是 es2015+ 的,期望产物中是 es5 的代码
|
519
|
-
|
520
|
-
react-router^6 的目前默认产物是 es2020 的,如果需要产物中是 es5 的代码,可以配置 `source.include`,让 react-router 相关包经过 bundler 编译 :
|
521
|
-
|
522
|
-
```
|
523
|
-
source: {
|
524
|
-
source: {
|
525
|
-
include: [/@remix-run\/router/, /react-router-dom/, /react-router/],
|
526
|
-
}
|
527
|
-
}
|
528
|
-
```
|
529
|
-
|
530
532
|
|
531
533
|
|
@@ -98,36 +98,55 @@ Modern.js 会将与 package.json 文件中 `name` 字段同名的入口作为主
|
|
98
98
|
|
99
99
|
#### 约定式路由
|
100
100
|
|
101
|
-
如果入口中存在 `routes/` 目录,Modern.js 会在启动时扫描 `routes/` 下的文件,基于文件约定,自动生成客户端路由(react-router
|
101
|
+
如果入口中存在 `routes/` 目录,Modern.js 会在启动时扫描 `routes/` 下的文件,基于文件约定,自动生成客户端路由(react-router)。例如:
|
102
102
|
|
103
|
-
|
104
|
-
|
105
|
-
|
103
|
+
```bash
|
104
|
+
.
|
105
|
+
├── src
|
106
|
+
│ └── routes
|
107
|
+
│ ├── layout.tsx
|
108
|
+
│ └── page.tsx
|
109
|
+
```
|
106
110
|
|
107
|
-
|
111
|
+
上述目录中,`layout.tsx` 中导出的组件会作为最外层的组件,`page.tsx` 中导出的组件会作为 `/` 路由的组件。
|
108
112
|
|
109
|
-
详细内容可以参考[路由](/guides/basic-features/routes)。
|
113
|
+
详细内容可以参考[路由](/guides/basic-features/routes#约定式路由)。
|
110
114
|
|
111
|
-
####
|
115
|
+
#### 自控式路由
|
112
116
|
|
113
|
-
如果入口中存在 `
|
117
|
+
如果入口中存在 `App.[jt]sx?` 文件,开发者可以在这个文件中通过代码的方式,设置客户端路由,或者不设置客户端路由。
|
114
118
|
|
115
119
|
```tsx
|
116
|
-
import
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
120
|
+
import { BrowserRouter, Route, Routes } from '@modern-js/runtime/router';
|
121
|
+
|
122
|
+
export default () => {
|
123
|
+
return (
|
124
|
+
<BrowserRouter>
|
125
|
+
<Routes>
|
126
|
+
<Route index element={<div>index</div>} />
|
127
|
+
<Route path="about" element={<div>about</div>} />
|
128
|
+
</Routes>
|
129
|
+
</BrowserRouter>
|
130
|
+
);
|
122
131
|
};
|
123
132
|
```
|
124
133
|
|
125
|
-
|
126
|
-
由于 bootstrap 函数需要兼容 React17 和 React18 的用法,调用 bootstrap 函数时需要手动传入 ReactDOM 参数。
|
134
|
+
详细内容可以参考[路由](/guides/basic-features/routes#自控式路由)。
|
127
135
|
|
128
|
-
|
136
|
+
#### 自定义 Bootstrap
|
137
|
+
|
138
|
+
如果入口中存在 `index.[jt]sx` 文件,并且当文件默认导出函数时,Modern.js 会将默认的 bootstrap 函数作为入参传入,并用导出的函数替代默认的 bootstrap,这样开发者可以自定义将组件挂载到 DOM 节点上,或在挂载前添加自定义行为。例如:
|
139
|
+
|
140
|
+
```tsx
|
141
|
+
export default (App: React.ComponentType, bootstrap: () => void) => {
|
142
|
+
// do something before bootstrap...
|
143
|
+
initSomething().then(() => {
|
144
|
+
bootstrap();
|
145
|
+
})
|
146
|
+
};
|
147
|
+
```
|
129
148
|
|
130
|
-
Modern.js 生成的文件内容如下:
|
149
|
+
此时,Modern.js 生成的文件内容如下:
|
131
150
|
|
132
151
|
```js
|
133
152
|
import React from 'react';
|
@@ -140,13 +159,14 @@ const IS_BROWSER = typeof window !== 'undefined' && window.name !== 'nodejs';
|
|
140
159
|
const MOUNT_ID = 'root';
|
141
160
|
|
142
161
|
let AppWrapper = null;
|
162
|
+
let root = null;
|
143
163
|
|
144
164
|
function render() {
|
145
165
|
AppWrapper = createApp({
|
146
166
|
// runtime 的插件参数...
|
147
167
|
})(App);
|
148
168
|
if (IS_BROWSER) {
|
149
|
-
customBootstrap(AppWrapper);
|
169
|
+
customBootstrap(AppWrapper, () => bootstrap(AppWrapper, MOUNT_ID, root, ReactDOM));
|
150
170
|
}
|
151
171
|
return AppWrapper;
|
152
172
|
}
|
@@ -170,9 +190,9 @@ import App from './App';
|
|
170
190
|
ReactDOM.render(<App />, document.getElementById('root'));
|
171
191
|
```
|
172
192
|
|
173
|
-
Modern.js
|
193
|
+
Modern.js **不推荐**新项目使用这种方式,这种方式丧失了框架的一些能力,如 **`modern.config.js` 文件中的 `runtime` 配置将不会再生效**。但是在项目从其他框架迁移到 Modern.js,例如 CRA,或是自己手动搭建的 webpack 时,这种方式会非常有用。
|
174
194
|
|
175
|
-
##
|
195
|
+
## 使用配置指定入口
|
176
196
|
|
177
197
|
大部分存量项目并不是按照 Modern.js 的目录结构来搭建的。如果要改成 Modern.js 约定的目录结构,会存在一定的迁移成本。
|
178
198
|
|
@@ -611,7 +611,7 @@ export default (): CliPlugin => ({
|
|
611
611
|
|
612
612
|
这样就为 HTML 模版中新增了一个 Script 标签。
|
613
613
|
|
614
|
-
|
614
|
+
{/* ## Server
|
615
615
|
|
616
616
|
:::note
|
617
617
|
目前 Server 插件还未完全开放,API 不保证稳定,使用需谨慎。
|
@@ -692,7 +692,7 @@ export default (): ServerPlugin => ({
|
|
692
692
|
};
|
693
693
|
},
|
694
694
|
});
|
695
|
-
|
695
|
+
```*/}
|
696
696
|
|
697
697
|
## Runtime
|
698
698
|
|
@@ -764,7 +764,7 @@ export default (): Plugin => ({
|
|
764
764
|
});
|
765
765
|
```
|
766
766
|
|
767
|
-
|
767
|
+
{/* ### `provide`
|
768
768
|
|
769
769
|
- 功能:修改需要渲染的 Element
|
770
770
|
- 执行阶段:渲染(SSR/CSR)
|
@@ -836,4 +836,4 @@ export default (): Plugin => ({
|
|
836
836
|
};
|
837
837
|
},
|
838
838
|
});
|
839
|
-
|
839
|
+
```*/}
|