@modern-js/main-doc 0.0.0-next-20221203140534 → 0.0.0-next-20221205074243
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/CHANGELOG.md +2 -2
- package/en/docusaurus-plugin-content-docs/current/apis/app/commands/new.md +0 -2
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/core/bootstrap.md +17 -3
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/builder.md +46 -0
- package/en/docusaurus-plugin-content-docs/current.json +18 -18
- package/package.json +3 -3
- package/zh/apis/app/commands/new.md +0 -2
- package/zh/apis/app/runtime/app/_category_.json +1 -1
- package/zh/apis/app/runtime/bff/_category_.json +1 -1
- package/zh/apis/app/runtime/core/_category_.json +1 -1
- package/zh/apis/app/runtime/core/bootstrap.md +17 -3
- package/zh/apis/app/runtime/model/_category_.json +1 -1
- package/zh/apis/app/runtime/router/_category_.json +1 -1
- package/zh/apis/app/runtime/ssr/_category_.json +1 -1
- package/zh/apis/app/runtime/testing/_category_.json +1 -1
- package/zh/apis/app/runtime/utility/_category_.json +1 -1
- package/zh/apis/app/runtime/web-server/_category_.json +1 -1
- package/zh/configure/app/output/ssg.md +118 -114
- package/zh/configure/app/server/ssr.md +0 -2
- package/zh/guides/advanced-features/custom-app.md +8 -2
- package/zh/guides/advanced-features/ssg.md +74 -63
- package/zh/guides/advanced-features/ssr.md +76 -11
- package/zh/guides/basic-features/builder.md +46 -0
- package/zh/guides/basic-features/css/_category_.json +1 -1
- package/zh/guides/basic-features/css/less-sass.md +1 -14
- package/zh/guides/basic-features/data-fetch.md +1 -1
- package/zh/guides/basic-features/routes.md +32 -35
- package/zh/guides/concept/entries.md +4 -4
- package/zh/tutorials/first-app/c01-getting-started/1.2-minimal-mwa.md +2 -2
- package/zh/tutorials/first-app/c01-getting-started/1.4-enable-ssr.md +5 -2
- package/zh/tutorials/first-app/c02-generator-and-studio/2.2-boilerplates.md +4 -6
- package/zh/tutorials/first-app/c02-generator-and-studio/2.3-configuration.md +2 -4
- package/zh/tutorials/first-app/c03-ide/3.1-setting-up.md +1 -1
- package/zh/tutorials/first-app/c03-ide/3.2-hints-in-ide.md +44 -50
- package/zh/tutorials/first-app/c03-ide/3.3-autofix-in-ide.md +1 -1
- package/zh/tutorials/first-app/c03-ide/3.4-autofix-in-cli.md +4 -4
- package/zh/tutorials/first-app/c04-es6-plus-and-ts/4.1-use-es6-plus.md +8 -21
- package/zh/tutorials/first-app/c04-es6-plus-and-ts/4.2-use-typescript.md +37 -13
- package/zh/tutorials/first-app/c05-component/5.1-use-ui-library.md +3 -13
- package/zh/tutorials/first-app/c05-component/5.2-use-standalone-component.md +1 -21
- package/zh/tutorials/first-app/c06-css-and-component/6.1-css-in-js.md +9 -9
- package/zh/tutorials/first-app/c06-css-and-component/6.2-utility-class.md +9 -14
- package/zh/tutorials/first-app/c06-css-and-component/6.3-postcss.md +7 -7
- package/zh/tutorials/first-app/c06-css-and-component/6.4-design-system.md +1 -1
- package/zh/tutorials/first-app/c06-css-and-component/6.5-storybook.md +2 -2
- package/zh/tutorials/first-app/c06-css-and-component/6.6-testing.md +8 -17
- package/zh/tutorials/first-app/c07-app-entry/7.1-intro.md +23 -18
- package/zh/tutorials/first-app/c07-app-entry/7.2-add-entry-in-cli.md +30 -30
- package/zh/tutorials/first-app/c07-app-entry/7.3-manage-entries-by-hand.md +4 -9
- package/zh/tutorials/first-app/c08-client-side-routing/8.1-code-based-routing.md +66 -63
- package/zh/tutorials/first-app/c09-bff/9.2-enable-bff.md +35 -33
- package/zh/tutorials/first-app/c09-bff/9.3-fetch-bff.md +28 -102
- package/zh/tutorials/first-app/c10-model/10.1-application-architecture.md +4 -6
- package/zh/tutorials/first-app/c10-model/10.2-add-model.md +3 -3
- package/zh/tutorials/first-app/c10-model/10.3-use-model.md +21 -20
- package/zh/tutorials/first-app/c10-model/10.4-testing.md +2 -2
- package/zh/tutorials/first-app/c11-container/11.1-use-model-with-app-state.md +34 -68
- package/zh/tutorials/first-app/c11-container/11.2-add-container.md +40 -37
- package/zh/tutorials/first-app/c11-container/11.3-use-loader.md +6 -4
- package/zh/tutorials/first-app/c11-container/11.4-testing.md +2 -2
- package/zh/tutorials/foundations/introduction.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/default-alias.md +0 -25
- package/zh/apis/app/runtime/default-alias.md +0 -23
- package/zh/guides/basic-features/image.md +0 -43
- package/zh/guides/topic-detail/compile-speed.md +0 -182
- package/zh/guides/troubleshooting/compile.md +0 -379
- package/zh/tutorials/first-app/c08-client-side-routing/8.2-file-based-routing.md +0 -310
@@ -3,15 +3,13 @@ sidebar_label: ssg
|
|
3
3
|
---
|
4
4
|
# output.ssg
|
5
5
|
|
6
|
-
|
7
|
-
|
8
6
|
* 类型: `boolean` | `object` | `function`
|
9
7
|
* 默认值: `undefined`
|
10
8
|
|
11
9
|
开启**自控式路由**或**约定式路由** SSG 功能的配置。
|
12
10
|
|
13
11
|
:::info 客户端路由
|
14
|
-
相关内容可以查看[
|
12
|
+
相关内容可以查看[路由](/docs/guides/basic-features/routes)。
|
15
13
|
:::
|
16
14
|
|
17
15
|
## 示例
|
@@ -20,115 +18,134 @@ sidebar_label: ssg
|
|
20
18
|
|
21
19
|
当该配置设置为 `true` 时,将会默认开启所有入口的 SSG 功能。
|
22
20
|
|
23
|
-
|
21
|
+
对**自控式路由**而言,将会渲染入口的根路由。对**约定式路由**而言,将会渲染入口中每一条路由。
|
24
22
|
|
25
23
|
例如 `src/` 目录下有以下满足**约定式路由**的文件结构:
|
26
24
|
|
27
25
|
```bash
|
28
26
|
.
|
29
|
-
├──
|
30
|
-
│
|
31
|
-
│
|
32
|
-
│
|
33
|
-
│
|
34
|
-
├── .
|
27
|
+
├── src
|
28
|
+
│ └── routes
|
29
|
+
│ ├── layout.tsx
|
30
|
+
│ ├── page.tsx
|
31
|
+
│ └── user
|
32
|
+
│ ├── layout.tsx
|
33
|
+
│ ├── page.tsx
|
34
|
+
│ └── profile
|
35
|
+
│ └── page.tsx
|
35
36
|
```
|
36
37
|
|
37
38
|
在 `modern.js.config` 中做以下设置:
|
38
39
|
|
39
40
|
```js
|
40
|
-
|
41
|
+
export default defineConfig({
|
41
42
|
output: {
|
42
43
|
ssg: true,
|
43
44
|
},
|
44
|
-
};
|
45
|
+
});
|
45
46
|
```
|
46
47
|
|
47
|
-
执行 `pnpm build` 构建应用后。`dist/` 目录将会生成三张 HTML 分别对应三条路由(不开启 SSG 时只有一张 HTML
|
48
|
+
执行 `pnpm build` 构建应用后。`dist/` 目录将会生成三张 HTML 分别对应三条路由(不开启 SSG 时只有一张 HTML),并且所有 HTML 都已经渲染。
|
48
49
|
|
49
50
|
而例如下面的**自控式路由**:
|
50
51
|
|
51
|
-
```
|
52
|
-
import {
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
52
|
+
```tsx title="App.tsx"
|
53
|
+
import { useRuntimeContext } from '@modern-js/runtime';
|
54
|
+
import { Routes, Route, BrowserRouter } from '@modern-js/runtime/router';
|
55
|
+
import { StaticRouter } from '@modern-js/runtime/router/server';
|
56
|
+
|
57
|
+
const Router = typeof window === 'undefined' ? StaticRouter : BrowserRouter;
|
58
|
+
|
59
|
+
export default () => {
|
60
|
+
const { context } = useRuntimeContext();
|
61
|
+
return (
|
62
|
+
<Router location={context.request.pathname}>
|
63
|
+
<Routes>
|
64
|
+
<Route index element={<div>index</div>} />
|
65
|
+
<Route path="about" element={<div>about</div>} />
|
66
|
+
</Routes>
|
67
|
+
</Router>
|
68
|
+
);
|
69
|
+
};
|
64
70
|
```
|
65
71
|
|
66
|
-
同样使用上面的配置,在执行 `pnpm build` 后,只有入口路由 `/` 会生成渲染后的 HTML。
|
72
|
+
同样使用上面的配置,在执行 `pnpm run build` 后,只有入口路由 `/` 会生成渲染后的 HTML。
|
67
73
|
|
68
74
|
### 多入口
|
69
75
|
|
76
|
+
:::note
|
77
|
+
多入口 SSG 暂未支持,敬请期待。
|
78
|
+
:::
|
79
|
+
|
70
80
|
`output.ssg` 也可以按照入口配置,配置生效的规则同样由入口路由方式决定。
|
71
81
|
|
72
82
|
例如以下目录结构:
|
73
83
|
|
74
84
|
```bash
|
75
|
-
|
76
|
-
├── src
|
77
|
-
│ ├──
|
78
|
-
│
|
79
|
-
│
|
80
|
-
│
|
81
|
-
│
|
82
|
-
│
|
83
|
-
│
|
85
|
+
。
|
86
|
+
├── src
|
87
|
+
│ ├── entryA
|
88
|
+
│ │ └── routes
|
89
|
+
│ │ ├── layout.tsx
|
90
|
+
│ │ ├── page.tsx
|
91
|
+
│ │ └── user
|
92
|
+
│ │ ├── layout.tsx
|
93
|
+
│ │ ├── page.tsx
|
94
|
+
│ │ └── profile
|
95
|
+
│ │ └── page.tsx
|
96
|
+
│ └── entryB
|
97
|
+
│ └── App.tsx
|
84
98
|
```
|
85
99
|
|
86
|
-
默认情况下,所有约定式路由的入口,在设置 `output.ssg`
|
100
|
+
默认情况下,所有约定式路由的入口,在设置 `output.ssg` 配置为 `true` 后都会在构建阶段渲染。可以配置 `false` 来取消指定入口的的默认行为,例如取消上述 entryA 入口在构建时的渲染:
|
87
101
|
|
88
102
|
```js
|
89
|
-
|
103
|
+
export default defineConfig({
|
90
104
|
output: {
|
91
105
|
ssg: {
|
92
|
-
|
93
|
-
|
106
|
+
entryA: true,
|
107
|
+
entryB: false,
|
94
108
|
},
|
95
109
|
},
|
96
|
-
};
|
110
|
+
});
|
97
111
|
```
|
98
112
|
|
99
113
|
### 配置路由
|
100
114
|
|
101
115
|
上述内容中提到,**自控式路由**默认只会开启入口路由的 SSG 配置。
|
102
116
|
|
103
|
-
可以在 `output.ssg` 中设置具体的路由,告知 Modern.js 开启这些客户端路由的 SSG 功能。例如上述 `
|
104
|
-
|
105
|
-
```
|
106
|
-
import {
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
117
|
+
可以在 `output.ssg` 中设置具体的路由,告知 Modern.js 开启这些客户端路由的 SSG 功能。例如上述 `src/App.tsx` 的文件内容为:
|
118
|
+
|
119
|
+
```tsx title="src/App.tsx"
|
120
|
+
import { useRuntimeContext } from '@modern-js/runtime';
|
121
|
+
import { Routes, Route, BrowserRouter } from '@modern-js/runtime/router';
|
122
|
+
import { StaticRouter } from '@modern-js/runtime/router/server';
|
123
|
+
|
124
|
+
const Router = typeof window === 'undefined' ? StaticRouter : BrowserRouter;
|
125
|
+
|
126
|
+
export default () => {
|
127
|
+
const { context } = useRuntimeContext();
|
128
|
+
return (
|
129
|
+
<Router location={context.request.pathname}>
|
130
|
+
<Routes>
|
131
|
+
<Route index element={<div>index</div>} />
|
132
|
+
<Route path="about" element={<div>about</div>} />
|
133
|
+
</Routes>
|
134
|
+
</Router>
|
135
|
+
);
|
136
|
+
};
|
118
137
|
```
|
119
138
|
|
120
|
-
在 `modern.config.js` 中这样设置后,`/
|
139
|
+
在 `modern.config.js` 中这样设置后,`/about` 路由也会开启 SSG 功能:
|
121
140
|
|
122
141
|
```js
|
123
|
-
|
142
|
+
export default defineConfig({
|
124
143
|
output: {
|
125
144
|
ssg: {
|
126
|
-
|
127
|
-
routes: ['/', '/foo'],
|
128
|
-
},
|
145
|
+
routes: ['/', '/about'],
|
129
146
|
},
|
130
147
|
},
|
131
|
-
};
|
148
|
+
});
|
132
149
|
```
|
133
150
|
|
134
151
|
Modern.js 将会自动根据入口拼接完整的 URL 并交给 SSG 插件完成渲染。
|
@@ -136,22 +153,20 @@ Modern.js 将会自动根据入口拼接完整的 URL 并交给 SSG 插件完成
|
|
136
153
|
也可以为具体入口或路由配置请求头,例如:
|
137
154
|
|
138
155
|
```js
|
139
|
-
|
156
|
+
export default defineConfig({
|
140
157
|
output: {
|
141
158
|
ssg: {
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
],
|
151
|
-
},
|
159
|
+
headers: {},
|
160
|
+
routes: [
|
161
|
+
'/',
|
162
|
+
{
|
163
|
+
url: '/about',
|
164
|
+
headers: {},
|
165
|
+
},
|
166
|
+
],
|
152
167
|
},
|
153
168
|
},
|
154
|
-
};
|
169
|
+
});
|
155
170
|
```
|
156
171
|
|
157
172
|
:::info
|
@@ -160,67 +175,56 @@ module.exports = {
|
|
160
175
|
|
161
176
|
### 阻止默认行为
|
162
177
|
|
163
|
-
|
164
|
-
|
165
|
-
但因为默认行为的不同,这里为**约定式路由**提供了另一个字段,用来阻止默认的 SSG 行为。
|
178
|
+
默认情况下,**约定式路由**的路由会全部开启 SSG。Modern.js 提供了另一个字段,用来阻止默认的 SSG 行为。
|
166
179
|
|
167
|
-
|
180
|
+
例如以下目录结构,`/`、`/user`、`/user/profle` 三条路由都开启 SSG:
|
168
181
|
|
169
182
|
```bash
|
170
183
|
.
|
171
|
-
├── src
|
172
|
-
│
|
173
|
-
│
|
174
|
-
│
|
175
|
-
│
|
176
|
-
│
|
177
|
-
│
|
178
|
-
│
|
179
|
-
│
|
180
|
-
│ │ ├── docs3.tsx
|
181
|
-
│ │ ├── docs4.tsx
|
182
|
-
│ │ └── index.tsx
|
184
|
+
├── src
|
185
|
+
│ └── routes
|
186
|
+
│ ├── layout.tsx
|
187
|
+
│ ├── page.tsx
|
188
|
+
│ └── user
|
189
|
+
│ ├── layout.tsx
|
190
|
+
│ ├── page.tsx
|
191
|
+
│ └── profile
|
192
|
+
│ └── page.tsx
|
183
193
|
```
|
184
194
|
|
185
195
|
可以这样设置,禁用某一条客户端路由的默认行为:
|
186
196
|
|
187
|
-
:::info 注
|
188
|
-
该配置仅在渲染动态路径的约定式路由时使用。
|
189
|
-
:::
|
190
|
-
|
191
197
|
```js
|
192
|
-
|
198
|
+
export default defineConfig({
|
193
199
|
output: {
|
194
|
-
'
|
195
|
-
preventDefault: ['/docs4'],
|
196
|
-
},
|
200
|
+
preventDefault: ['/user'],
|
197
201
|
},
|
198
|
-
};
|
202
|
+
});
|
199
203
|
```
|
200
204
|
|
201
205
|
### 添加动态路由参数
|
202
206
|
|
203
|
-
部分路由可能是动态的,例如自控式路由中的 `/user/:id` 或是约定式路由中 `user/[id].
|
207
|
+
部分路由可能是动态的,例如自控式路由中的 `/user/:id` 或是约定式路由中 `user/[id]/page.tsx` 文件生成的路由。
|
204
208
|
|
205
209
|
可以在 `output.ssg` 中配置具体的参数,渲染指定参数的路由,例如:
|
206
210
|
|
207
211
|
```js
|
208
|
-
|
212
|
+
export default defineConfig({
|
209
213
|
output: {
|
210
214
|
ssg: {
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
],
|
222
|
-
},
|
215
|
+
routes: [
|
216
|
+
{
|
217
|
+
url: '/user/:id',
|
218
|
+
params: [
|
219
|
+
{
|
220
|
+
id: 'modernjs',
|
221
|
+
},
|
222
|
+
],
|
223
|
+
},
|
224
|
+
],
|
223
225
|
},
|
224
226
|
},
|
225
|
-
};
|
227
|
+
});
|
226
228
|
```
|
229
|
+
|
230
|
+
动态路由和 SSG 的组合,在根据 CMS 系统数据变更,实时生成静态页面时非常有用。
|
@@ -9,17 +9,23 @@ sidebar_position: 7
|
|
9
9
|
|
10
10
|
## 添加自定义行为
|
11
11
|
|
12
|
-
当 `index` 文件默认导出**函数**是,Modern.js 还是会根据 `runtime
|
12
|
+
当 `index` 文件默认导出**函数**是,Modern.js 还是会根据 `runtime` 的设置情况生成 `createApp` 包裹后的代码。在渲染过程中,将 `createApp` 包裹后的组件作为参数传递给 `index` 文件导出的函数,这样开发者可以自定义将组件挂载到 DOM 节点上,或在挂载前添加自定义行为。例如:
|
13
13
|
|
14
14
|
```js title=src/index.jsx
|
15
|
+
import ReactDOM from 'react-dom/client'
|
15
16
|
import { bootstrap } from '@modern-js/runtime';
|
16
17
|
|
18
|
+
|
17
19
|
export default App => {
|
18
20
|
// do something before bootstrap...
|
19
|
-
bootstrap(App, 'root');
|
21
|
+
bootstrap(App, 'root', undefined, ReactDOM);
|
20
22
|
};
|
21
23
|
```
|
22
24
|
|
25
|
+
:::warning
|
26
|
+
由于 bootstrap 函数需要兼容 React17 和 React18 的用法,调用 bootstrap 函数时需要手动传入 ReactDOM 参数。
|
27
|
+
:::
|
28
|
+
|
23
29
|
Modern.js 生成的文件内容如下:
|
24
30
|
|
25
31
|
```js
|
@@ -1,5 +1,5 @@
|
|
1
1
|
---
|
2
|
-
title:
|
2
|
+
title: 静态站点生成(SSG)
|
3
3
|
sidebar_position: 4
|
4
4
|
---
|
5
5
|
|
@@ -9,11 +9,11 @@ SSG(Static Site Generation)是一种基于数据与模板,在构建时渲
|
|
9
9
|
SSG 是构建阶段的解决方案,因此仅对生产环境有效。通过 `dev` 命令运行时,表现效果与 SSR 相同。
|
10
10
|
:::
|
11
11
|
|
12
|
-
|
12
|
+
我们首先需要执行 `pnpm run new` 启用 SSG 功能:
|
13
13
|
|
14
14
|
```bash
|
15
|
-
?
|
16
|
-
?
|
15
|
+
? 请选择你想要的操作: 启用可选功能
|
16
|
+
? 启用可选功能: 启用「SSG」功能
|
17
17
|
```
|
18
18
|
|
19
19
|
SSG 在**约定式路由**和**自控式路由**下的使用方式不同。
|
@@ -25,22 +25,37 @@ SSG 在**约定式路由**和**自控式路由**下的使用方式不同。
|
|
25
25
|
例如,以下是一个使用约定式路由的项目目录结构:
|
26
26
|
|
27
27
|
```
|
28
|
-
|
29
|
-
|
30
|
-
│
|
31
|
-
│
|
32
|
-
│
|
28
|
+
.
|
29
|
+
├── src
|
30
|
+
│ └── routes
|
31
|
+
│ ├── layout.tsx
|
32
|
+
│ ├── page.tsx
|
33
|
+
│ └── user
|
34
|
+
│ ├── layout.tsx
|
35
|
+
│ ├── page.tsx
|
36
|
+
│ └── profile
|
37
|
+
│ └── page.tsx
|
33
38
|
```
|
34
39
|
|
35
|
-
|
40
|
+
上述文件目录将会生成以下三条路由:
|
41
|
+
|
42
|
+
- `/`
|
43
|
+
- `/user`
|
44
|
+
- `/user/profile`
|
45
|
+
|
46
|
+
:::note
|
47
|
+
如果还不了解约定式路由的规则,可以先查看[路由](/docs/guides/basic-features/routes)。
|
48
|
+
:::
|
49
|
+
|
50
|
+
在 `src/routes/page.tsx` 中添加组件代码:
|
36
51
|
|
37
52
|
```jsx title="src/pages/index.tsx"
|
38
53
|
export default () => {
|
39
|
-
return <div>
|
54
|
+
return <div>Index Page</div>
|
40
55
|
}
|
41
56
|
```
|
42
57
|
|
43
|
-
SSG
|
58
|
+
SSG 也是在 Node.js 环境渲染页面,因此我们可以在**开发阶段开启 SSR**,提前在暴露代码问题,验证 SSG 渲染效果:
|
44
59
|
|
45
60
|
```typescript title="modern.config.ts"
|
46
61
|
export default defineConfig({
|
@@ -50,13 +65,11 @@ export default defineConfig({
|
|
50
65
|
}
|
51
66
|
```
|
52
67
|
|
53
|
-
在项目根路径下执行 `pnpm run dev` 命令,查看 `dist/` 目录,此时只生成一个 HTML
|
54
|
-
|
55
|
-

|
68
|
+
在项目根路径下执行 `pnpm run dev` 命令,查看 `dist/` 目录,此时只生成一个 HTML 文件 `main/index.html`。
|
56
69
|
|
57
|
-
在项目根路径下执行 `pnpm run build` 命令,构建完成后,查看 `dist/` 目录,此时生成 `main/index.html`、`main/
|
70
|
+
在项目根路径下执行 `pnpm run build` 命令,构建完成后,查看 `dist/` 目录,此时生成 `main/index.html`、`main/user/index.html` 和 `main/user/profile/index.html` 三个 HTML 文件,内容分别对应上述三条路由。
|
58
71
|
|
59
|
-
|
72
|
+
**约定式路由**中的每一条路由,都会生成一个单独的 HTML 文件。查看 `main/index.html`,可以发现包含 `Index Page` 的文本内容,这正是 SSG 的效果。
|
60
73
|
|
61
74
|
执行 `pnpm run start` 启动项目后,访问页面,在浏览器我们工具的 Network 窗口,查看请求返回的文档,文档包含组件渲染后的完整页面内容。
|
62
75
|
|
@@ -64,45 +77,46 @@ export default defineConfig({
|
|
64
77
|
|
65
78
|
**自控式路由**是通过组件代码自定义路由,需要应用运行起来才能获取准确的路由信息。因此,无法开箱即用的使用 SSG 功能。此时需要用户提前告知 Modern.js 框架,哪些路由需要开启 SSG 功能。
|
66
79
|
|
67
|
-
|
68
|
-
|
69
|
-
```
|
70
|
-
import {
|
71
|
-
|
72
|
-
|
73
|
-
<Switch>
|
74
|
-
<Route path="/" exact={true}>
|
75
|
-
<div>Home</div>
|
76
|
-
</Route>
|
77
|
-
<Route path="/foo" exact={true}>
|
78
|
-
<div>Foo</div>
|
79
|
-
</Route>
|
80
|
-
</Switch>
|
81
|
-
);
|
82
|
-
```
|
80
|
+
例如有以下代码,包含多条路由,设置 `output.ssg` 为 `true` 时,默认只会渲染入口路由即 `/`:
|
81
|
+
|
82
|
+
```tsx title="src/App.tsx"
|
83
|
+
import { useRuntimeContext } from '@modern-js/runtime';
|
84
|
+
import { Routes, Route, BrowserRouter } from '@modern-js/runtime/router';
|
85
|
+
import { StaticRouter } from '@modern-js/runtime/router/server';
|
83
86
|
|
84
|
-
|
87
|
+
const Router = typeof window === 'undefined' ? StaticRouter : BrowserRouter;
|
88
|
+
|
89
|
+
export default () => {
|
90
|
+
const { context } = useRuntimeContext();
|
91
|
+
return (
|
92
|
+
<Router location={context.request.pathname}>
|
93
|
+
<Routes>
|
94
|
+
<Route index element={<div>index</div>} />
|
95
|
+
<Route path="about" element={<div>about</div>} />
|
96
|
+
</Routes>
|
97
|
+
</Router>
|
98
|
+
);
|
99
|
+
};
|
100
|
+
```
|
85
101
|
|
86
|
-
|
102
|
+
如果我们希望同时开启 `/about` 的 SSG 功能,可以配置 `output.ssg`,告知 Modern.js 开启指定路由的 SSG 功能。
|
87
103
|
|
88
104
|
```typescript title="modern.config.ts"
|
89
105
|
export default defineConfig({
|
90
106
|
output: {
|
91
107
|
ssg: {
|
92
|
-
routes: ['/', '/
|
108
|
+
routes: ['/', '/about'],
|
93
109
|
},
|
94
110
|
},
|
95
111
|
})
|
96
112
|
```
|
97
113
|
|
98
|
-
执行 `pnpm run build` 与 `pnpm run start` 后,访问 `http://localhost:8080/
|
99
|
-
|
100
|
-
查看构建产物文件,可以看到 `dist/` 目录中,在默认的 `main` 入口产物目录下,新增一个 `foo.html` 文件:
|
114
|
+
执行 `pnpm run build` 与 `pnpm run start` 后,访问 `http://localhost:8080/about`,在 Preview 视图中可以看到页面已经完成渲染。
|
101
115
|
|
102
|
-
|
116
|
+
查看构建产物文件,可以看到 `dist/` 目录中,新增了一个 `main/about/index.html` 文件。
|
103
117
|
|
104
118
|
:::info
|
105
|
-
|
119
|
+
以上仅介绍了单入口的情况,更多相关内容可以查看 [API 文档](/docs/configure/app/output/ssg)。
|
106
120
|
:::
|
107
121
|
|
108
122
|
### 获取数据
|
@@ -116,36 +130,33 @@ export default defineConfig({
|
|
116
130
|
- 在开发阶段,不管 `useLoader` 是否配置 `{ static: true }`,函数都会在 SSR 时获取数据。
|
117
131
|
:::
|
118
132
|
|
119
|
-
修改上述 `App.ts` 的代码为:
|
133
|
+
修改上述 `src/App.ts` 的代码为:
|
120
134
|
|
121
|
-
```
|
122
|
-
import {
|
123
|
-
import {
|
135
|
+
```tsx title="App.ts"
|
136
|
+
import { useRuntimeContext, useStaticLoader } from '@modern-js/runtime';
|
137
|
+
import { Routes, Route, BrowserRouter } from '@modern-js/runtime/router';
|
138
|
+
import { StaticRouter } from '@modern-js/runtime/router/server';
|
139
|
+
|
140
|
+
const Router = typeof window === 'undefined' ? StaticRouter : BrowserRouter;
|
124
141
|
|
125
142
|
export default () => {
|
126
|
-
const {
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
static: true,
|
132
|
-
params: 'foo',
|
133
|
-
},
|
134
|
-
);
|
143
|
+
const { context } = useRuntimeContext();
|
144
|
+
|
145
|
+
const { data } = useStaticLoader(async () => ({
|
146
|
+
message: Math.random(),
|
147
|
+
}));
|
135
148
|
|
136
149
|
return (
|
137
|
-
<
|
138
|
-
<
|
139
|
-
<div>
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
</Route>
|
144
|
-
</Switch>
|
150
|
+
<Router location={context.request.pathname}>
|
151
|
+
<Routes>
|
152
|
+
<Route index element={<div>index</div>} />
|
153
|
+
<Route path="about" element={<div>about, {data?.message}</div>} />
|
154
|
+
</Routes>
|
155
|
+
</Router>
|
145
156
|
);
|
146
157
|
};
|
147
158
|
```
|
148
159
|
|
149
160
|
执行 `pnpm run dev`,重复刷新页面,可以看到 `/foo` 页面的渲染结果不断发生变化,说明数据是在请求时获取的。
|
150
161
|
|
151
|
-
重新执行 `pnpm run build` 后,执行 `pnpm run start
|
162
|
+
重新执行 `pnpm run build` 后,执行 `pnpm run start`,重复刷新页面,发现页面渲染结果始终保持同样的内容,数据在请求时不会再次获取,说明页面在编译时已经完成渲染。
|