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
Copy file name to clipboardExpand all lines: apps/docs/content/en/docs/01-app/01-getting-started/04-images.mdx
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -125,7 +125,7 @@ Since Next.js does not have access to remote files during the build process, you
125
125
To safely allow images from remote servers, you need to define a list of supported URL patterns in [`next.config.js`](/docs/app/api-reference/config/next-config-js). Be as specific as possible to prevent malicious usage. For example, the following configuration will only allow images from a specific AWS S3 bucket:
Copy file name to clipboardExpand all lines: apps/docs/content/en/docs/01-app/01-getting-started/07-server-and-client-components.mdx
+52-43Lines changed: 52 additions & 43 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,52 +7,15 @@ related:
7
7
description: Learn more about the APIs mentioned in this page.
8
8
links:
9
9
- app/api-reference/directives/use-client
10
-
- app/api-reference/file-conventions/route
11
10
---
12
11
13
12
By default, layouts and pages are [Server Components](https://react.dev/reference/rsc/server-components), which lets you fetch data and render parts of your UI on the server, optionally cache the result, and stream it to the client. When you need interactivity or browser APIs, you can use [Client Components](https://react.dev/reference/rsc/use-client) to layer in functionality.
14
13
15
-
This page explains how Server and Client Components work in Next.js, when to use them, and how to compose them together in your application.
16
-
17
-
## How do Server and Client Components work in Next.js?
18
-
19
-
### On the server
20
-
21
-
On the server, Next.js uses React's APIs to orchestrate rendering. The rendering work is split into chunks, by individual route segments ([layouts and pages](/docs/app/getting-started/layouts-and-pages)):
22
-
23
-
-**Server Components** are rendered into a special data format called the React Server Component Payload (RSC Payload).
24
-
-**Client Components** and the RSC Payload are used to [prerender](/docs/app/getting-started/partial-prerendering#how-does-partial-prerendering-work) HTML.
25
-
26
-
> **What is the React Server Component Payload (RSC)?**
27
-
>
28
-
> The RSC Payload is a compact binary representation of the rendered React Server Components tree. It's used by React on the client to update the browser's DOM. The RSC Payload contains:
29
-
>
30
-
> - The rendered result of Server Components
31
-
> - Placeholders for where Client Components should be rendered and references to their JavaScript files
32
-
> - Any props passed from a Server Component to a Client Component
33
-
34
-
### On the client (first load)
35
-
36
-
Then, on the client:
37
-
38
-
1.**HTML** is used to immediately show a fast non-interactive preview of the route to the user.
39
-
2.**RSC Payload** is used to reconcile the Client and Server Component trees.
40
-
3.**JavaScript** is used to hydrate Client Components and make the application interactive.
41
-
42
-
> **What is hydration?**
43
-
>
44
-
> Hydration is React's process for attaching [event handlers](https://react.dev/learn/responding-to-events) to the DOM, to make the static HTML interactive.
45
-
46
-
### Subsequent Navigations
47
-
48
-
On subsequent navigations:
49
-
50
-
- The **RSC Payload** is prefetched and cached for instant navigation.
51
-
-**Client Components** are rendered entirely on the client, without the server-rendered HTML.
14
+
This page explains how Server and Client Components work in Next.js and when to use them, with examples of how to compose them together in your application.
52
15
53
16
## When to use Server and Client Components?
54
17
55
-
The client and server environments have different capabilities. Server and Client components allow you to run logic in each environment, and compose them together in the same application.
18
+
The client and server environments have different capabilities. Server and Client components allow you to run logic in each environment depending on your use case.
## How do Server and Client Components work in Next.js?
95
+
96
+
### On the server
97
+
98
+
On the server, Next.js uses React's APIs to orchestrate rendering. The rendering work is split into chunks, by individual route segments ([layouts and pages](/docs/app/getting-started/layouts-and-pages)):
99
+
100
+
-**Server Components** are rendered into a special data format called the React Server Component Payload (RSC Payload).
101
+
-**Client Components** and the RSC Payload are used to [prerender](/docs/app/getting-started/partial-prerendering#how-does-partial-prerendering-work) HTML.
102
+
103
+
> **What is the React Server Component Payload (RSC)?**
104
+
>
105
+
> The RSC Payload is a compact binary representation of the rendered React Server Components tree. It's used by React on the client to update the browser's DOM. The RSC Payload contains:
106
+
>
107
+
> - The rendered result of Server Components
108
+
> - Placeholders for where Client Components should be rendered and references to their JavaScript files
109
+
> - Any props passed from a Server Component to a Client Component
110
+
111
+
### On the client (first load)
112
+
113
+
Then, on the client:
114
+
115
+
1.**HTML** is used to immediately show a fast non-interactive preview of the route to the user.
116
+
2.**RSC Payload** is used to reconcile the Client and Server Component trees.
117
+
3.**JavaScript** is used to hydrate Client Components and make the application interactive.
118
+
119
+
> **What is hydration?**
120
+
>
121
+
> Hydration is React's process for attaching [event handlers](https://react.dev/learn/responding-to-events) to the DOM, to make the static HTML interactive.
122
+
123
+
### Subsequent Navigations
124
+
125
+
On subsequent navigations:
126
+
127
+
- The **RSC Payload** is prefetched and cached for instant navigation.
128
+
-**Client Components** are rendered entirely on the client, without the server-rendered HTML.
129
+
131
130
## Examples
132
131
133
132
### Using Client Components
@@ -186,7 +185,7 @@ export default function Search() {
@@ -508,9 +507,19 @@ export default function Page() {
508
507
509
508
### Preventing environment poisoning
510
509
511
-
JavaScript modules can be shared between both Server and Client Components modules. This means it's possible to accidentanlly import server-only code into the client.
510
+
JavaScript modules can be shared between both Server and Client Components modules. This means it's possible to accidentanlly import server-only code into the client. For example, consider the following function:
512
511
513
-
For example, take the following function:
512
+
```ts filename="lib/data.ts" switcher
513
+
exportasyncfunction getData() {
514
+
const res =awaitfetch('https://external-service.com/data', {
515
+
headers: {
516
+
authorization: process.env.API_KEY,
517
+
},
518
+
})
519
+
520
+
returnres.json()
521
+
}
522
+
```
514
523
515
524
```js filename="lib/data.js" switcher
516
525
exportasyncfunctiongetData() {
@@ -554,4 +563,4 @@ export async function getData() {
554
563
555
564
Now, if you try to import the module into a Client Component, there will be a build-time error.
556
565
557
-
> **Good to know**: The corresponding [`client-only` package](https://www.npmjs.com/package/client-only) can be used to mark modules that contain client-only code – for example, code that accesses the `window` object.
566
+
> **Good to know**: The corresponding [`client-only` package](https://www.npmjs.com/package/client-only) can be used to mark modules that contain client-only logic like code that accesses the `window` object.
In the example above, you need to wrap the `<Posts />` component in a [`<Suspense>` boundary](https://react.dev/reference/react/Suspense). This means the fallback will be shown while the promise is being resolved. Learn more about [streaming](#streaming).
180
+
In the example above, the `<Posts>` component is wrapped in a [`<Suspense>` boundary](https://react.dev/reference/react/Suspense). This means the fallback will be shown while the promise is being resolved. Learn more about [streaming](#streaming).
181
181
182
182
#### Community libraries
183
183
@@ -238,7 +238,7 @@ export default function BlogPage() {
238
238
239
239
> **Warning:** The content below assumes the [`dynamicIO` config option](/docs/app/api-reference/config/next-config-js/dynamicIO) is enabled in your application. The flag was introduced in Next.js 15 canary.
240
240
241
-
When using `async/await` in Server Components, Next.js will opt into **dynamic rendering**. This means the data will be fetched and rendered on the server for every user request. If there are any slow data requests, the whole route will be blocked from rendering.
241
+
When using `async/await` in Server Components, Next.js will opt into [dynamic rendering](/docs/app/getting-started/partial-prerendering#dynamic-rendering). This means the data will be fetched and rendered on the server for every user request. If there are any slow data requests, the whole route will be blocked from rendering.
242
242
243
243
To improve the initial load time and user experience, you can use streaming to break up the page's HTML into smaller chunks and progressively send those chunks from the server to the client.
244
244
@@ -252,8 +252,8 @@ To improve the initial load time and user experience, you can use streaming to b
252
252
253
253
There are two ways you can implement streaming in your application:
Copy file name to clipboardExpand all lines: apps/docs/content/en/docs/01-app/01-getting-started/09-updating-data.mdx
+10Lines changed: 10 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,6 +13,16 @@ related:
13
13
14
14
You can update data in Next.js using React's [Server Functions](https://react.dev/reference/rsc/server-functions). This page will go through how you can [create](#creating-server-functions) and [invoke](#invoking-server-functions) Server Functions.
15
15
16
+
## Server Functions
17
+
18
+
A Server Function is an asynchronous function that is executed on the server. Server Functions are inherently asynchronous because they are invoked by the client using a network request. When invoked as part of an `action`, they are also called **Server Actions**.
19
+
20
+
By convention, an `action` is an asynchronous function passed to `startTransition`. Server Functions are automatically wrapped with `startTransition` when:
21
+
22
+
- Passed to a `<form>` using the `action` prop,
23
+
- Passed to a `<button>` using the `formAction` prop
24
+
- Passed to `useActionState`
25
+
16
26
## Creating Server Functions
17
27
18
28
A Server Function can be defined by using the [`use server`](https://react.dev/reference/rsc/use-server) directive. You can place the directive at the top of an **asynchronous** function to mark the function as a Server Function, or at the top of a separate file to mark all exports of that file.
-[`fetch`](/docs/app/api-reference/functions/fetch) with `{ cache: 'no-store' }`
54
54
55
-
In Partial Prerendering, using the APIs throws a special React error that informs Next.js the component cannot be statically rendered, causing a build error. You can use [Suspense](#suspense) to defer rendering until runtime.
55
+
In Partial Prerendering, using these APIs throws a special React error that informs Next.js the component cannot be statically rendered, causing a build error. You can use a [Suspense](#suspense) boundary to wrap your component to defer rendering until runtime.
Copy file name to clipboardExpand all lines: apps/docs/content/en/docs/01-app/03-building-your-application/02-data-fetching/03-server-actions-and-mutations.mdx
+12-12Lines changed: 12 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,7 +46,7 @@ export default function Page() {
46
46
47
47
### Client Components
48
48
49
-
To call a Server Action in a Client Component, create a new file and add the `"use server"` directive at the top of it. All exported functions within the file will be marked as Server Actions that can be reused in both Client and Server Components:
49
+
To call a [Server Function](/docs/app/getting-started/updating-data#server-functions) in a Client Component, create a new file and add the `"use server"` directive at the top of it. All exported functions within the file will be marked as Server Functions that can be reused in both Client and Server Components:
50
50
51
51
```tsx filename="app/actions.ts" switcher
52
52
'use server'
@@ -671,20 +671,20 @@ You can use the React [`useEffect`](https://react.dev/reference/react/useEffect)
0 commit comments