You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Server Components are a new type of Component that renders ahead of time, before bundling, in an environment separate from your client app or SSR server.
13
+
服务器组件是一种新型的组件,它在打包之前,在独立于客户端应用程序或 SSR 服务器的环境中提前渲染。
14
14
15
15
</Intro>
16
16
17
-
This separate environment is the "server" in React Server Components. Server Components can run once at build time on your CI server, or they can be run for each request using a web server.
17
+
React 服务器组件中的「服务器」就是指这个独立的环境。服务器组件可以在构建时在你的 CI 服务器上运行一次,也可以在每次请求时在 Web 服务器中运行。
18
18
19
19
<InlineToc />
20
20
21
21
<Note>
22
22
23
-
#### How do I build support for Server Components? {/*how-do-i-build-support-for-server-components*/}
While React Server Components in React 19 are stable and will not break between minor versions, the underlying APIs used to implement a React Server Components bundler or framework do not follow semver and may break between minors in React 19.x.
To support React Server Components as a bundler or framework, we recommend pinning to a specific React version, or using the Canary release. We will continue working with bundlers and frameworks to stabilize the APIs used to implement React Server Components in the future.
### Server Components without a Server {/*server-components-without-a-server*/}
32
-
Server components can run at build time to read from the filesystem or fetch static content, so a web server is not required. For example, you may want to read static data from a content management system.
This pattern means users need to download and parse an additional 75K (gzipped) of libraries, and wait for a second request to fetch the data after the page loads, just to render static content that will not change for the lifetime of the page.
//NOTE: loads *during* render, when the app is built.
70
+
//注意: 会在应用构建的 **渲染过程中** 加载
71
71
constcontent=awaitfile.readFile(`${page}.md`);
72
72
73
73
return<div>{sanitizeHtml(marked(content))}</div>;
74
74
}
75
75
```
76
76
77
-
The rendered output can then be server-side rendered (SSR) to HTML and uploaded to a CDN. When the app loads, the client will not see the original `Page`component, or the expensive libraries for rendering the markdown. The client will only see the rendered output:
77
+
渲染的输出接着可以被服务端渲染(SSR)成 HTML 并上传至 CDN。当应用加载时,客户端不会看到原始的 `Page`组件,也不会看到用于渲染 markdown 且体积较大的包。客户端只会看到最终渲染出来的 HTML 内容:
78
78
79
79
```js
80
80
<div><!-- html for markdown --></div>
81
81
```
82
82
83
-
This means the content is visible during first page load, and the bundle does not include the expensive libraries needed to render the static content.
### Server Components with a Server {/*server-components-with-a-server*/}
102
-
Server Components can also run on a web server during a request for a page, letting you access your data layer without having to build an API. They are rendered before your application is bundled, and can pass data and JSX as props to Client Components.
With Server Components, you can read the data and render it in the component:
153
+
使用服务器组件,你可以在组件中读取数据并渲染:
154
154
155
155
```js
156
156
importdbfrom'./database';
157
157
158
158
asyncfunctionNote({id}) {
159
-
//NOTE: loads *during* render.
159
+
//注意: 在 **渲染时** 加载。
160
160
constnote=awaitdb.notes.get(id);
161
161
return (
162
162
<div>
@@ -167,14 +167,14 @@ async function Note({id}) {
167
167
}
168
168
169
169
asyncfunctionAuthor({id}) {
170
-
//NOTE: loads *after* Note,
171
-
//but is fast if data is co-located.
170
+
//注意: 在 Note **之后** 加载,
171
+
//如果服务器组件和数据库在同一个位置(例如在同一台服务器上),这里读取数据的加载速度会很快。
172
172
constauthor=awaitdb.authors.get(id);
173
173
return<span>By: {author.name}</span>;
174
174
}
175
175
```
176
176
177
-
The bundler then combines the data, rendered Server Components and dynamic Client Components into a bundle. Optionally, that bundle can then be server-side rendered (SSR) to create the initial HTML for the page. When the page loads, the browser does not see the original `Note`and`Author`components; only the rendered output is sent to the client:
177
+
打包器接着会整合数据、渲染服务器组件并和动态客户端组件一起打成一个包。接着可以选择将这个包进行服务端渲染(SSR)以创建初始的 HTML 页面。当页面加载时,浏览器不会看到原始的 `Note`和`Author`组件,只有渲染后的输出才会发送到客户端:
178
178
179
179
```js
180
180
<div>
@@ -183,26 +183,26 @@ The bundler then combines the data, rendered Server Components and dynamic Clien
183
183
</div>
184
184
```
185
185
186
-
Server Components can be made dynamic by re-fetching them from a server, where they can access the data and render again. This new application architecture combines the simple “request/response” mental model of server-centric Multi-Page Apps with the seamless interactivity of client-centric Single-Page Apps, giving you the best of both worlds.
Server Components are not sent to the browser, so they cannot use interactive APIs like `useState`. To add interactivity to Server Components, you can compose them with Client Component using the `"use client"`directive.
A common misunderstanding is that Server Components are denoted by `"use server"`, but there is no directive for Server Components. The `"use server"`directive is used for Server Functions.
For more info, see the docs for [Directives](/reference/rsc/directives).
198
+
要了解更多信息,查看 [指令](/reference/rsc/directives) 的文档.
199
199
200
200
</Note>
201
201
202
202
203
-
In the following example, the `Notes`Server Component imports an `Expandable`Client Component that uses state to toggle its `expanded`state:
203
+
在如下示例中,`Notes`服务器组件导入了 `Expandable`客户端组件,这个组件使用 state 切换 `expanded`状态:
204
204
```js
205
-
//Server Component
205
+
//服务器组件
206
206
importExpandablefrom'./Expandable';
207
207
208
208
asyncfunctionNotes() {
@@ -219,7 +219,7 @@ async function Notes() {
219
219
}
220
220
```
221
221
```js
222
-
//Client Component
222
+
//客户端组件
223
223
"use client"
224
224
225
225
exportdefaultfunctionExpandable({children}) {
@@ -237,11 +237,11 @@ export default function Expandable({children}) {
237
237
}
238
238
```
239
239
240
-
This works by first rendering `Notes`as a Server Component, and then instructing the bundler to create a bundle for the Client Component `Expandable`. In the browser, the Client Components will see output of the Server Components passed as props:
Server Components introduce a new way to write Components using async/await. When you `await`in an async component, React will suspend and wait for the promise to resolve before resuming rendering. This works across server/client boundaries with streaming support for Suspense.
You can even create a promise on the server, and await it on the client:
264
+
你甚至可以在服务器上创建一个 promise,然后再客户端上 await 它。
265
265
266
266
```js
267
-
//Server Component
267
+
//服务器组件
268
268
importdbfrom'./database';
269
269
270
270
asyncfunctionPage({id}) {
271
-
//Will suspend the Server Component.
271
+
//使用 await 会使服务器组件暂停
272
272
constnote=awaitdb.notes.get(id);
273
273
274
-
//NOTE: not awaited, will start here and await on the client.
274
+
//注意: 没有使用 await, 所以从这里开始执行,但是客户端上面进行 await
275
275
constcommentsPromise=db.comments.get(note.id);
276
276
return (
277
277
<div>
@@ -285,18 +285,18 @@ async function Page({id}) {
285
285
```
286
286
287
287
```js
288
-
//Client Component
288
+
//客户端组件
289
289
"use client";
290
290
import {use} from'react';
291
291
292
292
functionComments({commentsPromise}) {
293
-
//NOTE: this will resume the promise from the server.
294
-
//It will suspend until the data is available.
293
+
//注意: 这样做会复用服务器上的 promise
294
+
//它会一直等到数据可用之后才继续
295
295
constcomments=use(commentsPromise);
296
296
returncomments.map(commment=><p>{comment}</p>);
297
297
}
298
298
```
299
299
300
-
The`note`content is important data for the page to render, so we `await` it on the server. The comments are below the fold and lower-priority, so we start the promise on the server, and wait for it on the client with the `use` API. This will Suspend on the client, without blocking the `note`content from rendering.
300
+
因为`note`内容是页面渲染所需的重要数据,所以我们在服务器上进行 `await`。comments 数据在折叠部分中,优先级较低,所以我们在服务器上开始 promise,然后在客户端使用 `use` API 进行等待。这会在客户端上暂停,但不会阻塞 `note`内容的渲染。
301
301
302
-
Since async components are [not supported on the client](#why-cant-i-use-async-components-on-the-client), we await the promise with `use`.
0 commit comments