@modern-js/main-doc 0.0.0-nightly-20230918160602 → 0.0.0-nightly-20230920160608
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/commands.mdx +7 -6
- package/docs/en/components/debug-app.mdx +1 -3
- package/docs/en/components/global-proxy-config.mdx +4 -13
- package/docs/en/components/global-proxy.mdx +2 -4
- package/docs/en/components/init-app.mdx +1 -1
- package/docs/en/configure/app/server/base-url.mdx +0 -2
- package/docs/en/guides/basic-features/alias.mdx +1 -1
- package/docs/en/guides/basic-features/css.mdx +1 -1
- package/docs/en/guides/basic-features/data-fetch.mdx +12 -12
- package/docs/en/guides/basic-features/routes.mdx +27 -15
- package/docs/en/guides/get-started/quick-start.mdx +8 -17
- package/docs/en/guides/get-started/upgrade.mdx +10 -4
- package/docs/en/guides/topic-detail/framework-plugin/implement.mdx +2 -0
- package/docs/en/tutorials/first-app/c02-component.mdx +4 -3
- package/docs/zh/apis/app/commands.mdx +7 -6
- package/docs/zh/components/debug-app.mdx +1 -3
- package/docs/zh/components/global-proxy-config.mdx +4 -13
- package/docs/zh/components/global-proxy.mdx +2 -4
- package/docs/zh/configure/app/server/base-url.mdx +0 -2
- package/docs/zh/guides/basic-features/alias.mdx +1 -1
- package/docs/zh/guides/basic-features/css.mdx +1 -1
- package/docs/zh/guides/basic-features/data-fetch.mdx +11 -12
- package/docs/zh/guides/basic-features/routes.mdx +26 -19
- package/docs/zh/guides/get-started/quick-start.mdx +8 -17
- package/docs/zh/guides/get-started/upgrade.mdx +10 -4
- package/docs/zh/guides/topic-detail/framework-plugin/implement.mdx +2 -0
- package/docs/zh/tutorials/first-app/c02-component.mdx +4 -3
- package/package.json +5 -5
@@ -30,7 +30,6 @@ After running `modern dev`, Modern.js will watch source file changes and apply h
|
|
30
30
|
$ modern dev
|
31
31
|
|
32
32
|
info Starting dev server...
|
33
|
-
info App running at:
|
34
33
|
|
35
34
|
> Local: http://localhost:8080/
|
36
35
|
> Network: http://192.168.0.1:8080/
|
@@ -88,12 +87,14 @@ execute `npx modern build --analyze` command, can produce an HTML file that anal
|
|
88
87
|
|
89
88
|
```text
|
90
89
|
Bundle Analyzer saved report to /example/dist/report.html
|
91
|
-
File sizes after production build:
|
92
90
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
91
|
+
info Production file sizes:
|
92
|
+
|
93
|
+
File Size Gzipped
|
94
|
+
dist/static/js/lib-react.09721b5c.js 152.6 kB 49.0 kB
|
95
|
+
dist/html/main/index.html 5.8 kB 2.5 kB
|
96
|
+
dist/static/js/main.3568a38e.js 3.5 kB 1.4 kB
|
97
|
+
dist/static/css/main.03221f72.css 1.4 kB 741 B
|
97
98
|
```
|
98
99
|
|
99
100
|
Open the above HTML file in the browser, you can see the tile diagram of the bundled files, and perform package volume analysis and optimization:
|
@@ -6,12 +6,10 @@ $ pnpm run dev
|
|
6
6
|
> modern dev
|
7
7
|
|
8
8
|
info Starting dev server...
|
9
|
-
|
9
|
+
ready Client compiled in 50ms
|
10
10
|
|
11
11
|
> Local: http://localhost:8080/
|
12
12
|
> Network: http://192.168.0.1:8080/
|
13
|
-
|
14
|
-
Client ✔ done in 76.10ms
|
15
13
|
```
|
16
14
|
|
17
15
|
Open `http://localhost:8000/` in your browser to see the page content.
|
@@ -7,16 +7,9 @@ This option is used to configure a global proxy based on [whistle](https://wprox
|
|
7
7
|
|
8
8
|
Before using this option, you need to install and register the `@modern-js/plugin-proxy` plugin:
|
9
9
|
|
10
|
-
|
11
|
-
# npm
|
12
|
-
npm add @modern-js/plugin-proxy -D
|
13
|
-
|
14
|
-
# yarn
|
15
|
-
yarn add @modern-js/plugin-proxy -D
|
10
|
+
import { PackageManagerTabs } from '@theme';
|
16
11
|
|
17
|
-
|
18
|
-
pnpm add @modern-js/plugin-proxy -D
|
19
|
-
```
|
12
|
+
<PackageManagerTabs command="add @modern-js/plugin-proxy -D" />
|
20
13
|
|
21
14
|
After the installation, please register the plugin in the `modern.config.ts` file:
|
22
15
|
|
@@ -75,13 +68,11 @@ module.exports = {
|
|
75
68
|
Execute `dev`, when the prompt is as follows, the proxy server starts successfully:
|
76
69
|
|
77
70
|
```bash
|
78
|
-
App running at:
|
79
|
-
|
80
71
|
Local: http://localhost:8080/
|
81
72
|
Network: http://192.168.0.1:8080/
|
82
73
|
|
83
|
-
|
84
|
-
|
74
|
+
info Starting proxy server.....
|
75
|
+
success Proxy server start on localhost:8899
|
85
76
|
```
|
86
77
|
|
87
78
|
Access the `localhost:8899` to view the Network and configure proxy rules on the UI interface:
|
@@ -11,13 +11,11 @@ Specific proxy rules can be set via the [`dev.proxy`](/configure/app/dev/proxy)
|
|
11
11
|
After installing the proxy plugin and configuring the proxy rules, run the `pnpm run dev` command:
|
12
12
|
|
13
13
|
```bash
|
14
|
-
App running at:
|
15
|
-
|
16
14
|
Local: http://localhost:8080/
|
17
15
|
Network: http://192.168.0.1:8080/
|
18
16
|
|
19
|
-
|
20
|
-
|
17
|
+
info Starting proxy server.....
|
18
|
+
success Proxy server start on localhost:8899
|
21
19
|
```
|
22
20
|
|
23
21
|
You can see that the proxy server has started successfully in the console.
|
@@ -14,7 +14,7 @@ After create the project, Modern.js will automatically install dependencies and
|
|
14
14
|
[INFO] git repository has been automatically created
|
15
15
|
[INFO] Success!
|
16
16
|
You can run the following command in the directory of the new project:
|
17
|
-
pnpm run dev # Starting
|
17
|
+
pnpm run dev # Starting dev server
|
18
18
|
pnpm run build # Build the app for production
|
19
19
|
pnpm run serve # Preview the production build locally
|
20
20
|
pnpm run lint # Run ESLint and automatically fix problems
|
@@ -1,10 +1,11 @@
|
|
1
1
|
---
|
2
|
-
|
2
|
+
title: Data Fetching
|
3
|
+
sidebar_position: 3
|
3
4
|
---
|
4
5
|
|
5
6
|
# Data Fetching
|
6
7
|
|
7
|
-
Modern.js provides out-of-the-box data fetching capabilities,
|
8
|
+
Modern.js provides out-of-the-box data fetching capabilities,developers can fetch data in the project through these APIs.
|
8
9
|
|
9
10
|
It should be noted that these APIs do not help applications initiate requests, but rather help developers better manage data and improve project performance.
|
10
11
|
|
@@ -13,7 +14,7 @@ It should be noted that these APIs do not help applications initiate requests, b
|
|
13
14
|
Modern.js recommends using [conventional routing](/guides/basic-features/routes) for routing management. Through Modern.js's [conventional (nested) routing](/guides/basic-features/routes#conventional-routing), each routing component (`layout.ts` or `page.ts`) can have a same-named `loader` file. The `loader` file needs to export a function that will be executed before the component is rendered to provide data for the routing component.
|
14
15
|
|
15
16
|
:::info
|
16
|
-
Modern.js v1 supports fetching data via [useLoader](
|
17
|
+
Modern.js v1 supports fetching data via [useLoader](</guides/basic-features/data-fetch.html#useloader-(old-version)>), which is no longer the recommended usage. We do not recommend mixing the two except during the migration process.
|
17
18
|
|
18
19
|
:::
|
19
20
|
|
@@ -216,25 +217,24 @@ If you want to get the data returned by the `loader` in `entry1/routes/layout.ts
|
|
216
217
|
|
217
218
|
:::info
|
218
219
|
This feature is currently experimental and the API may change in the future.
|
219
|
-
Currently only supports CSR, please look forward to Streaming SSR.
|
220
220
|
:::
|
221
221
|
|
222
222
|
Create `user/layout.loader.ts` and add the following code:
|
223
223
|
|
224
224
|
```ts title="routes/user/layout.loader.ts"
|
225
|
-
import { defer } from
|
225
|
+
import { defer } from '@modern-js/runtime/router';
|
226
226
|
|
227
227
|
const loader = () =>
|
228
|
-
defer({
|
229
|
-
|
228
|
+
defer({
|
229
|
+
userInfo: new Promise(resolve => {
|
230
230
|
setTimeout(() => {
|
231
231
|
resolve({
|
232
232
|
age: 1,
|
233
|
-
name: 'user layout'
|
234
|
-
})
|
235
|
-
}, 1000)
|
236
|
-
})
|
237
|
-
})
|
233
|
+
name: 'user layout',
|
234
|
+
});
|
235
|
+
}, 1000);
|
236
|
+
}),
|
237
|
+
});
|
238
238
|
|
239
239
|
export default loader;
|
240
240
|
```
|
@@ -1,5 +1,5 @@
|
|
1
1
|
---
|
2
|
-
sidebar_position:
|
2
|
+
sidebar_position: 2
|
3
3
|
---
|
4
4
|
|
5
5
|
# Routing
|
@@ -140,6 +140,7 @@ To simplify the introduction of the relationship between `<Layout>` and `<Outlet
|
|
140
140
|
</UserLayout>
|
141
141
|
</Layout>
|
142
142
|
```
|
143
|
+
|
143
144
|
In summary, if there is a `layout.tsx` file under the sub-route's file directory, the `<Outlet>` in the parent `layout.tsx` will represent the `layout.tsx` in the sub-route file directory. Otherwise, it will represent the `page.tsx` in the sub-route file directory.
|
144
145
|
|
145
146
|
#### Page
|
@@ -148,12 +149,12 @@ All routes should end with the `<Page>` component. If the developer introduces t
|
|
148
149
|
|
149
150
|
#### Config
|
150
151
|
|
151
|
-
Each `Layout
|
152
|
+
Each `Layout`,`$` or `Page` file can define its own `config` file, such as `page.config.ts`. In this file, we have an conventinal on a named export called `handle`, which you can define any properties:
|
152
153
|
|
153
154
|
```ts title="routes/blog/page.config.ts"
|
154
155
|
export const handle = {
|
155
|
-
breadcrumbName: 'profile'
|
156
|
-
}
|
156
|
+
breadcrumbName: 'profile',
|
157
|
+
};
|
157
158
|
```
|
158
159
|
|
159
160
|
These properties as defined are available via the [`useMatches`](https://reactrouter.com/en/main/hooks/use-matches) hook:
|
@@ -161,12 +162,11 @@ These properties as defined are available via the [`useMatches`](https://reactro
|
|
161
162
|
```ts title="routes/layout.ts"
|
162
163
|
export default () => {
|
163
164
|
const matches = useMatches;
|
164
|
-
const breadcrumbs = matches.map(
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
}
|
165
|
+
const breadcrumbs = matches.map(
|
166
|
+
matchedRoute => matchedRoute?.handle?.breadcrumbName,
|
167
|
+
);
|
168
|
+
return <Breadcrumb names={breadcrumbs}></Breadcrumb>;
|
169
|
+
};
|
170
170
|
```
|
171
171
|
|
172
172
|
### Dynamic Routing
|
@@ -227,6 +227,7 @@ For example, the following directory structure:
|
|
227
227
|
```
|
228
228
|
|
229
229
|
When accessing any path that does not match(For example `/blog/a`), the `routes/$.tsx` component will be rendered, because there is `layout.tsx` here, the rendered UI is as follows.
|
230
|
+
|
230
231
|
```tsx
|
231
232
|
<RootLayout>
|
232
233
|
<BlogLayout>
|
@@ -364,6 +365,17 @@ export default () => {
|
|
364
365
|
};
|
365
366
|
```
|
366
367
|
|
368
|
+
If you want to redirect in a component,you can navigate by `useNavigate` hook, for example:
|
369
|
+
|
370
|
+
```ts title="routes/user/page.ts"
|
371
|
+
import { useNavigate } from '@modern-js/runtime/router';
|
372
|
+
|
373
|
+
export default () => {
|
374
|
+
const navigate = useNavigate();
|
375
|
+
navigate('/login');
|
376
|
+
};
|
377
|
+
```
|
378
|
+
|
367
379
|
### ErrorBoundary
|
368
380
|
|
369
381
|
In each directory under `routes/`, developers can also define an `error.tsx` file that exports an `<ErrorBoundary>` component by default.
|
@@ -391,7 +403,6 @@ export const ErrorBoundary = () => {
|
|
391
403
|
|
392
404
|
In each root `Layout` component (`routes/layout.ts`), you can dynamically define runtime configuration:
|
393
405
|
|
394
|
-
|
395
406
|
```tsx title="src/routes/layout.tsx"
|
396
407
|
// Define runtime config
|
397
408
|
import type { AppConfig } from '@modern-js/runtime';
|
@@ -489,6 +500,7 @@ To further improve the user experience and reduce loading time, Modern.js suppor
|
|
489
500
|
```
|
490
501
|
|
491
502
|
:::info
|
503
|
+
|
492
504
|
- This feature is currently only supported in Webpack projects and not yet supported in Rspack projects.
|
493
505
|
- Preloading data currently only preloads the data returned by the [Data Loader](/guides/basic-features/data-fetch) in SSR projects.
|
494
506
|
|
@@ -508,13 +520,13 @@ The `prefetch` attribute has three optional values:
|
|
508
520
|
- By using `render`, static assets will only be loaded when the system is idle, and will not compete with the static assets of the initial screen for network assets.
|
509
521
|
- In the SSR scenario, data will also be pre-fetched.
|
510
522
|
|
511
|
-
import Motivation from '@site-docs-en/components/convention-routing-motivation'
|
523
|
+
import Motivation from '@site-docs-en/components/convention-routing-motivation';
|
512
524
|
|
513
|
-
<Motivation/>
|
525
|
+
<Motivation />
|
514
526
|
|
515
|
-
import Practice from '@site-docs-en/components/routes-practice'
|
527
|
+
import Practice from '@site-docs-en/components/routes-practice';
|
516
528
|
|
517
|
-
<Practice/>
|
529
|
+
<Practice />
|
518
530
|
|
519
531
|
## Self-controlled Routing
|
520
532
|
|
@@ -91,23 +91,15 @@ $ pnpm run build
|
|
91
91
|
|
92
92
|
> modern build
|
93
93
|
|
94
|
-
info
|
95
|
-
|
96
|
-
info
|
94
|
+
info Staring production build...
|
95
|
+
ready Client compiled in 50ms
|
96
|
+
info Production file sizes:
|
97
97
|
|
98
98
|
File Size Gzipped
|
99
|
-
dist/static/js/lib-
|
100
|
-
dist/
|
101
|
-
dist/static/js/
|
102
|
-
dist/static/
|
103
|
-
dist/html/main/index.html 5.84 kB 2.57 kB
|
104
|
-
dist/static/js/main.3568a38e.js 3.57 kB 1.44 kB
|
105
|
-
dist/static/css/async/304.c3c481a5.css 2.62 kB 874 B
|
106
|
-
dist/asset-manifest.json 1.48 kB 349 B
|
107
|
-
dist/static/js/async/304.c45706bc.js 1.4 kB 575 B
|
108
|
-
dist/static/js/async/509.fcb06e14.js 283 B 230 B
|
109
|
-
|
110
|
-
Client ✔ done in 3.57s
|
99
|
+
dist/static/js/lib-react.09721b5c.js 152.6 kB 49.0 kB
|
100
|
+
dist/html/main/index.html 5.8 kB 2.5 kB
|
101
|
+
dist/static/js/main.3568a38e.js 3.5 kB 1.4 kB
|
102
|
+
dist/static/css/main.03221f72.css 1.4 kB 741 B
|
111
103
|
```
|
112
104
|
|
113
105
|
By default, the build artifacts are generated in `dist/`, with the following directory structure:
|
@@ -135,8 +127,7 @@ $ pnpm run serve
|
|
135
127
|
|
136
128
|
> modern serve
|
137
129
|
|
138
|
-
Starting
|
139
|
-
info App running at:
|
130
|
+
info Starting production server...
|
140
131
|
|
141
132
|
> Local: http://localhost:8080/
|
142
133
|
> Network: http://192.168.0.1:8080/
|
@@ -8,11 +8,13 @@ sidebar_position: 3
|
|
8
8
|
|
9
9
|
Modern.js provides the `upgrade` command to support upgrading the project to the latest version of Modern.js.
|
10
10
|
|
11
|
-
Run `
|
11
|
+
Run the `upgrade` script in the project:
|
12
12
|
|
13
|
-
|
14
|
-
|
13
|
+
import { PackageManagerTabs } from '@theme';
|
14
|
+
|
15
|
+
<PackageManagerTabs command="run upgrade" />
|
15
16
|
|
17
|
+
```bash
|
16
18
|
> modern upgrade
|
17
19
|
|
18
20
|
[INFO] [Project Type]: Web App
|
@@ -22,11 +24,15 @@ $ pnpm run upgrade
|
|
22
24
|
|
23
25
|
You can see that the dependencies in the project's `package.json` have been updated to the latest version.
|
24
26
|
|
27
|
+
:::tip
|
28
|
+
If the `upgrade` command is not declared in the project's `package.json`, you can execute `npx modern upgrade` as an equivalent alternative.
|
29
|
+
:::
|
30
|
+
|
25
31
|
## Upgrade to a specified version
|
26
32
|
|
27
33
|
All packages of Modern.js are currently released with a **uniform version number**.
|
28
34
|
|
29
|
-
import ReleaseNote from
|
35
|
+
import ReleaseNote from '@site-docs-en/components/release-note';
|
30
36
|
|
31
37
|
<ReleaseNote />
|
32
38
|
|
@@ -170,6 +170,8 @@ export const myPlugin = (): CliPlugin => ({
|
|
170
170
|
});
|
171
171
|
```
|
172
172
|
|
173
|
+
Note that the setup function of the next plugin is not executed until the async setup function of the current plugin has finished. Therefore, you should avoid performing time-consuming asynchronous operations in the setup function to avoid slowing down the startup performance of the CLI.
|
174
|
+
|
173
175
|
## Adding Plugins
|
174
176
|
|
175
177
|
Custom plugins can be used by following the instructions in the [plugins](/configure/app/plugins) section of the documentation. Below is the recommended way to implement plugins in Modern.js.
|
@@ -1,6 +1,7 @@
|
|
1
1
|
---
|
2
2
|
title: Add UI Components
|
3
3
|
---
|
4
|
+
|
4
5
|
# Add UI Components
|
5
6
|
|
6
7
|
In the previous chapter, we learned how to initialize a project and use configuration to modify the default behavior of Modern.js.
|
@@ -9,9 +10,9 @@ In this chapter, we continue to use the project code of the previous chapter and
|
|
9
10
|
|
10
11
|
In order to do better UI display and interaction, we introduce the component library [Antd](https://ant.design/index-cn) to develop, and use the `<List>` component instead of the primitive list. Add dependency first:
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
import { PackageManagerTabs } from '@theme';
|
14
|
+
|
15
|
+
<PackageManagerTabs command="add antd" />
|
15
16
|
|
16
17
|
Modify `src/routes/page.tsx` to import components at the top:
|
17
18
|
|
@@ -30,7 +30,6 @@ Options:
|
|
30
30
|
$ modern dev
|
31
31
|
|
32
32
|
info Starting dev server...
|
33
|
-
info App running at:
|
34
33
|
|
35
34
|
> Local: http://localhost:8080/
|
36
35
|
> Network: http://192.168.0.1:8080/
|
@@ -88,12 +87,14 @@ Options:
|
|
88
87
|
|
89
88
|
```
|
90
89
|
Bundle Analyzer saved report to /example/dist/report.html
|
91
|
-
File sizes after production build:
|
92
90
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
91
|
+
info Production file sizes:
|
92
|
+
|
93
|
+
File Size Gzipped
|
94
|
+
dist/static/js/lib-react.09721b5c.js 152.6 kB 49.0 kB
|
95
|
+
dist/html/main/index.html 5.8 kB 2.5 kB
|
96
|
+
dist/static/js/main.3568a38e.js 3.5 kB 1.4 kB
|
97
|
+
dist/static/css/main.03221f72.css 1.4 kB 741 B
|
97
98
|
```
|
98
99
|
|
99
100
|
手动在浏览器中打开上述 HTML 文件,可以看到打包产物的瓦片图,并进行包体积分析和优化:
|
@@ -6,12 +6,10 @@ $ pnpm run dev
|
|
6
6
|
> modern dev
|
7
7
|
|
8
8
|
info Starting dev server...
|
9
|
-
|
9
|
+
ready Client compiled in 50ms
|
10
10
|
|
11
11
|
> Local: http://localhost:8080/
|
12
12
|
> Network: http://192.168.0.1:8080/
|
13
|
-
|
14
|
-
Client ✔ done in 76.10ms
|
15
13
|
```
|
16
14
|
|
17
15
|
在浏览器中打开 `http://localhost:8000/`,可以看到页面内容。
|
@@ -7,16 +7,9 @@
|
|
7
7
|
|
8
8
|
使用该选项前,你需要提前安装和注册 `@modern-js/plugin-proxy` 插件:
|
9
9
|
|
10
|
-
|
11
|
-
# npm
|
12
|
-
npm add @modern-js/plugin-proxy -D
|
13
|
-
|
14
|
-
# yarn
|
15
|
-
yarn add @modern-js/plugin-proxy -D
|
10
|
+
import { PackageManagerTabs } from '@theme';
|
16
11
|
|
17
|
-
|
18
|
-
pnpm add @modern-js/plugin-proxy -D
|
19
|
-
```
|
12
|
+
<PackageManagerTabs command="add @modern-js/plugin-proxy -D" />
|
20
13
|
|
21
14
|
安装完成后,在 `modern.config.ts` 文件中注册插件:
|
22
15
|
|
@@ -75,13 +68,11 @@ module.exports = {
|
|
75
68
|
执行 `dev`, 提示如下时,即代理服务器启动成功:
|
76
69
|
|
77
70
|
```bash
|
78
|
-
App running at:
|
79
|
-
|
80
71
|
Local: http://localhost:8080/
|
81
72
|
Network: http://192.168.0.1:8080/
|
82
73
|
|
83
|
-
|
84
|
-
|
74
|
+
info Starting proxy server.....
|
75
|
+
success Proxy server start on localhost:8899
|
85
76
|
```
|
86
77
|
|
87
78
|
访问 `localhost:8899`, 可以在 UI 界面上查看 Network 以及配置代理规则:
|
@@ -11,13 +11,11 @@ Modern.js 提供了开箱即用的全局代理插件 `@modern-js/plugin-proxy`
|
|
11
11
|
安装代理插件并配置代理规则后, 执行 `pnpm run dev` 命令:
|
12
12
|
|
13
13
|
```bash
|
14
|
-
App running at:
|
15
|
-
|
16
14
|
Local: http://localhost:8080/
|
17
15
|
Network: http://192.168.0.1:8080/
|
18
16
|
|
19
|
-
|
20
|
-
|
17
|
+
info Starting proxy server.....
|
18
|
+
success Proxy server start on localhost:8899
|
21
19
|
```
|
22
20
|
|
23
21
|
在控制台中可以看到代理服务器成功启动。
|
@@ -1,10 +1,11 @@
|
|
1
1
|
---
|
2
|
-
|
2
|
+
title: 数据获取
|
3
|
+
sidebar_position: 3
|
3
4
|
---
|
4
5
|
|
5
6
|
# 数据获取
|
6
7
|
|
7
|
-
Modern.js 中提供了开箱即用的数据获取能力,开发者可以通过这些 API
|
8
|
+
Modern.js 中提供了开箱即用的数据获取能力,开发者可以通过这些 API,在项目中获取数据。
|
8
9
|
|
9
10
|
需要注意的是,这些 API 并不帮助应用发起请求,而是帮助开发者更好地管理数据,提升项目的性能。
|
10
11
|
|
@@ -216,26 +217,25 @@ export default function UserLayout() {
|
|
216
217
|
|
217
218
|
:::info
|
218
219
|
此功能目前是实验性质,后续 API 可能有调整。
|
219
|
-
目前仅支持 CSR,敬请期待 Streaming SSR。
|
220
220
|
|
221
221
|
:::
|
222
222
|
|
223
223
|
创建 `user/layout.loader.ts`,并添加以下代码:
|
224
224
|
|
225
225
|
```ts title="routes/user/layout.loader.ts"
|
226
|
-
import { defer } from
|
226
|
+
import { defer } from '@modern-js/runtime/router';
|
227
227
|
|
228
228
|
const loader = () =>
|
229
|
-
defer({
|
230
|
-
|
229
|
+
defer({
|
230
|
+
userInfo: new Promise(resolve => {
|
231
231
|
setTimeout(() => {
|
232
232
|
resolve({
|
233
233
|
age: 1,
|
234
|
-
name: 'user layout'
|
235
|
-
})
|
236
|
-
}, 1000)
|
237
|
-
})
|
238
|
-
})
|
234
|
+
name: 'user layout',
|
235
|
+
});
|
236
|
+
}, 1000);
|
237
|
+
}),
|
238
|
+
});
|
239
239
|
|
240
240
|
export default loader;
|
241
241
|
```
|
@@ -346,7 +346,6 @@ export default async (): Promise<ProfileData> => {
|
|
346
346
|
|
347
347
|
在 SSR 项目中,每个 `loader` 也是一个服务端接口,我们推荐使用 `loader` 替代 http method 为 `get` 的 BFF 函数,作为接口层,避免多一层转发和执行。
|
348
348
|
|
349
|
-
|
350
349
|
## useLoader(旧版)
|
351
350
|
|
352
351
|
**`useLoader`** 是 Modern.js 老版本中的 API。该 API 是一个 React Hook,专门提供给 SSR 应用使用,让开发者能同构的在组件中获取数据。
|
@@ -1,5 +1,5 @@
|
|
1
1
|
---
|
2
|
-
sidebar_position:
|
2
|
+
sidebar_position: 2
|
3
3
|
---
|
4
4
|
|
5
5
|
# 路由方案
|
@@ -151,13 +151,13 @@ export default () => {
|
|
151
151
|
|
152
152
|
#### Config
|
153
153
|
|
154
|
-
每个 `Layout
|
154
|
+
每个 `Layout`, `$` 或 `Page` 文件都可以定义一个自己的 `config` 文件,如 `page.config.ts`,该文件中我们约定了一个具名导出 `handle`,
|
155
155
|
这个字段中你可以定义任意属性:
|
156
156
|
|
157
157
|
```ts title="routes/page.config.ts"
|
158
158
|
export const handle = {
|
159
|
-
breadcrumbName: 'profile'
|
160
|
-
}
|
159
|
+
breadcrumbName: 'profile',
|
160
|
+
};
|
161
161
|
```
|
162
162
|
|
163
163
|
定义的这些属性可以通过 [`useMatches`](https://reactrouter.com/en/main/hooks/use-matches) hook 获取:
|
@@ -165,15 +165,13 @@ export const handle = {
|
|
165
165
|
```ts title="routes/layout.ts"
|
166
166
|
export default () => {
|
167
167
|
const matches = useMatches;
|
168
|
-
const breadcrumbs = matches.map(
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
}
|
168
|
+
const breadcrumbs = matches.map(
|
169
|
+
matchedRoute => matchedRoute?.handle?.breadcrumbName,
|
170
|
+
);
|
171
|
+
return <Breadcrumb names={breadcrumbs}></Breadcrumb>;
|
172
|
+
};
|
174
173
|
```
|
175
174
|
|
176
|
-
|
177
175
|
### 动态路由
|
178
176
|
|
179
177
|
通过 `[]` 命名的文件目录,生成的路由会作为动态路由。例如以下文件目录:
|
@@ -233,6 +231,7 @@ export default () => {
|
|
233
231
|
```
|
234
232
|
|
235
233
|
当访问任何匹配不到的路径时(如 `/blog/a`),都会渲染 `routes/$.tsx` 组件,因为这里有 `layout.tsx`,渲染的 UI 如下:
|
234
|
+
|
236
235
|
```tsx
|
237
236
|
<RootLayout>
|
238
237
|
<BlogLayout>
|
@@ -254,7 +253,6 @@ params['*']; // => 'aaa/bbb'
|
|
254
253
|
|
255
254
|
`$.tsx` 可以加入到 `routes` 目录下的任意目录中,一个常见的使用示例是添加 `routes/$.tsx` 文件去定制任意层级的 404 内容。
|
256
255
|
|
257
|
-
|
258
256
|
### 无路径布局
|
259
257
|
|
260
258
|
当目录名以 \_\_ 开头时,对应的目录名不会转换为实际的路由路径,例如以下文件目录:
|
@@ -368,6 +366,17 @@ export default () => {
|
|
368
366
|
};
|
369
367
|
```
|
370
368
|
|
369
|
+
在组件内做重定向,则可以通过 `useNavigate` hook,示例如下:
|
370
|
+
|
371
|
+
```ts title="routes/user/page.ts"
|
372
|
+
import { useNavigate } from '@modern-js/runtime/router';
|
373
|
+
|
374
|
+
export default () => {
|
375
|
+
const navigate = useNavigate();
|
376
|
+
navigate('/login');
|
377
|
+
};
|
378
|
+
```
|
379
|
+
|
371
380
|
### 错误处理
|
372
381
|
|
373
382
|
`routes/` 下每一层目录中,开发者同样可以定义一个 `error.tsx` 文件,默认导出一个 `<ErrorBoundary>` 组件。
|
@@ -493,6 +502,7 @@ export const init = (context: RuntimeContext) => {
|
|
493
502
|
```
|
494
503
|
|
495
504
|
:::info
|
505
|
+
|
496
506
|
- 该功能目前仅在 Webpack 项目中支持,Rspack 项目暂不支持。
|
497
507
|
- 对数据的预加载目前只会预加载 SSR 项目中 [Data Loader](/guides/basic-features/data-fetch) 中返回的数据。
|
498
508
|
|
@@ -512,14 +522,13 @@ export const init = (context: RuntimeContext) => {
|
|
512
522
|
- 使用 `render`,仅在空闲时对静态资源进行加载,不会与首屏静态资源抢占网络。
|
513
523
|
- 在 SSR 场景下,也会对数据进行预取。
|
514
524
|
|
515
|
-
import Motivation from '@site-docs/components/convention-routing-motivation'
|
516
|
-
|
517
|
-
<Motivation/>
|
525
|
+
import Motivation from '@site-docs/components/convention-routing-motivation';
|
518
526
|
|
519
|
-
|
527
|
+
<Motivation />
|
520
528
|
|
521
|
-
|
529
|
+
import Practice from '@site-docs/components/routes-practice';
|
522
530
|
|
531
|
+
<Practice />
|
523
532
|
|
524
533
|
## 自控式路由
|
525
534
|
|
@@ -569,5 +578,3 @@ export default defineConfig({
|
|
569
578
|
```
|
570
579
|
|
571
580
|
如上述配置, 如果关闭了 [`runtime.router`](/configure/app/runtime/router) 配置,并直接使用 `react-router-dom` 进行项目路由管理时,还需要根据 React Router 文档自行包裹 `Provider`。
|
572
|
-
|
573
|
-
|
@@ -91,23 +91,15 @@ $ pnpm run build
|
|
91
91
|
|
92
92
|
> modern build
|
93
93
|
|
94
|
-
info
|
95
|
-
|
96
|
-
info
|
94
|
+
info Staring production build...
|
95
|
+
ready Client compiled in 50ms
|
96
|
+
info Production file sizes:
|
97
97
|
|
98
98
|
File Size Gzipped
|
99
|
-
dist/static/js/lib-
|
100
|
-
dist/
|
101
|
-
dist/static/js/
|
102
|
-
dist/static/
|
103
|
-
dist/html/main/index.html 5.84 kB 2.57 kB
|
104
|
-
dist/static/js/main.3568a38e.js 3.57 kB 1.44 kB
|
105
|
-
dist/static/css/async/304.c3c481a5.css 2.62 kB 874 B
|
106
|
-
dist/asset-manifest.json 1.48 kB 349 B
|
107
|
-
dist/static/js/async/304.c45706bc.js 1.4 kB 575 B
|
108
|
-
dist/static/js/async/509.fcb06e14.js 283 B 230 B
|
109
|
-
|
110
|
-
Client ✔ done in 3.57s
|
99
|
+
dist/static/js/lib-react.09721b5c.js 152.6 kB 49.0 kB
|
100
|
+
dist/html/main/index.html 5.8 kB 2.5 kB
|
101
|
+
dist/static/js/main.3568a38e.js 3.5 kB 1.4 kB
|
102
|
+
dist/static/css/main.03221f72.css 1.4 kB 741 B
|
111
103
|
```
|
112
104
|
|
113
105
|
构建产物默认生成到 `dist/`,目录结构如下:
|
@@ -135,8 +127,7 @@ $ pnpm run serve
|
|
135
127
|
|
136
128
|
> modern serve
|
137
129
|
|
138
|
-
Starting
|
139
|
-
info App running at:
|
130
|
+
Starting production server...
|
140
131
|
|
141
132
|
> Local: http://localhost:8080/
|
142
133
|
> Network: http://192.168.0.1:8080/
|
@@ -8,11 +8,13 @@ sidebar_position: 3
|
|
8
8
|
|
9
9
|
Modern.js 提供了 `upgrade` 命令支持项目升级到最新的 Modern.js 版本。
|
10
10
|
|
11
|
-
在项目中执行 `
|
11
|
+
在项目中执行 `upgrade` 命令:
|
12
12
|
|
13
|
-
|
14
|
-
|
13
|
+
import { PackageManagerTabs } from '@theme';
|
14
|
+
|
15
|
+
<PackageManagerTabs command="run upgrade" />
|
15
16
|
|
17
|
+
```bash
|
16
18
|
> modern upgrade
|
17
19
|
|
18
20
|
[INFO] [项目类型]: Web 应用
|
@@ -22,11 +24,15 @@ $ pnpm run upgrade
|
|
22
24
|
|
23
25
|
可以看到项目 `package.json` 中的依赖已经更改到最新。
|
24
26
|
|
27
|
+
:::tip
|
28
|
+
如果项目的 package.json 中没有声明 upgrade 命令,你可以执行 `npx modern upgrade`,效果是等价的。
|
29
|
+
:::
|
30
|
+
|
25
31
|
## 指定版本升级
|
26
32
|
|
27
33
|
Modern.js 所有的官方包目前都使用**统一版本号**进行发布。
|
28
34
|
|
29
|
-
import ReleaseNote from
|
35
|
+
import ReleaseNote from '@site-docs/components/release-note';
|
30
36
|
|
31
37
|
<ReleaseNote />
|
32
38
|
|
@@ -177,6 +177,8 @@ export const myPlugin = (): CliPlugin => ({
|
|
177
177
|
});
|
178
178
|
```
|
179
179
|
|
180
|
+
注意,只有当前插件的 setup 异步函数执行完毕,才会继续执行下一个插件的 setup 函数。因此,你需要避免在 setup 函数中进行耗时过长的异步操作,防止影响 CLI 启动性能。
|
181
|
+
|
180
182
|
## 添加插件
|
181
183
|
|
182
184
|
自定义插件的使用方式可以查看:[plugins (框架插件)](/configure/app/plugins)。下面会介绍 Modern.js 中推荐的插件实现方法。
|
@@ -1,6 +1,7 @@
|
|
1
1
|
---
|
2
2
|
title: 编写 UI 组件
|
3
3
|
---
|
4
|
+
|
4
5
|
# 编写 UI 组件
|
5
6
|
|
6
7
|
上一章节中,我们学习了如何初始化项目,并使用配置修改 Modern.js 的默认行为。
|
@@ -9,9 +10,9 @@ title: 编写 UI 组件
|
|
9
10
|
|
10
11
|
为了做更好的 UI 展示和交互,我们引入组件库 [Antd](https://ant.design/index-cn) 来开发,使用 `<List>` 组件来代替原始的列表。先添加依赖:
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
import { PackageManagerTabs } from '@theme';
|
14
|
+
|
15
|
+
<PackageManagerTabs command="add antd" />
|
15
16
|
|
16
17
|
修改 `src/routes/page.tsx`,在顶部导入组件:
|
17
18
|
|
package/package.json
CHANGED
@@ -15,17 +15,17 @@
|
|
15
15
|
"modern",
|
16
16
|
"modern.js"
|
17
17
|
],
|
18
|
-
"version": "0.0.0-nightly-
|
18
|
+
"version": "0.0.0-nightly-20230920160608",
|
19
19
|
"publishConfig": {
|
20
20
|
"registry": "https://registry.npmjs.org/",
|
21
21
|
"access": "public",
|
22
22
|
"provenance": true
|
23
23
|
},
|
24
24
|
"dependencies": {
|
25
|
-
"@modern-js/sandpack-react": "0.0.0-nightly-
|
25
|
+
"@modern-js/sandpack-react": "0.0.0-nightly-20230920160608"
|
26
26
|
},
|
27
27
|
"peerDependencies": {
|
28
|
-
"@modern-js/builder-doc": "0.0.0-nightly-
|
28
|
+
"@modern-js/builder-doc": "0.0.0-nightly-20230920160608"
|
29
29
|
},
|
30
30
|
"devDependencies": {
|
31
31
|
"classnames": "^2",
|
@@ -39,8 +39,8 @@
|
|
39
39
|
"@rspress/shared": "0.0.13",
|
40
40
|
"@types/node": "^16",
|
41
41
|
"@types/fs-extra": "^9",
|
42
|
-
"@modern-js/
|
43
|
-
"@modern-js/doc
|
42
|
+
"@modern-js/doc-plugin-auto-sidebar": "0.0.0-nightly-20230920160608",
|
43
|
+
"@modern-js/builder-doc": "0.0.0-nightly-20230920160608"
|
44
44
|
},
|
45
45
|
"scripts": {
|
46
46
|
"dev": "rspress dev",
|