Skip to content

Commit 234ab18

Browse files
committed
docs: update nextjs documentation
1 parent 91f180d commit 234ab18

File tree

12 files changed

+84
-65
lines changed

12 files changed

+84
-65
lines changed

apps/docs/content/en/docs/01-app/01-getting-started/04-images.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ Since Next.js does not have access to remote files during the build process, you
125125
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:
126126

127127
```ts filename="next.config.ts" switcher
128-
import { NextConfig } from 'next'
128+
import type { NextConfig } from 'next'
129129

130130
const config: NextConfig = {
131131
images: {

apps/docs/content/en/docs/01-app/01-getting-started/07-server-and-client-components.mdx

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,15 @@ related:
77
description: Learn more about the APIs mentioned in this page.
88
links:
99
- app/api-reference/directives/use-client
10-
- app/api-reference/file-conventions/route
1110
---
1211

1312
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.
1413

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.
5215

5316
## When to use Server and Client Components?
5417

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.
5619

5720
Use **Client Components** when you need:
5821

@@ -128,6 +91,42 @@ export default function LikeButton({ likes }) {
12891
}
12992
```
13093

94+
## 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+
131130
## Examples
132131

133132
### Using Client Components
@@ -186,7 +185,7 @@ export default function Search() {
186185
}
187186
```
188187

189-
```tsx filename="app/ui/search.tsx" highlight={1} switcher
188+
```jsx filename="app/ui/search.js" highlight={1} switcher
190189
'use client'
191190

192191
export default function Search() {
@@ -508,9 +507,19 @@ export default function Page() {
508507
509508
### Preventing environment poisoning
510509

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:
512511

513-
For example, take the following function:
512+
```ts filename="lib/data.ts" switcher
513+
export async function getData() {
514+
const res = await fetch('https://external-service.com/data', {
515+
headers: {
516+
authorization: process.env.API_KEY,
517+
},
518+
})
519+
520+
return res.json()
521+
}
522+
```
514523

515524
```js filename="lib/data.js" switcher
516525
export async function getData() {
@@ -554,4 +563,4 @@ export async function getData() {
554563

555564
Now, if you try to import the module into a Client Component, there will be a build-time error.
556565

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.

apps/docs/content/en/docs/01-app/01-getting-started/09-fetching-data.mdx renamed to apps/docs/content/en/docs/01-app/01-getting-started/08-fetching-data.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ export default function Posts({ posts }) {
177177
}
178178
```
179179

180-
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).
181181

182182
#### Community libraries
183183

@@ -238,7 +238,7 @@ export default function BlogPage() {
238238

239239
> **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.
240240
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.
242242

243243
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.
244244

@@ -252,8 +252,8 @@ To improve the initial load time and user experience, you can use streaming to b
252252

253253
There are two ways you can implement streaming in your application:
254254

255-
1. With the [`loading.js` file](#with-loadingjs)
256-
2. With React's [`<Suspense>` component](#with-suspense)
255+
1. Wrapping a page with a [`loading.js` file](#with-loadingjs)
256+
2. Wrapping a component with [`<Suspense>`](#with-suspense)
257257

258258
### With `loading.js`
259259

apps/docs/content/en/docs/01-app/01-getting-started/10-updating-data.mdx renamed to apps/docs/content/en/docs/01-app/01-getting-started/09-updating-data.mdx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ related:
1313

1414
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.
1515

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+
1626
## Creating Server Functions
1727

1828
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.
File renamed without changes.

apps/docs/content/en/docs/01-app/01-getting-started/08-partial-prerendering.mdx renamed to apps/docs/content/en/docs/01-app/01-getting-started/11-partial-prerendering.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ A component becomes dynamic if it uses the following APIs:
5252
- [`unstable_noStore`](/docs/app/api-reference/functions/unstable_noStore)
5353
- [`fetch`](/docs/app/api-reference/functions/fetch) with `{ cache: 'no-store' }`
5454

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.
5656

5757
### Suspense
5858

apps/docs/content/en/docs/01-app/03-building-your-application/02-data-fetching/03-server-actions-and-mutations.mdx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export default function Page() {
4646

4747
### Client Components
4848

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:
5050

5151
```tsx filename="app/actions.ts" switcher
5252
'use server'
@@ -671,20 +671,20 @@ You can use the React [`useEffect`](https://react.dev/reference/react/useEffect)
671671
'use client'
672672

673673
import { incrementViews } from './actions'
674-
import { useState, useEffect } from 'react'
674+
import { useState, useEffect, useTransition } from 'react'
675675

676676
export default function ViewCount({ initialViews }: { initialViews: number }) {
677677
const [views, setViews] = useState(initialViews)
678+
const [isPending, startTransition] = useTransition()
678679

679680
useEffect(() => {
680-
const updateViews = async () => {
681+
starTransition(async () => {
681682
const updatedViews = await incrementViews()
682683
setViews(updatedViews)
683-
}
684-
685-
updateViews()
684+
})
686685
}, [])
687686

687+
// You can use `isPending` to give users feedback
688688
return <p>Total Views: {views}</p>
689689
}
690690
```
@@ -693,20 +693,20 @@ export default function ViewCount({ initialViews }: { initialViews: number }) {
693693
'use client'
694694

695695
import { incrementViews } from './actions'
696-
import { useState, useEffect } from 'react'
696+
import { useState, useEffect, useTransition } from 'react'
697697

698-
export default function ViewCount({ initialViews }: { initialViews: number }) {
698+
export default function ViewCount({ initialViews }) {
699699
const [views, setViews] = useState(initialViews)
700+
const [isPending, startTransition] = useTransition()
700701

701702
useEffect(() => {
702-
const updateViews = async () => {
703+
starTransition(async () => {
703704
const updatedViews = await incrementViews()
704705
setViews(updatedViews)
705-
}
706-
707-
updateViews()
706+
})
708707
}, [])
709708

709+
// You can use `isPending` to give users feedback
710710
return <p>Total Views: {views}</p>
711711
}
712712
```

apps/docs/content/en/docs/01-app/05-api-reference/04-functions/connection.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,5 @@ function connection(): Promise<void>
5454

5555
| Version | Changes |
5656
| ------------ | ------------------------ |
57-
| `v15.0.0` | `connection` stabilized. |
58-
| `v15.0.0-RC` | `connection` introduced. |
57+
| `v15.0.0` | `connection` stabilized. |
58+
| `v15.0.0-RC` | `connection` introduced. |
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)