@modern-js/main-doc 0.0.0-nightly-20230917160555 → 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.
Files changed (42) hide show
  1. package/docs/en/apis/app/commands.mdx +12 -11
  2. package/docs/en/components/debug-app.mdx +1 -3
  3. package/docs/en/components/global-proxy-config.mdx +4 -13
  4. package/docs/en/components/global-proxy.mdx +2 -4
  5. package/docs/en/components/init-app.mdx +1 -1
  6. package/docs/en/configure/app/server/base-url.mdx +0 -2
  7. package/docs/en/guides/basic-features/alias.mdx +1 -1
  8. package/docs/en/guides/basic-features/css.mdx +35 -15
  9. package/docs/en/guides/basic-features/data-fetch.mdx +12 -12
  10. package/docs/en/guides/basic-features/html.mdx +2 -2
  11. package/docs/en/guides/basic-features/routes.mdx +27 -15
  12. package/docs/en/guides/get-started/quick-start.mdx +8 -17
  13. package/docs/en/guides/get-started/upgrade.mdx +10 -4
  14. package/docs/en/guides/topic-detail/framework-plugin/implement.mdx +2 -0
  15. package/docs/en/guides/topic-detail/micro-frontend/c01-introduction.mdx +1 -0
  16. package/docs/en/guides/topic-detail/micro-frontend/c02-development.mdx +24 -17
  17. package/docs/en/guides/topic-detail/micro-frontend/c03-main-app.mdx +51 -37
  18. package/docs/en/guides/topic-detail/micro-frontend/c04-communicate.mdx +1 -0
  19. package/docs/en/guides/topic-detail/micro-frontend/c05-mixed-stack.mdx +1 -1
  20. package/docs/en/tutorials/first-app/c02-component.mdx +4 -3
  21. package/docs/zh/apis/app/commands.mdx +10 -9
  22. package/docs/zh/apis/app/hooks/api/api.mdx +2 -1
  23. package/docs/zh/apis/app/hooks/server/index_.mdx +2 -1
  24. package/docs/zh/components/debug-app.mdx +1 -3
  25. package/docs/zh/components/global-proxy-config.mdx +4 -13
  26. package/docs/zh/components/global-proxy.mdx +2 -4
  27. package/docs/zh/configure/app/server/base-url.mdx +0 -2
  28. package/docs/zh/guides/basic-features/alias.mdx +1 -1
  29. package/docs/zh/guides/basic-features/css.mdx +35 -15
  30. package/docs/zh/guides/basic-features/data-fetch.mdx +11 -12
  31. package/docs/zh/guides/basic-features/html.mdx +4 -5
  32. package/docs/zh/guides/basic-features/routes.mdx +26 -19
  33. package/docs/zh/guides/get-started/quick-start.mdx +8 -17
  34. package/docs/zh/guides/get-started/upgrade.mdx +10 -4
  35. package/docs/zh/guides/topic-detail/framework-plugin/hook-list.mdx +1 -1
  36. package/docs/zh/guides/topic-detail/framework-plugin/implement.mdx +2 -0
  37. package/docs/zh/guides/topic-detail/micro-frontend/c02-development.mdx +45 -21
  38. package/docs/zh/guides/topic-detail/micro-frontend/c03-main-app.mdx +51 -33
  39. package/docs/zh/guides/topic-detail/micro-frontend/c04-communicate.mdx +1 -0
  40. package/docs/zh/guides/topic-detail/micro-frontend/c05-mixed-stack.mdx +1 -0
  41. package/docs/zh/tutorials/first-app/c02-component.mdx +4 -3
  42. package/package.json +5 -5
@@ -2,13 +2,14 @@
2
2
  sidebar_position: 3
3
3
  title: Develop Main App
4
4
  ---
5
+
5
6
  # Develop Main App
6
7
 
7
8
  In the previous [Experience micro frontend](/guides/topic-detail/micro-frontend/c02-development), an example was used to demonstrate how to create and configure micro frontend sub-applications. Through this chapter, you can further understand how to develop the main application, and its common configuration.
