|
| 1 | +--- |
| 2 | +title: use server |
| 3 | +description: Learn how to use the use server directive to execute code on the server. |
| 4 | +related: |
| 5 | + description: React documentation for use server. |
| 6 | + links: |
| 7 | + - https://react.dev/reference/rsc/use-server |
| 8 | +--- |
| 9 | + |
| 10 | +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. |
| 11 | + |
| 12 | +## Using `use server` at the top of a file |
| 13 | + |
| 14 | +The following example shows a file with a `use server` directive at the top. All functions in the file are executed on the server. |
| 15 | + |
| 16 | +```tsx filename="app/actions.ts" highlight={1} switcher |
| 17 | +'use server' |
| 18 | +import { db } from '@/lib/db' // Your database client |
| 19 | +
|
| 20 | +export async function createUser(data: { name: string; email: string }) { |
| 21 | + const user = await db.user.create({ data }) |
| 22 | + return user |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +```jsx filename="app/actions.js" highlight={1} switcher |
| 27 | +'use server' |
| 28 | +import { db } from '@/lib/db' // Your database client |
| 29 | + |
| 30 | +export async function createUser(data) { |
| 31 | + const user = await db.user.create({ data }) |
| 32 | + return user |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +### Using Server Functions in a Client Component |
| 37 | + |
| 38 | +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. |
| 39 | + |
| 40 | +Assuming you have a `fetchUsers` Server Function in `actions.ts`: |
| 41 | + |
| 42 | +```tsx filename="app/actions.ts" highlight={1} switcher |
| 43 | +'use server' |
| 44 | +import { db } from '@/lib/db' // Your database client |
| 45 | +
|
| 46 | +export async function fetchUsers() { |
| 47 | + const users = await db.user.findMany() |
| 48 | + return users |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +```jsx filename="app/actions.js" highlight={1} switcher |
| 53 | +'use server' |
| 54 | +import { db } from '@/lib/db' // Your database client |
| 55 | + |
| 56 | +export async function fetchUsers() { |
| 57 | + const users = await db.user.findMany() |
| 58 | + return users |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +Then you can import the `fetchUsers` Server Function into a Client Component and execute it on the client-side. |
| 63 | + |
| 64 | +```tsx filename="app/components/my-button.tsx" highlight={1,2,8} switcher |
| 65 | +'use client' |
| 66 | +import { fetchUsers } from '../actions' |
| 67 | + |
| 68 | +export default function MyButton() { |
| 69 | + return <button onClick={() => fetchUsers()}>Fetch Users</button> |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +```jsx filename="app/components/my-button.js" highlight={1,2,8} switcher |
| 74 | +'use client' |
| 75 | +import { fetchUsers } from '../actions' |
| 76 | + |
| 77 | +export default function MyButton() { |
| 78 | + return <button onClick={() => fetchUsers()}>Fetch Users</button> |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +## Using `use server` inline |
| 83 | + |
| 84 | +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): |
| 85 | + |
| 86 | +```tsx filename="app/page.tsx" highlight={5} switcher |
| 87 | +import { db } from '@/lib/db' // Your database client |
| 88 | +
|
| 89 | +export default function UserList() { |
| 90 | + async function fetchUsers() { |
| 91 | + 'use server' |
| 92 | + const users = await db.user.findMany() |
| 93 | + return users |
| 94 | + } |
| 95 | + |
| 96 | + return <button onClick={() => fetchUsers()}>Fetch Users</button> |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +```jsx filename="app/page.js" highlight={5} switcher |
| 101 | +import { db } from '@/lib/db' // Your database client |
| 102 | + |
| 103 | +export default function UserList() { |
| 104 | + async function fetchUsers() { |
| 105 | + 'use server' |
| 106 | + const users = await db.user.findMany() |
| 107 | + return users |
| 108 | + } |
| 109 | + |
| 110 | + return <button onClick={() => fetchUsers()}>Fetch Users</button> |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +## Security considerations |
| 115 | + |
| 116 | +When using the `use server` directive, it's important to ensure that all server-side logic is secure and that sensitive data remains protected. |
| 117 | + |
| 118 | +### Authentication and authorization |
| 119 | + |
| 120 | +Always authenticate and authorize users before performing sensitive server-side operations. |
| 121 | + |
| 122 | +```tsx filename="app/actions.ts" highlight={1,7,8,9,10} switcher |
| 123 | +'use server' |
| 124 | + |
| 125 | +import { db } from '@/lib/db' // Your database client |
| 126 | +import { authenticate } from '@/lib/auth' // Your authentication library |
| 127 | +
|
| 128 | +export async function createUser( |
| 129 | + data: { name: string; email: string }, |
| 130 | + token: string |
| 131 | +) { |
| 132 | + const user = authenticate(token) |
| 133 | + if (!user) { |
| 134 | + throw new Error('Unauthorized') |
| 135 | + } |
| 136 | + const newUser = await db.user.create({ data }) |
| 137 | + return newUser |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +```jsx filename="app/actions.js" highlight={1,7,8,9,10} switcher |
| 142 | +'use server' |
| 143 | + |
| 144 | +import { db } from '@/lib/db' // Your database client |
| 145 | +import { authenticate } from '@/lib/auth' // Your authentication library |
| 146 | + |
| 147 | +export async function createUser(data, token) { |
| 148 | + const user = authenticate(token) |
| 149 | + if (!user) { |
| 150 | + throw new Error('Unauthorized') |
| 151 | + } |
| 152 | + const newUser = await db.user.create({ data }) |
| 153 | + return newUser |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +## Reference |
| 158 | + |
| 159 | +See the [React documentation](https://react.dev/reference/rsc/use-server) for more information on `use server`. |
0 commit comments