@modern-js/main-doc 2.51.0 → 2.52.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,7 +12,7 @@ UnstableMiddleware will replace [Middleware](/apis/app/runtime/web-server/middle
12
12
  ```ts title="server/index.ts"
13
13
  import { UnstableMiddleware } from '@modern-js/runtime/server';
14
14
 
15
- export const unstableMiddlewares: UnstableMiddleware[] = [];
15
+ export const unstableMiddleware: UnstableMiddleware[] = [];
16
16
  ```
17
17
 
18
18
  ## Types
@@ -74,7 +74,7 @@ const time: UnstableMiddleware = async (c, next) => {
74
74
  console.log(`${end - start}`);
75
75
  };
76
76
 
77
- export const unstableMiddlewares: UnstableMiddleware = [time];
77
+ export const unstableMiddleware: UnstableMiddleware[] = [time];
78
78
  ```
79
79
 
80
80
  ### Injecting Server Data for DataLoader Consumption
@@ -101,7 +101,7 @@ const setPayload: UnstableMiddleware = async (
101
101
  await next();
102
102
  };
103
103
 
104
- export const unstableMiddlewares: UnstableMiddleware = [setPayload];
104
+ export const unstableMiddleware: UnstableMiddleware[] = [setPayload];
105
105
  ```
106
106
 
107
107
  ```ts title="src/routes/page.data.ts"
@@ -130,5 +130,31 @@ const auth: UnstableMiddleware = async (c, next) => {
130
130
  await next();
131
131
  };
132
132
 
133
- export const unstableMiddlewares: UnstableMiddleware = [auth];
133
+ export const unstableMiddleware: UnstableMiddleware[] = [auth];
134
+ ```
135
+
136
+ ### Modify Response
137
+
138
+ ```ts
139
+ import { UnstableMiddleware } from '@modern-js/runtime/server';
140
+
141
+ const modifier: UnstableMiddleware = async (c, next) => {
142
+ await next();
143
+
144
+ const { response } = c;
145
+
146
+ const text = await response.text();
147
+
148
+ const newText = text.replace('<html>', `<html lang="${language}">`);
149
+
150
+ const newheaders = response.headers;
151
+ newheaders.set('x-custom-value', 'modern');
152
+
153
+ c.response = c.body(newText, {
154
+ status: response.status,
155
+ headers: newheaders,
156
+ });
157
+ };
158
+
159
+ export const unstableMiddleware: UnstableMiddleware[] = [modifier];
134
160
  ```
@@ -90,7 +90,9 @@ Modern.js provides a set of APIs by default for projects to use:
90
90
  import { Middleware } from '@modern-js/runtime/server';
91
91
 
92
92
  export const middleware: Middleware = (context, next) => {
93
- const { source: { req, res } } = context;
93
+ const {
94
+ source: { req, res },
95
+ } = context;
94
96
  console.log(req.url);
95
97
  next();
96
98
  };
@@ -108,7 +110,7 @@ Projects should follow these best practices when using Middleware:
108
110
 
109
111
  **In general, in CSR projects, using Hook can basically meet all the needs of simple scenarios. In SSR projects, Middleware can be used for more complex Node extensions.**
110
112
 
111
- ### UnstableMiddleware
113
+ ### Unstable Middleware
112
114
 
113
115
  Modern.js will provide new Middleware to add pre-processing middleware to the Web Server, supporting the execution of custom logic before and after handling the page.
114
116
 
@@ -155,6 +155,8 @@ function loader() {
155
155
 
156
156
  ### Error Handling
157
157
 
158
+ #### Basic Usage
159
+
158
160
  In the `loader` function, errors can be handled by throwing an `error` or a `response`. When an error is thrown in the `loader` function, Modern.js will stop executing the code in the current `loader` and switch the front-end UI to the defined [`ErrorBoundary`](/guides/basic-features/routes#error-handling) component:
159
161
 
160
162
  ```tsx
@@ -182,6 +184,32 @@ const ErrorBoundary = () => {
182
184
  export default ErrorBoundary;
183
185
  ```
184
186
 
187
+ #### Practice
188
+
189
+ In an SSR project you can control the status code of a page and display the corresponding UI by `throw response` in the data loader, as in the following example, where there is one loader for the entire routing route
190
+
191
+ For example, if there is a loader in the entire routing route that throws a response, the status code of the page will be consistent with this `response` and the UI of the page will switch to `ErrorBoundary`.
192
+
193
+ ```ts
194
+ // routes/user/profile/page.data.ts
195
+ export async function loader() {
196
+ const user = await fetchUser();
197
+ if(!user){
198
+ throw new Response('The user was not found', { status: 404 });
199
+ }
200
+ return user;
201
+ }
202
+
203
+ // routes/error.tsx
204
+ import { useRouteError } from '@modern-js/runtime/router';
205
+ const ErrorBoundary = () => {
206
+ const error = useRouteError() as { data: string };
207
+ return <div className="error">{error.data}</div>;
208
+ };
209
+
210
+ export default ErrorBoundary;
211
+ ```
212
+
185
213
  ### Get data from parent component
186
214
 
187
215
  In many cases, child components need to access data in the parent component `loader`. You can easily get the data from the parent component using `useRouteLoaderData`:
@@ -2,18 +2,20 @@
2
2
  sidebar_position: 15
3
3
  ---
4
4
 
5
- # Deploy
5
+ # Deploy Application
6
6
 
7
- Currently, Modern.js can run in the node.js environment, you can host it yourself. At the same time, Modern.js officially supports deployment on the Netlify platform.
7
+ Currently, Modern.js offers two deployment way:
8
+ - You can host your application in a container that includes a Node.js environment on your own, which provides flexibility for the deployment of the application.
9
+ - You can also deploy your application through a platform. Currently, Modern.js supports the [Netlify](https://www.netlify.com/) and [Vercel](https://vercel.com/).
8
10
 
9
11
  :::info
10
- Currently Modern.js only supports running in node.js environments, support for other runtime environments will be provided in a later version.
12
+ Currently, Modern.js only supports running in a Node.js environment. Support for more runtime environments will be provided in the future.
11
13
  :::
12
14
 
13
15
 
14
- ## Commands to build products
16
+ ## Build Deployment Products
15
17
 
16
- Running the `modern deploy` command will automatically build the output file for the production environment. This process includes optimizing the output file and its dependencies, and detecting the current deployment platform and automatically generating a product that will run on that platform.
18
+ Running the `modern deploy` command will automatically produce deployment products. This process includes optimizing Bundler build products and their dependencies, detecting the current deployment platform, and automatically generating deployment products that can run on that platform.
17
19
 
18
20
  If you want to generate and test the output locally for a specific deployment platform, you can specify the platform by setting the environment variable: `modern deploy`:
19
21
 
@@ -33,11 +35,12 @@ When deploying on the deployment platforms officially supported by Modern.js, th
33
35
  By default, Modern.js outputs builds that can be run in a Node.js environment when no Modern.js-supported deployment platform is detected.
34
36
 
35
37
  Use the following command to build the project:
38
+
36
39
  ```bash
37
40
  npx modern deploy
38
41
  ```
39
42
 
40
- When running the `modern deploy` command, Modern.js will generate runnable products and output the following content:
43
+ When running the `modern deploy` command, Modern.js will generate runnable products and output the following content in terminal:
41
44
 
42
45
  ```bash
43
46
  Static directory: `.output/static`
@@ -56,35 +59,35 @@ PORT=3000 node .output/index
56
59
 
57
60
  ### Monorepo
58
61
 
59
- For the Monorepo project, in addition to building our current project, we also need to build other repositories that the current project depends on.
62
+ For Monorepo projects, in addition to building the current project, it is also necessary to build other sub-projects in the repository that the current project depends on.
60
63
 
61
- We assume that the name in the `package.json` of the current project is `app`. Taking pnpm as an example of a monorepo management tool, you can add the following command to the `package.json` of the current project to build products for the current project:
64
+ Assume that the name in the `package.json` of the current project is `app`. Taking pnpm as an example of a monorepo management tool, you can add the following command to the `package.json` of the current project to build products for the current project:
62
65
 
63
66
  ```json title="app/package.json"
64
67
  {
65
68
  "scripts": {
66
- "deploy": "modern deploy",
67
- "deploy:app": "pnpm --filter 'app^...' run build && pnpm --filter=app run deploy",
69
+ "build:packages": "pnpm --filter 'app^...' run build",
70
+ "deploy": "pnpm run build:packages && modern deploy",
68
71
  }
69
72
  }
70
73
  ```
71
74
 
72
- If you use rush to manage repositories, you can add the following command in `package.json`:
75
+ If you use Rush as your Monorepo management tool, you can add the following commands to your `package.json`:
73
76
 
74
77
  ```json
75
78
  {
76
79
  "scripts": {
77
- "deploy": "modern deploy",
78
- "deploy:app": "rush rebuild --to-except app && rushx deploy",
80
+ "build:packages": "rush rebuild --to-except app",
81
+ "deploy": "rushx build:packages && modern deploy",
79
82
  }
80
83
  }
81
84
  ```
82
85
 
83
- After the build is completed, Modern.js will generate all dependencies in the `.output/node_modules` directory of the project. Similarly, you can run the entire server using `node .output/index`.
86
+ After the build is completed, Modern.js will generate all dependencies in the `.output/node_modules` directory of the project. Similarly, you can run the Modern.js server using `node .output/index`.
84
87
 
85
88
  ## Netlify
86
89
 
87
- Netlify is a popular web development platform designed for building, deploying, and maintaining modern web projects. Deploying on Netlify mainly requires configuring the `netlify.toml` file.
90
+ Netlify is a popular Web development platform designed for building, deploying, and maintaining modern web projects. Deploying on Netlify mainly requires configuring the `netlify.toml` file.
88
91
 
89
92
  Depending on the complexity of your project, you can configure it incrementally by this doc.
90
93
 
@@ -93,31 +96,31 @@ Depending on the complexity of your project, you can configure it incrementally
93
96
  Add the `netlify.toml` file to the root directory of the current project:
94
97
 
95
98
  ```bash
96
- ./
97
- ├── src/
99
+ .
100
+ ├── src
98
101
  ├── modern.config.ts
99
102
  ├── netlify.toml
100
- ├── package.json
103
+ └── package.json
101
104
  ```
102
105
 
103
106
  Add the following content to `netlify.toml`:
107
+
104
108
  ```toml
105
109
  [build]
106
110
  publish = "dist"
107
- command = "npm run deploy"
111
+ command = "modern deploy"
108
112
  ```
109
113
 
110
- Add a project to the Netlify platform and deploy it!
114
+ Now, add a project to the Netlify platform and deploy it!
111
115
 
112
116
  ### Full Stack Project
113
117
 
114
- Full-stack projects refer to projects that use custom servers, SSR, and BFF. These projects need to be deployed on Netlify Functions. Based on the `netlify.toml` file mentioned above,
115
- add the following configuration:
118
+ Full-stack projects refer to projects that use Custom Web Server, SSR or BFF. These projects need to be deployed on **Netlify Functions**. Based on the `netlify.toml` file mentioned above, add the following configuration:
116
119
 
117
120
  ```toml title="netlify.toml"
118
121
  [build]
119
122
  publish = "dist"
120
- command = "npm run deploy"
123
+ command = "modern deploy"
121
124
 
122
125
  [functions]
123
126
  directory = ".netlify/functions"
@@ -133,31 +136,40 @@ Currently, Modern.js does not support deployment on Netlify Edge Functions. We w
133
136
 
134
137
  ### Monorepo
135
138
 
136
- For Monorepo projects, in addition to building our current project, we also need to build the dependencies of the current project from other repositories.
137
- We take a pnpm monorepo repository as an example and deploy the Monorepo project on Netlify.
139
+ :::info
140
+ The following guide is mainly for full-stack projects, for pure CSR projects, just follow [Pure Frontend Project](#Pure Frontend Project) to deploy.
141
+ :::
142
+
143
+ For Monorepo projects, in addition to building our current project, you also need to build other sub-projects in the repository that the current project depends on.
138
144
 
139
- Suppose we have the following Monorepo repository:
145
+ We take a pnpm Monorepo repository as an example and deploy the Monorepo project on Netlify.
146
+
147
+ Assuming the directory structure of the Monorepo repository is as follows:
140
148
 
141
149
  ```
142
- ./
143
- ├── packages/
144
- │ ├── app/
150
+ .
151
+ ├── packages
152
+ │ ├── app
145
153
  │ └── app-dep1
146
154
  ├── package.json
147
155
  ├── pnpm-lock.yaml
148
156
  └── pnpm-workspace.yaml
149
157
  ```
150
158
 
151
- We need to configure Base directory on the netlify platform as `packages/app`:
159
+ You need to configure Base directory on the netlify platform as `packages/app`:
152
160
 
153
161
  <img src="https://sf16-sg.tiktokcdn.com/obj/eden-sg/lmeh7nuptpfnuhd/netlify-monorepo-basedir.png?x-resource-account=public" />
154
162
 
155
163
  Add the following script in `packages/app/package.json`, before executing the deployment command of the `app` repository, first execute the build of other repositories in the workspace:
156
164
 
157
165
  ```json
158
- "scripts": {
159
- "deploy:app": "pnpm --filter 'app^...' run build && pnpm --filter=app run deploy",
166
+ {
167
+ "scripts": {
168
+ "build:packages": "pnpm --filter 'app^...' run build",
169
+ "deploy": "pnpm run build:packages && modern deploy",
170
+ }
160
171
  }
172
+
161
173
  ```
162
174
 
163
175
  Configure the build command in `netlify.toml`:
@@ -165,7 +177,7 @@ Configure the build command in `netlify.toml`:
165
177
  ```toml
166
178
  [build]
167
179
  publish = "dist"
168
- command = "npm run deploy:app"
180
+ command = "npm run deploy"
169
181
 
170
182
  [functions]
171
183
  directory = ".netlify/functions"
@@ -174,3 +186,101 @@ Configure the build command in `netlify.toml`:
174
186
  ```
175
187
 
176
188
  Just submit your code and deploy it using the Netlify platform.
189
+
190
+ ## Vercel
191
+
192
+ Vercel is a deployment platform for modern web applications that provides a rich set of features to support deploying static websites, server-side rendered applications, and more. To deploy on Vercel, you usually need to configure the `vercel.json` file, which you can configure incrementally depending on the complexity of your project.
193
+
194
+ ### Pure Front-end Project
195
+
196
+ Add the `vercel.json` file to the root directory of the current project:
197
+ ```bash
198
+ ./
199
+ ├── src
200
+ ├── modern.config.ts
201
+ ├── vercel.json
202
+ └── package.json
203
+ ```
204
+
205
+ Add the following content to `vercel.json`:
206
+ ```json title="vercel.json"
207
+ {
208
+ "buildCommand": "modern deploy",
209
+ "outputDirectory": ".vercel/output"
210
+ }
211
+ ```
212
+
213
+ Commit your project to git, select Framework Preset as `Other` on the Vercel platform and deploy.
214
+
215
+ <img src="https://sf16-sg.tiktokcdn.com/obj/eden-sg/lmeh7nuptpfnuhd/vercel-framework-preset.png" />
216
+
217
+ ### Full Stack Project
218
+
219
+ Full-stack projects refer to projects that use Custom Web Server, SSR or BFF. These projects need to be deployed on **Vercel Functions**.
220
+
221
+ In addition to configuring `vercel.json` in the same way as a [pure front-end project](#pure-front-end-project), there are two points to note for full-stack projects:
222
+
223
+ 1. Currently, Modern.js does not support deploying BFF projects on the Vercel platform. We will support it in future versions.
224
+ 2. When deploying on Vercel platform, the default node runtime is `20.x`, it is recommended to choose `18.x` when deploying full-stack projects,
225
+ see [Serverless Function contains invalid runtime error](https://vercel.com/guides/serverless-function-contains-invalid-runtime-error), you can modify `package.json` to specify the version:
226
+ ```json title="package.json"
227
+ "engines": {
228
+ "node": "18.x"
229
+ }
230
+ ```
231
+
232
+
233
+ ### Monorepo
234
+
235
+ :::info
236
+ The following guide is mainly for full-stack projects, for pure CSR projects, just follow [Pure Frontend Project](#pure-front-end-project-1) to deploy.
237
+ :::
238
+
239
+ For Monorepo projects, in addition to building our current project, you also need to build other sub-projects in the repository that the current project depends on.
240
+
241
+ We take a pnpm Monorepo repository as an example and deploy the Monorepo project on Vercel.
242
+
243
+ Assuming the directory structure of the Monorepo repository is as follows:
244
+
245
+ ```
246
+ .
247
+ ├── packages
248
+ │ ├── app
249
+ │ └── app-dep1
250
+ ├── package.json
251
+ ├── pnpm-lock.yaml
252
+ └── pnpm-workspace.yaml
253
+ ```
254
+
255
+ First, you need to configure the **Root Directory** as `packages/app` on the Vercel platform:
256
+
257
+ <img src="https://sf16-sg.tiktokcdn.com/obj/eden-sg/lmeh7nuptpfnuhd/vercel-root-directory.png" />
258
+
259
+ Specify Node.js runtime as `18.x`:
260
+
261
+ ```json title="package.json"
262
+ "engines": {
263
+ "node": "18.x"
264
+ }
265
+ ````
266
+
267
+ Add the following script to `packages/app/package.json` to run `build` command of the other repositories in the workspace before run the `deploy` command for the `app` repository:
268
+
269
+ ```json
270
+ {
271
+ "scripts": {
272
+ "build:packages": "pnpm --filter 'app^...' run build",
273
+ "deploy": "pnpm run build:packages && modern deploy",
274
+ }
275
+ }
276
+ ```
277
+
278
+ Add the following content to the `packages/app/vercel.json` file:
279
+ ```json title="vercel.json"
280
+ {
281
+ "buildCommand": "npm run deploy",
282
+ "outputDirectory": ".vercel/output"
283
+ }
284
+ ```
285
+
286
+ Just submit your code and deploy it using the Vercel platform.
@@ -11,7 +11,7 @@ title: Unstable Middleware
11
11
  ```ts title="server/index.ts"
12
12
  import { UnstableMiddleware } from '@modern-js/runtime/server';
13
13
 
14
- export const unstableMiddlewares: UnstableMiddleware = [];
14
+ export const unstableMiddleware: UnstableMiddleware[] = [];
15
15
  ```
16
16
 
17
17
  ## 类型
@@ -73,7 +73,7 @@ const time: UnstableMiddleware = async (c, next) => {
73
73
  console.log(`${end - start}`);
74
74
  };
75
75
 
76
- export const unstableMiddlewares: UnstableMiddleware = [time];
76
+ export const unstableMiddleware: UnstableMiddleware[] = [time];
77
77
  ```
78
78
 
79
79
  ### 注入服务端数据,供页面 dataLoader 消费
@@ -100,7 +100,7 @@ const setPayload: UnstableMiddleware = async (
100
100
  await next();
101
101
  };
102
102
 
103
- export const unstableMiddlewares: UnstableMiddleware = [setPayload];
103
+ export const unstableMiddleware: UnstableMiddleware[] = [setPayload];
104
104
  ```
105
105
 
106
106
  ```ts title="src/routes/page.data.ts"
@@ -129,5 +129,31 @@ const auth: UnstableMiddleware = async (c, next) => {
129
129
  await next();
130
130
  };
131
131
 
132
- export const unstableMiddlewares: UnstableMiddleware = [auth];
132
+ export const unstableMiddleware: UnstableMiddleware[] = [auth];
133
+ ```
134
+
135
+ ### 修改响应体
136
+
137
+ ```ts
138
+ import { UnstableMiddleware } from '@modern-js/runtime/server';
139
+
140
+ const modifier: UnstableMiddleware = async (c, next) => {
141
+ await next();
142
+
143
+ const { response } = c;
144
+
145
+ const text = await response.text();
146
+
147
+ const newText = text.replace('<html>', `<html lang="${language}">`);
148
+
149
+ const newheaders = response.headers;
150
+ newheaders.set('x-custom-value', 'modern');
151
+
152
+ c.response = c.body(newText, {
153
+ status: response.status,
154
+ headers: newheaders,
155
+ });
156
+ };
157
+
158
+ export const unstableMiddleware: UnstableMiddleware[] = [modifier];
133
159
  ```
@@ -108,7 +108,7 @@ export const middleware: Middleware = (context, next) => {
108
108
 
109
109
  **总的来说,CSR 项目中,使用 Hook 基本能满足简单场景的所有需求。SSR 项目中,可以使用 Middleware 做更复杂的 Node 扩展。**
110
110
 
111
- ### UnstableMiddleware
111
+ ### Unstable Middleware
112
112
 
113
113
  Modern.js 将提供新 Middleware 为 Web Server 添加前置中间件,支持在处理页面的前后执行自定义逻辑。
114
114
 
@@ -155,6 +155,8 @@ export async function loader() {
155
155
 
156
156
  ### 错误处理
157
157
 
158
+ #### 基本用法
159
+
158
160
  在 `loader` 函数中,可以通过 `throw error` 或者 `throw response` 的方式处理错误,当 `loader` 函数中有错误被抛出时,Modern.js 会停止执行当前 `loader` 中的代码,并将前端 UI 切换到定义的 [`ErrorBoundary`](/guides/basic-features/routes#错误处理) 组件:
159
161
 
160
162
  ```tsx
@@ -182,6 +184,31 @@ const ErrorBoundary = () => {
182
184
  export default ErrorBoundary;
183
185
  ```
184
186
 
187
+ #### 实践
188
+
189
+ 在 SSR 项目中你可以通过在 data loader 中 `throw response` 的方式,控制页面的状态码,展示对应的 UI,如以下示例,整条路由路线中有一个 loader
190
+ throw response,页面的状态码将与这个 `response` 保持一致,页面的 UI 也会切换为 `ErrorBoundary`:
191
+
192
+ ```ts
193
+ // routes/user/profile/page.data.ts
194
+ export async function loader() {
195
+ const user = await fetchUser();
196
+ if(!user){
197
+ throw new Response('The user was not found', { status: 404 });
198
+ }
199
+ return user;
200
+ }
201
+
202
+ // routes/error.tsx
203
+ import { useRouteError } from '@modern-js/runtime/router';
204
+ const ErrorBoundary = () => {
205
+ const error = useRouteError() as { data: string };
206
+ return <div className="error">{error.data}</div>;
207
+ };
208
+
209
+ export default ErrorBoundary;
210
+ ```
211
+
185
212
  ### 获取上层组件的数据
186
213
 
187
214
  很多场景下,子组件需要获取到祖先组件 `loader` 中的数据,你可以通过 `useRouteLoaderData` 方便地获取到祖先组件的数据:
@@ -385,8 +412,6 @@ export const loader = () => {}
385
412
  ```
386
413
 
387
414
 
388
-
389
-
390
415
  ### 错误用法
391
416
 
392
417
  1. `loader` 中只能返回可序列化的数据,在 SSR 环境下,`loader` 函数的返回值会被序列化为 JSON 字符串,然后在客户端被反序列化为对象。因此,`loader` 函数中不能返回不可序列化的数据(如函数)。
@@ -2,18 +2,21 @@
2
2
  sidebar_position: 15
3
3
  ---
4
4
 
5
- # 部署
5
+ # 部署应用
6
6
 
7
- 目前 Modern.js 支持在 node.js 环境下运行,你可以自行托管,同时,Modern.js 官方支持在 Netlify 平台上部署。
7
+ 目前,Modern.js 提供了两种部署方式:
8
+
9
+ - 你可以将应用自行托管在包含 Node.js 环境的容器中,这为应用提供了部署的灵活性。
10
+ - 你也可以通过平台部署应用,目前 Modern.js 官方支持了 [Netlify](https://www.netlify.com/) 和 [Vercel](https://vercel.com/) 平台。
8
11
 
9
12
  :::info
10
- 当前仅支持在 node.js 环境运行,对其他运行时环境的支持将在后续版本中提供。
13
+ 目前 Modern.js 仅支持在 Node.js 环境中运行,未来将提供更多运行时环境的支持。
11
14
  :::
12
15
 
13
16
 
14
- ## 构建产物的命令
17
+ ## 构建部署产物
15
18
 
16
- 执行 `modern deploy` 命令将自动构建适用于生产环境的输出文件。此过程包括优化输出文件及其依赖,并检测当前部署平台,自动生成可以在该平台运行的产物。
19
+ 执行 `modern deploy` 命令将自动输出部署产物。此过程包括优化 Bundler 构建产物及产物依赖,检测当前部署平台,并自动生成可以在该平台运行的产物。
17
20
  如果你希望在本地生成并测试特定部署平台的产物,可以通过设置环境变量来指定平台:
18
21
 
19
22
  ```bash
@@ -29,24 +32,26 @@ MODERNJS_DEPLOY=netlify npx modern deploy
29
32
 
30
33
  ### 单仓库项目
31
34
 
32
- 默认情况下,当未检测到 Modern.js 支持的部署平台时,Modern.js 会输出可以在 Node.js 环境下运行的构建产物。
35
+ 默认情况下,如果未检测到 Modern.js 支持的部署平台,Modern.js 将生成可以在 Node.js 环境下运行的部署产物。
36
+
37
+ 你可以使用以下命令构建项目:
33
38
 
34
- 使用以下命令构建项目:
35
39
  ```bash
36
40
  npx modern deploy
37
41
  ```
38
42
 
39
- 当执行 `modern deploy` 命令时,Modern.js 会产出可运行的生产环境产物,并输出以下内容:
43
+ 当执行 `modern deploy` 命令时,Modern.js 将生成可执行的部署产物,并在控制台输出以下内容:
40
44
 
41
45
  ```bash
42
46
  Static directory: `.output/static`
43
47
  You can preview this build by `node .output/index`
44
48
  ```
45
49
 
46
- 此时,你可以通过 `node .output/index` 运行整个 server,在 `.output/static` 目录下是页面所需的静态资源,你可以将这些静态资源自行上传至 CDN
50
+ 现在,你可以通过执行 `node .output/index` 命令来运行服务器。在 `.output/static` 目录中,存放了页面运行所需的静态资源,你可以选择将这些资源上传到 CDN 以提高访问速度。
47
51
 
48
52
  :::info
49
- 默认情况下,运行 Modern.js Server 时会监听 8080 端口,如果你想修改监听的端口,可以指定 `PORT` 环境变量:
53
+ 默认情况下,运行 Modern.js 服务器时会监听 8080 端口,如果你想修改监听的端口,可以指定 `PORT` 环境变量:
54
+
50
55
  ```
51
56
  PORT=3000 node .output/index
52
57
  ```
@@ -55,36 +60,35 @@ PORT=3000 node .output/index
55
60
 
56
61
  ### Monorepo
57
62
 
58
- 对于 Monorepo 项目,除了需要构建我们当前的项目外,还需要构建当前项目的依赖的其他仓库。
63
+ 对于 Monorepo 项目,除了需要构建当前的项目外,还需要构建当前项目依赖的仓库中其他子项目。
59
64
 
60
- 假设当前项目的 `package.json` 中的 name 为 `app`,以 pnpm 作为 monorepo 管理工具为例,你可以在当前项目的 `package.json` 添加以下命令用于构建当前项目的产物:
65
+ 假设当前项目的 `package.json` 中的 name 为 `app`,以 pnpm 作为 Monorepo 管理工具为例,你可以在项目 `package.json` 中添加以下命令用于构建:
61
66
 
62
67
  ```json title="app/package.json"
63
68
  {
64
69
  "scripts": {
65
- "deploy": "modern deploy",
66
- "deploy:app": "pnpm --filter 'app^...' run build && pnpm --filter=app run deploy",
70
+ "build:packages": "pnpm --filter 'app^...' run build",
71
+ "deploy": "pnpm run build:packages && modern deploy",
67
72
  }
68
73
  }
69
74
  ```
70
75
 
71
- 如果你使用 rush 管理仓库,可以在 `package.json` 中添加以下命令:
76
+ 如果你使用 rush 作为 Monorepo 管理工具,可以在 `package.json` 中添加以下命令:
72
77
 
73
78
  ```json
74
79
  {
75
80
  "scripts": {
76
- "deploy": "modern deploy",
77
- "deploy:app": "rush rebuild --to-except app && rushx deploy",
81
+ "build:packages": "rush rebuild --to-except app",
82
+ "deploy": "rushx build:packages && modern deploy",
78
83
  }
79
84
  }
80
85
  ```
81
86
 
82
- 当构建完成后,Modern.js 会将项目中所有的依赖生成在 `.output/node_modules` 目录下。同样的,你可以使用 `node .output/index` 运行整个 Server。
87
+ 构建完成后,框架会将项目中所有的依赖生成在 `.output/node_modules` 目录下。你同样可以使用 `node .output/index` 运行 Modern.js 服务器。
83
88
 
84
89
  ## Netlify
85
90
 
86
- Netlify 是一个流行的 web 开发平台,专为构建、发布和维护现代 web 项目而设计,在 Netlify 上部署,主要需要配置 `netlify.toml` 文件,
87
- 根据你的项目复杂度,可以渐进配置。
91
+ Netlify 是一个流行的 Web 开发平台,专为构建、发布和维护现代 Web 项目而设计。在 Netlify 上部署,通常需要配置 `netlify.toml` 文件,你可以根据项目复杂度,渐进地配置该文件。
88
92
 
89
93
  ### 纯前端项目
90
94
 
@@ -92,30 +96,29 @@ Netlify 是一个流行的 web 开发平台,专为构建、发布和维护现
92
96
 
93
97
  ```bash
94
98
  ./
95
- ├── src/
99
+ ├── src
96
100
  ├── modern.config.ts
97
101
  ├── netlify.toml
98
- ├── package.json
102
+ └── package.json
99
103
  ```
100
104
 
101
105
  在 `netlify.toml` 中添加以下内容:
102
106
  ```toml
103
107
  [build]
104
108
  publish = "dist"
105
- command = "npm run deploy"
109
+ command = "modern deploy"
106
110
  ```
107
111
 
108
112
  在 Netlify 平台上添加项目,部署即可。
109
113
 
110
114
  ### 全栈项目
111
115
 
112
- 全栈项目是指使用了 自定义 ServerSSRBFF 的项目,这些项目需要部署在 Netlify Functions 上。基于上述的 `netlify.toml` 文件,
113
- 添加以下配置:
116
+ 全栈项目是指使用了自定义 Web ServerSSRBFF 的项目,这些项目需要部署在 **Netlify Functions** 上。你需要基于上述的 `netlify.toml` 文件,添加以下配置:
114
117
 
115
118
  ```toml title="netlify.toml"
116
119
  [build]
117
120
  publish = "dist"
118
- command = "npm run deploy"
121
+ command = "modern deploy"
119
122
 
120
123
  [functions]
121
124
  directory = ".netlify/functions"
@@ -131,30 +134,36 @@ Netlify 是一个流行的 web 开发平台,专为构建、发布和维护现
131
134
 
132
135
  ### Monorepo 项目
133
136
 
134
- 对于 Monorepo 项目,除了需要构建我们当前的项目外,还需要构建当前项目的依赖的其他仓库。我们以一个 pnpm monorepo 仓库为例,
135
- Netlify 上对 Monorepo 项目进行部署。
137
+ :::info
138
+ 以下指南主要针对于全栈项目,对于纯 CSR 的项目,只需要按照[纯前端项目](#纯前端项目)部署即可。
139
+ :::
140
+
141
+ 对于 Monorepo 项目,除了需要构建当前的项目外,还需要构建当前项目依赖的仓库中其他子项目。这里以一个 pnpm Monorepo 仓库为例,在 Netlify 上对 Monorepo 项目进行部署。
136
142
 
137
- 假设我们有以下 Monorepo 仓库:
143
+ 假设 Monorepo 仓库目录结构如下:
138
144
 
139
145
  ```
140
- ./
141
- ├── packages/
142
- │ ├── app/
146
+ .
147
+ ├── packages
148
+ │ ├── app
143
149
  │ └── app-dep1
144
150
  ├── package.json
145
151
  ├── pnpm-lock.yaml
146
152
  └── pnpm-workspace.yaml
147
153
  ```
148
154
 
149
- 我们需要在 netlify 平台上配置 Base directory 为 `packages/app`:
155
+ 你需要在 Netlify 平台上配置 **Base directory** 为 `packages/app`:
150
156
 
151
157
  <img src="https://sf16-sg.tiktokcdn.com/obj/eden-sg/lmeh7nuptpfnuhd/netlify-monorepo-basedir.png?x-resource-account=public" />
152
158
 
153
159
  在 `packages/app/package.json` 中添加以下 script,在执行 `app` 仓库的部署命令之前,先执行 workspace 中其他仓库的构建:
154
160
 
155
161
  ```json
156
- "scripts": {
157
- "deploy:app": "pnpm --filter 'app^...' run build && pnpm --filter=app run deploy",
162
+ {
163
+ "scripts": {
164
+ "build:packages": "pnpm --filter 'app^...' run build",
165
+ "deploy": "pnpm run build:packages && modern deploy",
166
+ }
158
167
  }
159
168
  ```
160
169
 
@@ -163,7 +172,7 @@ Netlify 是一个流行的 web 开发平台,专为构建、发布和维护现
163
172
  ```toml
164
173
  [build]
165
174
  publish = "dist"
166
- command = "npm run deploy:app"
175
+ command = "npm run deploy"
167
176
 
168
177
  [functions]
169
178
  directory = ".netlify/functions"
@@ -173,5 +182,100 @@ Netlify 是一个流行的 web 开发平台,专为构建、发布和维护现
173
182
 
174
183
  提交你的代码,使用 Netlify 平台部署即可。
175
184
 
185
+ ## Vercel
186
+
187
+ Vercel 是一个面向现代 Web 应用的部署平台,它提供了丰富的功能,支持部署静态网站,服务端渲染应用等。在 Vercel 上部署,通常需要配置 `vercel.json` 文件,你可以根据项目复杂度,渐进地配置该文件。
188
+
189
+ ### 纯前端项目
190
+
191
+ 在当前项目的根目录添加 `vercel.json` 文件:
192
+ ```bash
193
+ ./
194
+ ├── src
195
+ ├── modern.config.ts
196
+ ├── vercel.json
197
+ └── package.json
198
+ ```
199
+
200
+ 在 `vercel.json` 中添加以下内容:
201
+ ```json title="vercel.json"
202
+ {
203
+ "buildCommand": "modern deploy",
204
+ "outputDirectory": ".vercel/output"
205
+ }
206
+ ```
207
+
208
+ 提交你的项目到 git,在 Vercel 平台上选择 Frmeworkwork Preset 为 `Other`,部署即可。
209
+
210
+ <img src="https://sf16-sg.tiktokcdn.com/obj/eden-sg/lmeh7nuptpfnuhd/vercel-framework-preset.png" />
211
+
212
+ ### 全栈项目
213
+
214
+ 全栈项目是指使用了自定义 Web Server、SSR、BFF 的项目,这些项目需要部署在 **Vercel Functions** 上。
215
+
216
+ 全栈项目除了按照[纯前端项目](#纯前端项目)的方式配置 `vercel.json` 外,有两点需要注意:
217
+
218
+ 1. 当前,Modern.js 还不支持在 Vercel 平台上部署 BFF 项目,我们将在后续的版本中支持。
219
+ 2. Vercel 平台部署时,默认 node 运行时为 `20.x`,部署全栈项目时建议选择 `18.x`,具体原因详见[Serverless Function contains invalid runtime error](https://vercel.com/guides/serverless-function-contains-invalid-runtime-error),你可以修改 `package.json` 指定版本:
220
+ ```json title="package.json"
221
+ "engines": {
222
+ "node": "18.x"
223
+ }
224
+ ```
225
+
226
+
227
+ ### Monorepo 项目
228
+
229
+ :::info
230
+ 以下指南主要针对于全栈项目,对于纯 CSR 的项目,只需要按照[纯前端项目](#纯前端项目-1)部署即可。
231
+ :::
232
+
233
+ 对于 Monorepo 项目,除了需要构建当前的项目外,还需要构建当前项目依赖的仓库中其他子项目。这里以一个 pnpm Monorepo 仓库为例,在 Vercel 上对 Monorepo 项目进行部署。
234
+
235
+ 假设 Monorepo 仓库目录结构如下:
236
+
237
+ ```
238
+ .
239
+ ├── packages
240
+ │ ├── app
241
+ │ └── app-dep1
242
+ ├── package.json
243
+ ├── pnpm-lock.yaml
244
+ └── pnpm-workspace.yaml
245
+ ```
246
+
247
+ 首先,你需要在 Vercel 平台上配置 **Root Directory** 为 `packages/app`:
248
+
249
+ <img src="https://sf16-sg.tiktokcdn.com/obj/eden-sg/lmeh7nuptpfnuhd/vercel-root-directory.png" />
250
+
251
+ 将 Node.js 运行时设置为 `18.x`:
252
+ ```json title="package.json"
253
+ "engines": {
254
+ "node": "18.x"
255
+ },
256
+ ```
257
+
258
+ 在 `packages/app/package.json` 中添加以下 script,在执行 `app` 仓库的部署命令之前,先执行 workspace 中其他仓库的构建:
259
+
260
+ ```json
261
+ {
262
+ "scripts": {
263
+ "build:packages": "pnpm --filter 'app^...' run build",
264
+ "deploy": "pnpm run build:packages && modern deploy",
265
+ }
266
+ }
267
+ ```
268
+
269
+ 在 `packages/app/vercel.json` 文件中添加以下内容:
270
+ ```json title="vercel.json"
271
+ {
272
+ "buildCommand": "npm run deploy",
273
+ "outputDirectory": ".vercel/output"
274
+ }
275
+ ```
276
+
277
+ 提交你的代码,使用 Vercel 平台部署即可。
278
+
279
+
176
280
 
177
281
 
package/package.json CHANGED
@@ -15,17 +15,17 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.51.0",
18
+ "version": "2.52.0",
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": "2.51.0"
25
+ "@modern-js/sandpack-react": "2.52.0"
26
26
  },
27
27
  "peerDependencies": {
28
- "@modern-js/builder-doc": "^2.51.0"
28
+ "@modern-js/builder-doc": "^2.52.0"
29
29
  },
30
30
  "devDependencies": {
31
31
  "classnames": "^2",
@@ -39,8 +39,8 @@
39
39
  "@rspress/shared": "1.18.2",
40
40
  "@types/node": "^16",
41
41
  "@types/fs-extra": "9.0.13",
42
- "@modern-js/builder-doc": "2.51.0",
43
- "@modern-js/doc-plugin-auto-sidebar": "2.51.0"
42
+ "@modern-js/builder-doc": "2.52.0",
43
+ "@modern-js/doc-plugin-auto-sidebar": "2.52.0"
44
44
  },
45
45
  "scripts": {
46
46
  "dev": "rspress dev",