8
9
 
9
- After creating an application project through the `@modern-js/create` command, you can execute `pnpm run new` in the project (the `modern new` command is actually executed). After selecting the 「micro frontend」 mode, the micro frontend will be installed. Dependencies, just register the plugin manually.
10
+ After creating an Modern.js project through the `@modern-js/create` command, you can execute `pnpm run new` in the project (the `modern new` command is actually executed). After selecting the 「micro frontend」 mode, the micro frontend will be installed. Dependencies, just register the plugin manually.
10
11
 
11
- import EnableMicroFrontend from "@site-docs-en/components/enable-micro-frontend";
12
+ import EnableMicroFrontend from '@site-docs-en/components/enable-micro-frontend';
12
13
 
13
14
  <EnableMicroFrontend />
14
15
 
@@ -26,7 +27,7 @@ It can be configured at runtime via the API [defineConfig](/apis/app/runtime/app
26
27
  When the parameter is a function, it cannot be serialized to the output code, so please configure it through defineConfig when it comes to configuration such as functions
27
28
  :::
28
29
 
29
- import MicroRuntimeConfig from "@site-docs-en/components/micro-runtime-config";
30
+ import MicroRuntimeConfig from '@site-docs-en/components/micro-runtime-config';
30
31
 
31
32
  <MicroRuntimeConfig />
32
33
 
@@ -40,15 +41,18 @@ defineConfig(App, {
40
41
  manifest: {
41
42
  getAppList: async () => {
42
43
  // get from remote api
43
- return [{
44
- name: 'Table',
45
- entry: 'http://localhost:8001',
46
- // activeWhen: '/table'
47
- }, {
48
- name: 'Dashboard',
49
- entry: 'http://localhost:8002'
50
- // activeWhen: '/dashboard'
51
- }];
44
+ return [
45
+ {
46
+ name: 'Table',
47
+ entry: 'http://localhost:8001',
48
+ // activeWhen: '/table'
49
+ },
50
+ {
51
+ name: 'Dashboard',
52
+ entry: 'http://localhost:8002',
53
+ // activeWhen: '/dashboard'
54
+ },
55
+ ];
52
56
  },
53
57
  },
54
58
  },
@@ -77,16 +81,30 @@ Suppose our subapp list is configured as follows:
77
81
  ```js title="App.tsx"
78
82
  import { useModuleApps } from '@modern-js/plugin-garfish/runtime';
79
83
 
80
- import { RouterProvider, Route, createBrowserRouter, createRoutesFromElements, BrowserRouter, Link, Outlet } from '@modern-js/runtime/router';
84
+ import {
85
+ RouterProvider,
86
+ Route,
87
+ createBrowserRouter,
88
+ createRoutesFromElements,
89
+ BrowserRouter,
90
+ Link,
91
+ Outlet,
92
+ } from '@modern-js/runtime/router';
81
93
 
82
94
  const AppLayout = () => (
83
95
  <>
84
- <div><Link to={'/table'}>load file-based sub-app</Link></div>
85
- <div><Link to={'/dashboard'}>load self-controlled sub-app</Link></div>
86
- <div><Link to={'/'}>unmount sub-app</Link></div>
96
+ <div>
97
+ <Link to={'/table'}>load file-based sub-app</Link>
98
+ </div>
99
+ <div>
100
+ <Link to={'/dashboard'}>load self-controlled sub-app</Link>
101
+ </div>
102
+ <div>
103
+ <Link to={'/'}>unmount sub-app</Link>
104
+ </div>
87
105
  <Outlet />
88
106
  </>
89
- )
107
+ );
90
108
 
91
109
  export default () => {
92
110
  const { apps, MApp } = useModuleApps();
@@ -103,25 +121,25 @@ export default () => {
103
121
  key={app.name}
104
122
  path={`${app.name.toLowerCase()}/*`}
105
123
  element={
106
- <Component
107
- loadable={{
108
- loading: ({ pastDelay, error }: any) => {
109
- if (error) {
110
- return <div>error: {error?.message}</div>;
111
- } else if (pastDelay) {
112
- return <div>loading</div>;
113
- } else {
114
- return null;
115
- }
116
- },
117
- }}
118
- />
124
+ <Component
125
+ loadable={{
126
+ loading: ({ pastDelay, error }: any) => {
127
+ if (error) {
128
+ return <div>error: {error?.message}</div>;
129
+ } else if (pastDelay) {
130
+ return <div>loading</div>;
131
+ } else {
132
+ return null;
133
+ }
134
+ },
135
+ }}
136
+ />
119
137
  }
120
138
  />
121
- )
139
+ );
122
140
  })}
123
- </Route>
124
- )
141
+ </Route>,
142
+ ),
125
143
  );
126
144
 
127
145
  return (
@@ -164,13 +182,10 @@ function App() {
164
182
 
165
183
  After starting the application in this way, accessing the '/table' route will render the'Table 'sub-application, and accessing the'/dashboard 'route will render the'Dashboard' sub-application.
166
184
 
167
-
168
185
  ### Mix Mode
169
186
 
170
-
171
187
  Of course, **sub-application components** and **centralized routing** can be mixed.
172
188
 
173
-
174
189
  - One molecular application is activated as a **sub-application component**, and the other part is activated as a **centralized routing**.
175
190
  - A molecular application can be activated either as a **centralized routing** or as a **sub-application component**.
176
191
 
@@ -178,7 +193,6 @@ Of course, **sub-application components** and **centralized routing** can be mix
178
193
 
179
194
  By configuring the `loadable` configuration, loading components can be added for 「centralized routing」 and 「sub-applicati」, and errors, timeouts, flashes, etc. can be considered, so as to provide users with a better user experience. The design and implementation of this function refer to [react-loadable](https://github.com/jamiebuilds/react-loadable), and the basic functions are similar.
180
195
 
181
-
182
196
  ```tsx
183
197
  function Loading() {
184
198
  return <div>Loading...</div>;
@@ -2,6 +2,7 @@
2
2
  sidebar_position: 4
3
3
  title: Communicate
4
4
  ---
5
+
5
6
  # Communicate
6
7
 
7
8
  ## Props
@@ -2,6 +2,7 @@
2
2
  sidebar_position: 5
3
3
  title: Mixed Stack
4
4
  ---
5
+
5
6
  # Mixed Stack
6
7
 
7
8
  The Modern.js micro frontend scheme is based on the [Garfish](https://garfishjs.org/) package and provides some more out-of-the-box usage.
@@ -16,7 +17,6 @@ When your main application and sub-application are not all Modern.js application
16
17
  **Modern.js** subapps compile to generate a standard [Garfish subapp export](https://www.garfishjs.org/guide/start#2%E5%AF%BC%E5%87%BA-provider-%E5%87%BD%E6%95%B0).
17
18
  So you can directly access the standard micro frontend main application.
18
19
 
19
-
20
20
  :::info
21
21
  The child application is **Modern.js**, when the main application uses the native Garfish micro frontend, the **child application debugging mode** is not available.
22
22
  :::
@@ -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
- ```bash
13
- pnpm add antd
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
- 122.35 KB dist/static/js/885.1d4fbe5a.js
94
- 2.3 KB dist/static/js/main.4b8e8d64.js
95
- 761 B dist/static/js/runtime-main.edb7cf35.js
96
- 645 B dist/static/css/main.0dd3ecc1.css
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 文件,可以看到打包产物的瓦片图,并进行包体积分析和优化:
@@ -123,7 +124,7 @@ Options:
123
124
 
124
125
  ### 添加入口
125
126
 
126
- 在应用工程中,执行 `new` 命令添加入口如下:
127
+ Modern.js 工程中,执行 `new` 命令添加入口的步骤如下:
127
128
 
128
129
  ```bash
129
130
  $ npx modern new
@@ -134,7 +135,7 @@ $ npx modern new
134
135
 
135
136
  ### 启用可选功能
136
137
 
137
- 在应用工程中,执行 `new` 命令启用可选能力如下:
138
+ Modern.js 工程中,执行 `new` 命令启用可选能力的步骤如下:
138
139
 
139
140
  ```bash
140
141
  $ npx modern new
@@ -156,7 +157,7 @@ pnpm 暂不支持使用 JSON 字符串作为参数值,可使用 `npm new` 开
156
157
 
157
158
  ## modern serve
158
159
 
159
- `modern serve` 命令用于在生产环境下启用应用工程, 注意需要提前执行 [`build`](/apis/app/commands#modern-build) 命令构建出对应产物。
160
+ `modern serve` 命令用于在生产环境下启动 Modern.js 工程, 也可以用于在本地预览生产环境构建的产物。注意你需要提前执行 [`build`](/apis/app/commands#modern-build) 命令构建出对应产物。
160
161
 
161
162
  ```bash
162
163
  Usage: modern serve [options]
@@ -2,6 +2,7 @@
2
2
  title: '**/*.[tj]s'
3
3
  sidebar_position: 1
4
4
  ---
5
+
5
6
  # **/*.[tj]s
6
7
 
7
8
  在 [BFF 函数写法](/guides/advanced-features/bff/type.html#函数写法)下,声明 API 路由的文件。除了[某些约定文件](/apis/app/hooks/api/api#白名单)外,`api` 目录下的文件会被注册为接口的路由。
@@ -66,7 +67,7 @@ export const get = async () => {
66
67
 
67
68
  这样导出函数,则会得到一个 `GET` 接口。
68
69
 
69
- 应用工程中支持了 9 个 Method 定义,即:`GET`、`POST`、`PUT`、`DELETE`、`CONNECT`、`TRACE`、`PATCH`、`OPTION`、`HEAD`,即可以用这些 Method 作为函数导出的名字。
70
+ Modern.js 工程中支持了 9 个 Method 定义,即:`GET`、`POST`、`PUT`、`DELETE`、`CONNECT`、`TRACE`、`PATCH`、`OPTION`、`HEAD`,即可以用这些 Method 作为函数导出的名字。
70
71
 
71
72
  名字是大小不敏感的,就是说,如果是 `GET`,写成 `get`、`Get`、`GEt`、`GET`,都可以准确识别。而默认导出,即 `export default xxx` 则会被映射为 `Get`。
72
73
 
@@ -2,8 +2,9 @@
2
2
  title: index.[tj]s
3
3
  sidebar_position: 1
4
4
  ---
5
+
5
6
  # index.[tj]s
6
7
 
7
- 扩展 Modern.js Web Server 的文件,在此文件中可以给应用工程启动的 Web Server 添加 [Hook](/apis/app/runtime/web-server/hook) 或 [Middleware](/apis/app/runtime/web-server/middleware)。
8
+ 扩展 Modern.js Web Server 的文件,在此文件中可以给 Modern.js 工程启动的 Web Server 添加 [Hook](/apis/app/runtime/web-server/hook) 或 [Middleware](/apis/app/runtime/web-server/middleware)。
8
9
 
9
10
  可以对请求和响应进行拦截处理,鉴权与角色、请求预处理、异常兜底等。也可在内置处理逻辑(包括路由匹配、资源寻址、头部注入、页面渲染,静态 Web 托管)插入特定的业务逻辑。
@@ -6,12 +6,10 @@ $ pnpm run dev
6
6
  > modern dev
7
7
 
8
8
  info Starting dev server...
9
- info App running at:
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
- ```bash
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
- # pnpm
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
- info Starting the proxy server.....
84
- success Proxy Server start on localhost:8899
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
- info Starting the proxy server.....
20
- success Proxy Server start on localhost:8899
17
+ info Starting proxy server.....
18
+ success Proxy server start on localhost:8899
21
19
  ```
22
20
 
23
21
  在控制台中可以看到代理服务器成功启动。
@@ -25,8 +25,6 @@ export default defineConfig({
25
25
  `dev` 之后可以看到路由访问会加上对应前缀:
26
26
 
27
27
  ```bash
28
- App running at:
29
-
30
28
  > Local: http://localhost:8080/base/
31
29
  > Network: http://192.168.0.1:8080/base/
32
30
  ```
@@ -1,5 +1,5 @@
1
1
  ---
2
- sidebar_position: 3
2
+ sidebar_position: 4
3
3
  ---
4
4
 
5
5
  # 路径别名
@@ -1,5 +1,5 @@
1
1
  ---
2
- sidebar_position: 2
2
+ sidebar_position: 1
3
3
  ---
4
4
 
5
5
  # 样式开发
@@ -61,16 +61,31 @@ Modern.js 内部集成了 Babel 的 [babel-plugin-styled-components](https://git
61
61
 
62
62
  ## 使用 Tailwind CSS
63
63
 
64
- [Tailwind CSS](https://tailwindcss.com/) 是一个以 Utility Class 为基础的 CSS 框架和设计系统,可以快速地为组件添加常用样式,同时支持主题样式的灵活扩展。在 Modern.js 中使用 [Tailwind CSS](https://tailwindcss.com/),只需要在项目根目录下执行 `pnpm run new` 并开启。
64
+ [Tailwind CSS](https://tailwindcss.com/) 是一个以 Utility Class 为基础的 CSS 框架和设计系统,可以快速地为组件添加常用样式,同时支持主题样式的灵活扩展。
65
65
 
66
- 按照如下进行选择:
66
+ 在 Modern.js 中使用 [Tailwind CSS](https://tailwindcss.com/),你只需要按照以下步骤操作:
67
+
68
+ 1. 在项目根目录下执行 `pnpm run new`,按照如下进行选择:
67
69
 
68
70
  ```text
69
71
  ? 请选择你想要的操作 启用可选功能
70
72
  ? 请选择功能名称 启用 「Tailwind CSS」 支持
71
73
  ```
72
74
 
73
- `modern.config.ts` 中注册 Tailwind 插件:
75
+ 成功开启后,会看到 `package.json` 中新增了依赖:
76
+
77
+ ```json title="./package.json"
78
+ {
79
+ "dependencies": {
80
+ "tailwindcss": "^3.0.0"
81
+ },
82
+ "devDependencies": {
83
+ "@modern-js/plugin-tailwindcss": "^2.0.0"
84
+ }
85
+ }
86
+ ```
87
+
88
+ 2. 在 `modern.config.ts` 中注册 Tailwind 插件:
74
89
 
75
90
  ```ts title="modern.config.ts"
76
91
  import { tailwindcssPlugin } from '@modern-js/plugin-tailwindcss';
@@ -80,29 +95,34 @@ export default defineConfig({
80
95
  });
81
96
  ```
82
97
 
83
- 使用时在入口的根组件(如 `src/App.jsx`)添加如下代码:
98
+ 3. 创建 `index.css` 文件,添加以下代码:
99
+
100
+ ```css title="index.css"
101
+ @tailwind base;
102
+ @tailwind components;
103
+ @tailwind utilities;
104
+ ```
105
+
106
+ :::info
107
+ 根据需求不同,你可以选择性地导入 Tailwind CSS 提供的 CSS 样式。请参考 [`@tailwind` 文档](https://tailwindcss.com/docs/functions-and-directives#tailwind) 来了解 `@tailwind` 指令的详细用法。
108
+ :::
109
+
110
+ 4. 引用 `index.css` 文件,比如在入口的根组件 `src/App.jsx` 添加如下代码:
84
111
 
85
112
  ```js
86
- import 'tailwindcss/base.css';
87
- import 'tailwindcss/components.css';
88
- import 'tailwindcss/utilities.css';
113
+ import './index.css';
89
114
  ```
90
115
 
91
- 然后即可在各个组件中使用 Tailwind CSS 提供的 Utility Class 了:
116
+ 5. 然后即可在各个组件中使用 Tailwind CSS 提供的 Utility Class 了:
92
117
 
93
118
  ```tsx
94
- const App = () => (
119
+ const Hello = () => (
95
120
  <div className="h-12 w-48">
96
121
  <p className="text-xl font-medium text-black">hello world</p>
97
122
  </div>
98
123
  );
99
124
  ```
100
125
 
101
- :::info 补充信息
102
- 根据需求不同,你可以选择性的导入 Tailwind CSS 提供的 CSS 文件。由于使用 `@tailwind` 与直接导入 CSS 文件的作用等价,因此关于 Tailwind CSS 提供的 CSS 文件的用途,可以参考 [`@tailwind` 的使用](https://tailwindcss.com/docs/functions-and-directives#tailwind) 文档中注释里的内容。
103
-
104
- :::
105
-
106
126
  ### 配置 Tailwind CSS
107
127
 
108
128
  在 Modern.js 中,你有两种方式来配置 Tailwind CSS:
@@ -1,10 +1,11 @@
1
1
  ---
2
- sidebar_position: 4
2
+ title: 数据获取
3
+ sidebar_position: 3
3
4
  ---
4
5
 
5
6
  # 数据获取
6
7
 
7
- Modern.js 中提供了开箱即用的数据获取能力,开发者可以通过这些 API,在 CSR 和 SSR 环境同构的进行开发。
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 "@modern-js/runtime/router"
226
+ import { defer } from '@modern-js/runtime/router';
227
227
 
228
228
  const loader = () =>
229
- defer({
230
- userInfo: new Promise((resolve) => {
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 应用使用,让开发者能同构的在组件中获取数据。
@@ -135,7 +135,7 @@ export default function Document(): React.ReactElement {
135
135
  <!--<?- html ?>-->
136
136
  <h1 style="color:red">以下为构建时传过来的参数:</h1>
137
137
  <h2>entryName: sub</h2>
138
- <h2>title: </h2>
138
+ <h2>title:</h2>
139
139
  <h2>rootId: root</h2>
140
140
  </div>
141
141
  <h1>bottom</h1>
@@ -166,12 +166,11 @@ export default function Document(): React.ReactElement {
166
166
  }
167
167
  ```
168
168
 
169
+ ## HTML 语法
169
170
 
170
- ## Html 语法
171
+ Modern.js 也支持通过 HTML(EJS) 语法来生成 HTML 文件。
171
172
 
172
- Modern.js 也支持 HTML 语法。默认情况下,Modern.js 的应用工程中会内置一份 HTML 模板,用于生成 HTML 代码。
173
-
174
- 基于 HTML 语法的模板,Modern.js 提供了**自定义 HTML 片段**和**完全自定义 HTML 模板**两种方式来自定义模板。
173
+ 默认情况下,Modern.js 的工程中会内置一份 HTML 模板,用于生成 HTML 代码。如果你需要自定义 HTML 模板,可以使用**自定义 HTML 片段**和**完全自定义 HTML 模板**两种方式。
175
174
 
176
175
  ### 自定义 HTML 片段
177
176
 
@@ -1,5 +1,5 @@
1
1
  ---
2
- sidebar_position: 1
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` 或 `Page` 文件都可以定义一个自己的 `config` 文件,如 `page.config.ts`,该文件中我们约定了一个具名导出 `handle`,
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(matchedRoute => matchedRoute?.handle?.breadcrumbName);
169
- return (
170
- <Breadcrumb names={breadcrumbs}>
171
- </Breadcrumb>
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
- import Practice from '@site-docs/components/routes-practice'
527
+ <Motivation />
520
528
 
521
- <Practice/>
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
-