Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update docs 1 #71812

Merged
merged 13 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/02-app/02-api-reference/01-directives/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: Directives
description: Directives are used to modify the behavior of your Next.js application.
---

The following directives are available:
381 changes: 381 additions & 0 deletions docs/02-app/02-api-reference/01-directives/use-cache.mdx

Large diffs are not rendered by default.

95 changes: 95 additions & 0 deletions docs/02-app/02-api-reference/01-directives/use-client.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
title: use client
description: Learn how to use the use client directive to render a component on the client.
related:
description: React documentation for use client.
links:
- https://react.dev/reference/rsc/use-client
---

The `use client` directive designates a component to be rendered on the **client side** and should be used when creating interactive user interfaces (UI) that require client-side JavaScript capabilities, such as state management, event handling, and access to browser APIs. This is a React feature.

## Usage

To designate a component as a Client Component, add the `use client` directive **at the top of the file**, before any imports:

````tsx filename="app/components/counter.tsx" highlight={1} switcher
'use client'

import { useState } from 'react'

export default function Counter() {
const [count, setCount] = useState(0)

return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
)
}
````

````jsx filename="app/components/counter.js" highlight={1} switcher
'use client'

import { useState } from 'react'

export default function Counter() {
const [count, setCount] = useState(0)

return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
)
}
````

## Nesting Client Components within Server Components

Combining Server and Client Components allows you to build applications that are both performant and interactive:

1. **Server Components**: Use for static content, data fetching, and SEO-friendly elements.
2. **Client Components**: Use for interactive elements that require state, effects, or browser APIs.
3. **Component composition**: Nest Client Components within Server Components as needed for a clear separation of server and client logic.

In the following example:

- `Header` is a Server Component handling static content.
- `Counter` is a Client Component enabling interactivity within the page.

```tsx filename="app/page.tsx" highlight={2,8} switcher
import Header from './header'
import Counter from './counter' // This is a Client Component

export default function Page() {
return (
<div>
<Header />
<Counter />
</div>
)
}
```

```jsx filename="app/page.js" highlight={2,8} switcher
import Header from './header'
import Counter from './counter' // This is a Client Component

export default function Page() {
return (
<div>
<Header />
<Counter />
</div>
)
}
```


## Reference

See the [React documentation](https://react.dev/reference/rsc/use-client) for more information on `use client`.

158 changes: 158 additions & 0 deletions docs/02-app/02-api-reference/01-directives/use-server.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
---
title: use server
description: Learn how to use the use server directive to execute code on the server.
related:
description: React documentation for use server.
links:
- https://react.dev/reference/rsc/use-server
---

The `use server` directive designates a function or file to be executed on the **server side**. It can be used at the top of a file to indicate that all functions in the file are server-side, or inline at the top of a function to mark the function as a [Server Function](https://19.react.dev/reference/rsc/server-functions). This is a React feature.

## Using `use server` at the top of a file

The following example shows a file with a `use server` directive at the top. All functions in the file are executed on the server.

```tsx filename="app/actions.ts" highlight={1} switcher
'use server'
import { db } from '@/lib/db' // Your database client

export async function createUser(data: { name: string; email: string }) {
const user = await db.user.create({ data })
return user
}
```

```jsx filename="app/actions.js" highlight={1} switcher
'use server'
import { db } from '@/lib/db' // Your database client

export async function createUser(data) {
const user = await db.user.create({ data })
return user
}
```

### Using Server Functions in a Client Component

To use Server Functions in Client Components you need to create your Server Functions in a dedicated file using the `use server` directive at the top of the file. These Server Functions can then be imported into Client and Server Components and executed.

Assuming you have a `fetchUsers` Server Function in `actions.ts`:

```tsx filename="app/actions.ts" highlight={1} switcher
'use server'
import { db } from '@/lib/db' // Your database client

export async function fetchUsers() {
const users = await db.user.findMany()
return users
}
```

```jsx filename="app/actions.js" highlight={1} switcher
'use server'
import { db } from '@/lib/db' // Your database client

export async function fetchUsers() {
const users = await db.user.findMany()
return users
}
```

Then you can import the `fetchUsers` Server Function into a Client Component and execute it on the client-side.

```tsx filename="app/components/my-button.tsx" highlight={1,2,8} switcher
'use client'
import { fetchUsers } from '../actions'

export default function MyButton() {
return <button onClick={() => fetchUsers()}>Fetch Users</button>
}
```

```jsx filename="app/components/my-button.js" highlight={1,2,8} switcher
'use client'
import { fetchUsers } from '../actions'

export default function MyButton() {
return <button onClick={() => fetchUsers()}>Fetch Users</button>
}
```


## Using `use server` inline

In the following example, `use server` is used inline at the top of a function to mark it as a [Server Function](https://19.react.dev/reference/rsc/server-functions):

```tsx filename="app/page.tsx" highlight={5} switcher
import { db } from '@/lib/db' // Your database client

export default function UserList() {
async function fetchUsers() {
'use server'
const users = await db.user.findMany()
return users
}

return <button onClick={() => fetchUsers()}>Fetch Users</button>
}
```

```jsx filename="app/page.js" highlight={5} switcher
import { db } from '@/lib/db' // Your database client

export default function UserList() {
async function fetchUsers() {
'use server'
const users = await db.user.findMany()
return users
}

return <button onClick={() => fetchUsers()}>Fetch Users</button>
}
```

## Security considerations

When using the `use server` directive, it's important to ensure that all server-side logic is secure and that sensitive data remains protected.

### Authentication and authorization

Always authenticate and authorize users before performing sensitive server-side operations.

```tsx filename="app/actions.ts" highlight={1,7,8,9,10} switcher
'use server'

import { db } from '@/lib/db' // Your database client
import { authenticate } from '@/lib/auth' // Your authentication library

export async function createUser(data: { name: string; email: string }, token: string) {
const user = authenticate(token)
if (!user) {
throw new Error('Unauthorized')
}
const newUser = await db.user.create({ data })
return newUser
}
```

```jsx filename="app/actions.js" highlight={1,7,8,9,10} switcher
'use server'

import { db } from '@/lib/db' // Your database client
import { authenticate } from '@/lib/auth' // Your authentication library

export async function createUser(data, token) {
const user = authenticate(token)
if (!user) {
throw new Error('Unauthorized')
}
const newUser = await db.user.create({ data })
return newUser
}
```


## Reference

See the [React documentation](https://react.dev/reference/rsc/use-server) for more information on `use server`.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ title: Route Segment Config
description: Learn about how to configure options for Next.js route segments.
---

> The options outlined on this page are disabled if the [`dynamicIO`](/docs/app/api-reference/next-config-js/dynamicIO) flag is on, and will eventually be deprecated in the future.

The Route Segment options allows you to configure the behavior of a [Page](/docs/app/building-your-application/routing/layouts-and-templates), [Layout](/docs/app/building-your-application/routing/layouts-and-templates), or [Route Handler](/docs/app/building-your-application/routing/route-handlers) by directly exporting the following variables:

| Option | Type | Default |
Expand Down
Loading
Loading