Some checks failed
Test examples / Test Examples (20) (push) Has been cancelled
Test examples / Test Examples (22) (push) Has been cancelled
Lock Threads / action (push) Has been cancelled
Trigger Release / start (push) Has been cancelled
Stale issue handler / stale (push) Has been cancelled
Update Font Data / create-pull-request (push) Has been cancelled
build-and-deploy / deploy-target (push) Has been cancelled
build-and-deploy / build (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / build-wasm (nodejs) (push) Has been cancelled
build-and-deploy / build-wasm (web) (push) Has been cancelled
build-and-deploy / Deploy preview tarball (push) Has been cancelled
build-and-deploy / Potentially publish release (push) Has been cancelled
build-and-deploy / publish-turbopack-npm-packages (push) Has been cancelled
build-and-deploy / Deploy examples (push) Has been cancelled
build-and-deploy / thank you, build (push) Has been cancelled
build-and-deploy / Upload Turbopack Bytesize metrics to Datadog (push) Has been cancelled
Rspack Next.js development integration tests / Rspack integration tests (push) Has been cancelled
Rspack Next.js production integration tests / Rspack integration tests (push) Has been cancelled
Turbopack Next.js development integration tests / Next.js integration tests (push) Has been cancelled
Turbopack Next.js production integration tests / Next.js integration tests (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack development test manifest (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack production test manifest (push) Has been cancelled
Upload bundler test manifests to areweturboyet.com / Upload test results (push) Has been cancelled
Update React / create-pull-request (push) Has been cancelled
test-e2e-project-reset-cron / reset-test-project (push) Has been cancelled
Notify about the top 15 issues/PRs/feature requests (most reacted) in the last 90 days / run (push) Has been cancelled
162 lines
4.9 KiB
Plaintext
162 lines
4.9 KiB
Plaintext
---
|
|
title: use server
|
|
description: Learn how to use the use server directive to execute code on the 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,5} 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,5} 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/posts/[id]/page.tsx" switcher highlight={8}
|
|
import { EditPost } from './edit-post'
|
|
import { revalidatePath } from 'next/cache'
|
|
|
|
export default async function PostPage({ params }: { params: { id: string } }) {
|
|
const post = await getPost(params.id)
|
|
|
|
async function updatePost(formData: FormData) {
|
|
'use server'
|
|
await savePost(params.id, formData)
|
|
revalidatePath(`/posts/${params.id}`)
|
|
}
|
|
|
|
return <EditPost updatePostAction={updatePost} post={post} />
|
|
}
|
|
```
|
|
|
|
```jsx filename="app/posts/[id]/page.js" switcher highlight={8}
|
|
import { EditPost } from './edit-post'
|
|
import { revalidatePath } from 'next/cache'
|
|
|
|
export default async function PostPage({ params }) {
|
|
const post = await getPost(params.id)
|
|
|
|
async function updatePost(formData) {
|
|
'use server'
|
|
await savePost(params.id, formData)
|
|
revalidatePath(`/posts/${params.id}`)
|
|
}
|
|
|
|
return <EditPost updatePostAction={updatePost} post={post} />
|
|
}
|
|
```
|
|
|
|
## 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`.
|