Unverified Commit 24434f18 authored by Delba de Oliveira's avatar Delba de Oliveira Committed by GitHub
parent a075f9ce
...@@ -13,15 +13,21 @@ related: ...@@ -13,15 +13,21 @@ related:
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. 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.
## Server Functions ## What are Server Functions?
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**. A **Server Function** is an asynchronous function that runs on the server. They can be called from client through a network request, which is why they must be asynchronous.
By convention, an `action` is an asynchronous function passed to `startTransition`. Server Functions are automatically wrapped with `startTransition` when: In an `action` or mutation context, they are also called **Server Actions**.
- Passed to a `<form>` using the `action` prop, By convention, a Server Action is an async function used with [`startTransition`](https://react.dev/reference/react/startTransition). This happens automatically when the function is:
- Passed to a `<button>` using the `formAction` prop
- Passed to `useActionState` - Passed to a `<form>` using the `action` prop.
- Passed to a `<button>` using the `formAction` prop.
- Passed to `useActionState`.
In Next.js, Server Actions integrate with the framework's [caching](/docs/app/deep-dive/caching) architecture. When an action is invoked, Next.js can return both the updated UI and new data in a single server roundtrip.
Behind the scenes, actions use the `POST` method, and only this HTTP method can invoke them.
## Creating Server Functions ## Creating Server Functions
...@@ -93,6 +99,8 @@ export default function Page() { ...@@ -93,6 +99,8 @@ export default function Page() {
} }
``` ```
> **Good to know:** Server Components support progressive enhancement by default, meaning forms that call Server Actions will be submitted even if JavaScript hasn't loaded yet or is disabled.
### Client Components ### Client Components
It's not possible to define Server Functions in Client Components. However, you can invoke them in Client Components by importing them from a file that has the `"use server"` directive at the top of it: It's not possible to define Server Functions in Client Components. However, you can invoke them in Client Components by importing them from a file that has the `"use server"` directive at the top of it:
...@@ -129,6 +137,36 @@ export function Button() { ...@@ -129,6 +137,36 @@ export function Button() {
} }
``` ```
> **Good to know:** In Client Components, forms invoking Server Actions will queue submissions if JavaScript isn't loaded yet, and will be prioritized for hydration. After hydration, the browser does not refresh on form submission.
### Passing actions as props
You can also pass an action to a Client Component as a prop:
```jsx
<ClientComponent updateItemAction={updateItem} />
```
```tsx filename="app/client-component.tsx" switcher
'use client'
export default function ClientComponent({
updateItemAction,
}: {
updateItemAction: (formData: FormData) => void
}) {
return <form action={updateItemAction}>{/* ... */}</form>
}
```
```jsx filename="app/client-component.js" switcher
'use client'
export default function ClientComponent({ updateItemAction }) {
return <form action={updateItemAction}>{/* ... */}</form>
}
```
## Invoking Server Functions ## Invoking Server Functions
There are two main ways you can invoke a Server Function: There are two main ways you can invoke a Server Function:
...@@ -194,8 +232,6 @@ export async function createPost(formData) { ...@@ -194,8 +232,6 @@ export async function createPost(formData) {
} }
``` ```
> **Good to know:** When passed to the `action` prop, Server Functions are also known as _Server Actions_.
### Event Handlers ### Event Handlers
You can invoke a Server Function in a Client Component by using event handlers such as `onClick`. You can invoke a Server Function in a Client Component by using event handlers such as `onClick`.
...@@ -292,7 +328,7 @@ export function Button() { ...@@ -292,7 +328,7 @@ export function Button() {
} }
``` ```
### Revalidating the cache ### Revalidating
After performing an update, you can revalidate the Next.js cache and show the updated data by calling [`revalidatePath`](/docs/app/api-reference/functions/revalidatePath) or [`revalidateTag`](/docs/app/api-reference/functions/revalidateTag) within the Server Function: After performing an update, you can revalidate the Next.js cache and show the updated data by calling [`revalidatePath`](/docs/app/api-reference/functions/revalidatePath) or [`revalidateTag`](/docs/app/api-reference/functions/revalidateTag) within the Server Function:
...@@ -348,3 +384,94 @@ export async function createPost(formData) { ...@@ -348,3 +384,94 @@ export async function createPost(formData) {
redirect('/posts') redirect('/posts')
} }
``` ```
### Cookies
You can `get`, `set`, and `delete` cookies inside a Server Action using the [`cookies`](/docs/app/api-reference/functions/cookies) API:
```ts filename="app/actions.ts" switcher
'use server'
import { cookies } from 'next/headers'
export async function exampleAction() {
const cookieStore = await cookies()
// Get cookie
cookieStore.get('name')?.value
// Set cookie
cookieStore.set('name', 'Delba')
// Delete cookie
cookieStore.delete('name')
}
```
```js filename="app/actions.js" switcher
'use server'
import { cookies } from 'next/headers'
export async function exampleAction() {
// Get cookie
const cookieStore = await cookies()
// Get cookie
cookieStore.get('name')?.value
// Set cookie
cookieStore.set('name', 'Delba')
// Delete cookie
cookieStore.delete('name')
}
```
### `useEffect`
You can use the React [`useEffect`](https://react.dev/reference/react/useEffect) hook to invoke a Server Action when the component mounts or a dependency changes. This is useful for mutations that depend on global events or need to be triggered automatically. For example, `onKeyDown` for app shortcuts, an intersection observer hook for infinite scrolling, or when the component mounts to update a view count:
```tsx filename="app/view-count.tsx" switcher
'use client'
import { incrementViews } from './actions'
import { useState, useEffect, useTransition } from 'react'
export default function ViewCount({ initialViews }: { initialViews: number }) {
const [views, setViews] = useState(initialViews)
const [isPending, startTransition] = useTransition()
useEffect(() => {
startTransition(async () => {
const updatedViews = await incrementViews()
setViews(updatedViews)
})
}, [])
// You can use `isPending` to give users feedback
return <p>Total Views: {views}</p>
}
```
```jsx filename="app/view-count.js" switcher
'use client'
import { incrementViews } from './actions'
import { useState, useEffect, useTransition } from 'react'
export default function ViewCount({ initialViews }) {
const [views, setViews] = useState(initialViews)
const [isPending, startTransition] = useTransition()
useEffect(() => {
startTransition(async () => {
const updatedViews = await incrementViews()
setViews(updatedViews)
})
}, [])
// You can use `isPending` to give users feedback
return <p>Total Views: {views}</p>
}
```
...@@ -30,7 +30,7 @@ The examples on this page walk through basic username and password auth for educ ...@@ -30,7 +30,7 @@ The examples on this page walk through basic username and password auth for educ
### Sign-up and login functionality ### Sign-up and login functionality
You can use the [`<form>`](https://react.dev/reference/react-dom/components/form) element with React's [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations) and `useActionState` to capture user credentials, validate form fields, and call your Authentication Provider's API or database. You can use the [`<form>`](https://react.dev/reference/react-dom/components/form) element with React's [Server Actions](/docs/app/getting-started/updating-data) and `useActionState` to capture user credentials, validate form fields, and call your Authentication Provider's API or database.
Since Server Actions always execute on the server, they provide a secure environment for handling authentication logic. Since Server Actions always execute on the server, they provide a secure environment for handling authentication logic.
...@@ -1403,7 +1403,7 @@ export const getUser = cache(async () => { ...@@ -1403,7 +1403,7 @@ export const getUser = cache(async () => {
### Server Actions ### Server Actions
Treat [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations) with the same security considerations as public-facing API endpoints, and verify if the user is allowed to perform a mutation. Treat [Server Actions](/docs/app/getting-started/updating-data) with the same security considerations as public-facing API endpoints, and verify if the user is allowed to perform a mutation.
In the example below, we check the user's role before allowing the action to proceed: In the example below, we check the user's role before allowing the action to proceed:
......
...@@ -377,7 +377,7 @@ export default async function Page() { ...@@ -377,7 +377,7 @@ export default async function Page() {
} }
``` ```
You can then use `revalidateTag` in a [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations) or [Route Handler](/docs/app/building-your-application/routing/route-handlers): You can then use `revalidateTag` in a [Server Actions](/docs/app/getting-started/updating-data) or [Route Handler](/docs/app/building-your-application/routing/route-handlers):
```ts filename="app/actions.ts" switcher ```ts filename="app/actions.ts" switcher
'use server' 'use server'
......
...@@ -27,8 +27,8 @@ description: Learn how to implement common UI patterns and use cases using Next. ...@@ -27,8 +27,8 @@ description: Learn how to implement common UI patterns and use cases using Next.
### Server Actions ### Server Actions
- [Passing additional values](/docs/app/guides/forms) - [Passing additional values](/docs/app/guides/forms)
- [Revalidating data](/docs/app/building-your-application/data-fetching/server-actions-and-mutations#revalidating-data) - [Revalidating data](/docs/app/getting-started/updating-data#revalidating)
- [Redirecting](/docs/app/building-your-application/data-fetching/server-actions-and-mutations#redirecting) - [Redirecting](/docs/app/guides/redirecting)
- [Setting cookies](/docs/app/api-reference/functions/cookies#setting-a-cookie) - [Setting cookies](/docs/app/api-reference/functions/cookies#setting-a-cookie)
- [Deleting cookies](/docs/app/api-reference/functions/cookies#deleting-cookies) - [Deleting cookies](/docs/app/api-reference/functions/cookies#deleting-cookies)
......
...@@ -111,7 +111,7 @@ We recommend reducing the combined complexity of these updates by breaking down ...@@ -111,7 +111,7 @@ We recommend reducing the combined complexity of these updates by breaking down
- Use `layout.js` to define UI that is shared across multiple routes. - Use `layout.js` to define UI that is shared across multiple routes.
- `.js`, `.jsx`, or `.tsx` file extensions can be used for special files. - `.js`, `.jsx`, or `.tsx` file extensions can be used for special files.
- You can colocate other files inside the `app` directory such as components, styles, tests, and more. [Learn more](/docs/app/building-your-application/routing). - You can colocate other files inside the `app` directory such as components, styles, tests, and more. [Learn more](/docs/app/building-your-application/routing).
- Data fetching functions like `getServerSideProps` and `getStaticProps` have been replaced with [a new API](/docs/app/building-your-application/data-fetching) inside `app`. `getStaticPaths` has been replaced with [`generateStaticParams`](/docs/app/api-reference/functions/generate-static-params). - Data fetching functions like `getServerSideProps` and `getStaticProps` have been replaced with [a new API](/docs/app/getting-started/fetching-data) inside `app`. `getStaticPaths` has been replaced with [`generateStaticParams`](/docs/app/api-reference/functions/generate-static-params).
- `pages/_app.js` and `pages/_document.js` have been replaced with a single `app/layout.js` root layout. [Learn more](/docs/app/api-reference/file-conventions/layout#root-layout). - `pages/_app.js` and `pages/_document.js` have been replaced with a single `app/layout.js` root layout. [Learn more](/docs/app/api-reference/file-conventions/layout#root-layout).
- `pages/_error.js` has been replaced with more granular `error.js` special files. [Learn more](/docs/app/getting-started/error-handling). - `pages/_error.js` has been replaced with more granular `error.js` special files. [Learn more](/docs/app/getting-started/error-handling).
- `pages/404.js` has been replaced with the [`not-found.js`](/docs/app/api-reference/file-conventions/not-found) file. - `pages/404.js` has been replaced with the [`not-found.js`](/docs/app/api-reference/file-conventions/not-found) file.
...@@ -326,7 +326,7 @@ export default function Page() { ...@@ -326,7 +326,7 @@ export default function Page() {
### Step 4: Migrating Pages ### Step 4: Migrating Pages
- Pages in the [`app` directory](/docs/app/building-your-application/routing) are [Server Components](/docs/app/getting-started/server-and-client-components) by default. This is different from the `pages` directory where pages are [Client Components](/docs/app/getting-started/server-and-client-components). - Pages in the [`app` directory](/docs/app/building-your-application/routing) are [Server Components](/docs/app/getting-started/server-and-client-components) by default. This is different from the `pages` directory where pages are [Client Components](/docs/app/getting-started/server-and-client-components).
- [Data fetching](/docs/app/building-your-application/data-fetching) has changed in `app`. `getServerSideProps`, `getStaticProps` and `getInitialProps` have been replaced with a simpler API. - [Data fetching](/docs/app/getting-started/fetching-data) has changed in `app`. `getServerSideProps`, `getStaticProps` and `getInitialProps` have been replaced with a simpler API.
- The `app` directory uses nested folders to define routes and a special `page.js` file to make a route segment publicly accessible. - The `app` directory uses nested folders to define routes and a special `page.js` file to make a route segment publicly accessible.
- | `pages` Directory | `app` Directory | Route | - | `pages` Directory | `app` Directory | Route |
| ----------------- | --------------------- | -------------- | | ----------------- | --------------------- | -------------- |
...@@ -485,7 +485,7 @@ This is the `useRouter` hook from the `pages` directory, but intended to be used ...@@ -485,7 +485,7 @@ This is the `useRouter` hook from the `pages` directory, but intended to be used
### Step 6: Migrating Data Fetching Methods ### Step 6: Migrating Data Fetching Methods
The `pages` directory uses `getServerSideProps` and `getStaticProps` to fetch data for pages. Inside the `app` directory, these previous data fetching functions are replaced with a [simpler API](/docs/app/building-your-application/data-fetching) built on top of `fetch()` and `async` React Server Components. The `pages` directory uses `getServerSideProps` and `getStaticProps` to fetch data for pages. Inside the `app` directory, these previous data fetching functions are replaced with a [simpler API](/docs/app/getting-started/fetching-data) built on top of `fetch()` and `async` React Server Components.
```tsx filename="app/page.tsx" switcher ```tsx filename="app/page.tsx" switcher
export default async function Page() { export default async function Page() {
......
...@@ -125,7 +125,7 @@ Since the pages in different zones may be released at different times, feature f ...@@ -125,7 +125,7 @@ Since the pages in different zones may be released at different times, feature f
## Server Actions ## Server Actions
When using [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations) with Multi-Zones, you must explicitly allow the user-facing origin since your user facing domain may serve multiple applications. In your `next.config.js` file, add the following lines: When using [Server Actions](/docs/app/getting-started/updating-data) with Multi-Zones, you must explicitly allow the user-facing origin since your user facing domain may serve multiple applications. In your `next.config.js` file, add the following lines:
```js filename="next.config.js" ```js filename="next.config.js"
const nextConfig = { const nextConfig = {
......
...@@ -97,7 +97,7 @@ While building your application, we recommend using the following features to en ...@@ -97,7 +97,7 @@ While building your application, we recommend using the following features to en
<AppOnly> <AppOnly>
- **[Tainting](/docs/app/api-reference/config/next-config-js/taint):** Prevent sensitive data from being exposed to the client by tainting data objects and/or specific values. - **[Tainting](/docs/app/api-reference/config/next-config-js/taint):** Prevent sensitive data from being exposed to the client by tainting data objects and/or specific values.
- **[Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations#authentication-and-authorization):** Ensure users are authorized to call Server Actions. Review the recommended [security practices](/blog/security-nextjs-server-components-actions). - **[Server Actions](/docs/app/getting-started/updating-data):** Ensure users are authorized to call Server Actions. Review the recommended [security practices](/blog/security-nextjs-server-components-actions).
</AppOnly> </AppOnly>
......
...@@ -38,7 +38,7 @@ There are a few ways you can handle redirects in Next.js. This page will go thro ...@@ -38,7 +38,7 @@ There are a few ways you can handle redirects in Next.js. This page will go thro
## `redirect` function ## `redirect` function
The `redirect` function allows you to redirect the user to another URL. You can call `redirect` in [Server Components](/docs/app/getting-started/server-and-client-components), [Route Handlers](/docs/app/building-your-application/routing/route-handlers), and [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations). The `redirect` function allows you to redirect the user to another URL. You can call `redirect` in [Server Components](/docs/app/getting-started/server-and-client-components), [Route Handlers](/docs/app/building-your-application/routing/route-handlers), and [Server Actions](/docs/app/getting-started/updating-data).
`redirect` is often used after a mutation or event. For example, creating a post: `redirect` is often used after a mutation or event. For example, creating a post:
...@@ -90,7 +90,7 @@ See the [`redirect` API reference](/docs/app/api-reference/functions/redirect) f ...@@ -90,7 +90,7 @@ See the [`redirect` API reference](/docs/app/api-reference/functions/redirect) f
## `permanentRedirect` function ## `permanentRedirect` function
The `permanentRedirect` function allows you to **permanently** redirect the user to another URL. You can call `permanentRedirect` in [Server Components](/docs/app/getting-started/server-and-client-components), [Route Handlers](/docs/app/building-your-application/routing/route-handlers), and [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations). The `permanentRedirect` function allows you to **permanently** redirect the user to another URL. You can call `permanentRedirect` in [Server Components](/docs/app/getting-started/server-and-client-components), [Route Handlers](/docs/app/building-your-application/routing/route-handlers), and [Server Actions](/docs/app/getting-started/updating-data).
`permanentRedirect` is often used after a mutation or event that changes an entity's canonical URL, such as updating a user's profile URL after they change their username: `permanentRedirect` is often used after a mutation or event that changes an entity's canonical URL, such as updating a user's profile URL after they change their username:
......
...@@ -25,7 +25,7 @@ Next.js can automatically code split your JavaScript bundles, and generate multi ...@@ -25,7 +25,7 @@ Next.js can automatically code split your JavaScript bundles, and generate multi
The [`next/link`](/docs/app/api-reference/components/link) component automatically [prefetches](/docs/app/api-reference/components/link#prefetch) routes, giving you the fast page transitions of a strict SPA, but with the advantage of persisting application routing state to the URL for linking and sharing. The [`next/link`](/docs/app/api-reference/components/link) component automatically [prefetches](/docs/app/api-reference/components/link#prefetch) routes, giving you the fast page transitions of a strict SPA, but with the advantage of persisting application routing state to the URL for linking and sharing.
Next.js can start as a static site or even a strict SPA where everything is rendered client-side. If your project grows, Next.js allows you to progressively add more server features (e.g. [React Server Components](/docs/app/getting-started/server-and-client-components), [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations), and more) as needed. Next.js can start as a static site or even a strict SPA where everything is rendered client-side. If your project grows, Next.js allows you to progressively add more server features (e.g. [React Server Components](/docs/app/getting-started/server-and-client-components), [Server Actions](/docs/app/getting-started/updating-data), and more) as needed.
## Examples ## Examples
...@@ -389,7 +389,7 @@ export function Button() { ...@@ -389,7 +389,7 @@ export function Button() {
} }
``` ```
Learn more about [mutating data with Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations). Learn more about [mutating data with Server Actions](/docs/app/getting-started/updating-data).
## Static export (optional) ## Static export (optional)
......
...@@ -288,7 +288,7 @@ Features that require a Node.js server, or dynamic logic that cannot be computed ...@@ -288,7 +288,7 @@ Features that require a Node.js server, or dynamic logic that cannot be computed
- [Incremental Static Regeneration](/docs/app/guides/incremental-static-regeneration) - [Incremental Static Regeneration](/docs/app/guides/incremental-static-regeneration)
- [Image Optimization](/docs/app/api-reference/components/image) with the default `loader` - [Image Optimization](/docs/app/api-reference/components/image) with the default `loader`
- [Draft Mode](/docs/app/guides/draft-mode) - [Draft Mode](/docs/app/guides/draft-mode)
- [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations) - [Server Actions](/docs/app/getting-started/updating-data)
- [Intercepting Routes](/docs/app/api-reference/file-conventions/intercepting-routes) - [Intercepting Routes](/docs/app/api-reference/file-conventions/intercepting-routes)
Attempting to use any of these features with `next dev` will result in an error, similar to setting the [`dynamic`](/docs/app/api-reference/file-conventions/route-segment-config#dynamic) option to `error` in the root layout. Attempting to use any of these features with `next dev` will result in an error, similar to setting the [`dynamic`](/docs/app/api-reference/file-conventions/route-segment-config#dynamic) option to `error` in the root layout.
......
---
title: Data Fetching
description: Learn how to fetch, cache, revalidate, and mutate data with Next.js.
---
...@@ -367,7 +367,7 @@ When configuring the different caching mechanisms, it's important to understand ...@@ -367,7 +367,7 @@ When configuring the different caching mechanisms, it's important to understand
### Data Cache and Client-side Router cache ### Data Cache and Client-side Router cache
- To immediately invalidate the Data Cache and Router cache, you can use [`revalidatePath`](#revalidatepath) or [`revalidateTag`](#fetch-optionsnexttags-and-revalidatetag) in a [Server Action](/docs/app/building-your-application/data-fetching/server-actions-and-mutations). - To immediately invalidate the Data Cache and Router cache, you can use [`revalidatePath`](#revalidatepath) or [`revalidateTag`](#fetch-optionsnexttags-and-revalidatetag) in a [Server Action](/docs/app/getting-started/updating-data).
- Revalidating the Data Cache in a [Route Handler](/docs/app/building-your-application/routing/route-handlers) **will not** immediately invalidate the Router Cache as the Route Handler isn't tied to a specific route. This means Router Cache will continue to serve the previous payload until a hard refresh, or the automatic invalidation period has elapsed. - Revalidating the Data Cache in a [Route Handler](/docs/app/building-your-application/routing/route-handlers) **will not** immediately invalidate the Router Cache as the Route Handler isn't tied to a specific route. This means Router Cache will continue to serve the previous payload until a hard refresh, or the automatic invalidation period has elapsed.
## APIs ## APIs
...@@ -473,7 +473,7 @@ revalidateTag('a') ...@@ -473,7 +473,7 @@ revalidateTag('a')
There are two places you can use `revalidateTag`, depending on what you're trying to achieve: There are two places you can use `revalidateTag`, depending on what you're trying to achieve:
1. [Route Handlers](/docs/app/building-your-application/routing/route-handlers) - to revalidate data in response of a third party event (e.g. webhook). This will not invalidate the Router Cache immediately as the Router Handler isn't tied to a specific route. 1. [Route Handlers](/docs/app/building-your-application/routing/route-handlers) - to revalidate data in response of a third party event (e.g. webhook). This will not invalidate the Router Cache immediately as the Router Handler isn't tied to a specific route.
2. [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations) - to revalidate data after a user action (e.g. form submission). This will invalidate the Router Cache for the associated route. 2. [Server Actions](/docs/app/getting-started/updating-data) - to revalidate data after a user action (e.g. form submission). This will invalidate the Router Cache for the associated route.
### `revalidatePath` ### `revalidatePath`
...@@ -486,7 +486,7 @@ revalidatePath('/') ...@@ -486,7 +486,7 @@ revalidatePath('/')
There are two places you can use `revalidatePath`, depending on what you're trying to achieve: There are two places you can use `revalidatePath`, depending on what you're trying to achieve:
1. [Route Handlers](/docs/app/building-your-application/routing/route-handlers) - to revalidate data in response to a third party event (e.g. webhook). 1. [Route Handlers](/docs/app/building-your-application/routing/route-handlers) - to revalidate data in response to a third party event (e.g. webhook).
2. [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations) - to revalidate data after a user interaction (e.g. form submission, clicking a button). 2. [Server Actions](/docs/app/getting-started/updating-data) - to revalidate data after a user interaction (e.g. form submission, clicking a button).
See the [`revalidatePath` API reference](/docs/app/api-reference/functions/revalidatePath) for more information. See the [`revalidatePath` API reference](/docs/app/api-reference/functions/revalidatePath) for more information.
......
...@@ -401,6 +401,6 @@ export default async function PostPage({ params }) { ...@@ -401,6 +401,6 @@ export default async function PostPage({ params }) {
} }
``` ```
See the [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations) docs for more examples. See the [Server Actions](/docs/app/getting-started/updating-data) docs for more examples.
</AppOnly> </AppOnly>
...@@ -211,7 +211,7 @@ export const maxDuration = 5 ...@@ -211,7 +211,7 @@ export const maxDuration = 5
> **Good to know**: > **Good to know**:
> >
> - If using [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations), set the `maxDuration` at the page level to change the default timeout of all Server Actions used on the page. > - If using [Server Actions](/docs/app/getting-started/updating-data), set the `maxDuration` at the page level to change the default timeout of all Server Actions used on the page.
### `generateStaticParams` ### `generateStaticParams`
......
...@@ -5,7 +5,7 @@ description: API Reference for the after function. ...@@ -5,7 +5,7 @@ description: API Reference for the after function.
`after` allows you to schedule work to be executed after a response (or prerender) is finished. This is useful for tasks and other side effects that should not block the response, such as logging and analytics. `after` allows you to schedule work to be executed after a response (or prerender) is finished. This is useful for tasks and other side effects that should not block the response, such as logging and analytics.
It can be used in [Server Components](/docs/app/getting-started/server-and-client-components) (including [`generateMetadata`](https://nextjs.org/docs/app/api-reference/functions/generate-metadata)), [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations), [Route Handlers](/docs/app/building-your-application/routing/route-handlers), and [Middleware](/docs/app/building-your-application/routing/middleware). It can be used in [Server Components](/docs/app/getting-started/server-and-client-components) (including [`generateMetadata`](https://nextjs.org/docs/app/api-reference/functions/generate-metadata)), [Server Actions](/docs/app/getting-started/updating-data), [Route Handlers](/docs/app/building-your-application/routing/route-handlers), and [Middleware](/docs/app/building-your-application/routing/middleware).
The function accepts a callback that will be executed after the response (or prerender) is finished: The function accepts a callback that will be executed after the response (or prerender) is finished:
...@@ -59,7 +59,7 @@ export default function Layout({ children }) { ...@@ -59,7 +59,7 @@ export default function Layout({ children }) {
### With request APIs ### With request APIs
You can use request APIs such as [`cookies`](/docs/app/api-reference/functions/cookies) and [`headers`](/docs/app/api-reference/functions/headers) inside `after` in [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations) and [Route Handlers](/docs/app/api-reference/file-conventions/route). This is useful for logging activity after a mutation. For example: You can use request APIs such as [`cookies`](/docs/app/api-reference/functions/cookies) and [`headers`](/docs/app/api-reference/functions/headers) inside `after` in [Server Actions](/docs/app/getting-started/updating-data) and [Route Handlers](/docs/app/api-reference/file-conventions/route). This is useful for logging activity after a mutation. For example:
```ts filename="app/api/route.ts" highlight={2,7-9} switcher ```ts filename="app/api/route.ts" highlight={2,7-9} switcher
import { after } from 'next/server' import { after } from 'next/server'
......
...@@ -64,7 +64,7 @@ export async function getData() { ...@@ -64,7 +64,7 @@ export async function getData() {
} }
``` ```
You can then purge the cache on-demand using [`revalidateTag`](/docs/app/api-reference/functions/revalidateTag) API in another function, for example, a [route handler](/docs/app/building-your-application/routing/route-handlers) or [Server Action](/docs/app/building-your-application/data-fetching/server-actions-and-mutations): You can then purge the cache on-demand using [`revalidateTag`](/docs/app/api-reference/functions/revalidateTag) API in another function, for example, a [route handler](/docs/app/building-your-application/routing/route-handlers) or [Server Action](/docs/app/getting-started/updating-data):
```tsx filename="app/action.ts" switcher ```tsx filename="app/action.ts" switcher
'use server' 'use server'
......
...@@ -3,7 +3,7 @@ title: cookies ...@@ -3,7 +3,7 @@ title: cookies
description: API Reference for the cookies function. description: API Reference for the cookies function.
--- ---
`cookies` is an **async** function that allows you to read the HTTP incoming request cookies in [Server Components](/docs/app/getting-started/server-and-client-components), and read/write outgoing request cookies in [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations) or [Route Handlers](/docs/app/building-your-application/routing/route-handlers). `cookies` is an **async** function that allows you to read the HTTP incoming request cookies in [Server Components](/docs/app/getting-started/server-and-client-components), and read/write outgoing request cookies in [Server Actions](/docs/app/getting-started/updating-data) or [Route Handlers](/docs/app/building-your-application/routing/route-handlers).
```tsx filename="app/page.tsx" switcher ```tsx filename="app/page.tsx" switcher
import { cookies } from 'next/headers' import { cookies } from 'next/headers'
...@@ -70,9 +70,9 @@ To learn more about these options, see the [MDN docs](https://developer.mozilla. ...@@ -70,9 +70,9 @@ To learn more about these options, see the [MDN docs](https://developer.mozilla.
- In version 14 and earlier, `cookies` was a synchronous function. To help with backwards compatibility, you can still access it synchronously in Next.js 15, but this behavior will be deprecated in the future. - In version 14 and earlier, `cookies` was a synchronous function. To help with backwards compatibility, you can still access it synchronously in Next.js 15, but this behavior will be deprecated in the future.
- `cookies` is a [Dynamic API](/docs/app/getting-started/partial-prerendering#dynamic-rendering) whose returned values cannot be known ahead of time. Using it in a layout or page will opt a route into [dynamic rendering](/docs/app/getting-started/partial-prerendering#dynamic-rendering). - `cookies` is a [Dynamic API](/docs/app/getting-started/partial-prerendering#dynamic-rendering) whose returned values cannot be known ahead of time. Using it in a layout or page will opt a route into [dynamic rendering](/docs/app/getting-started/partial-prerendering#dynamic-rendering).
- The `.delete` method can only be called: - The `.delete` method can only be called:
- In a [Server Action](/docs/app/building-your-application/data-fetching/server-actions-and-mutations) or [Route Handler](/docs/app/building-your-application/routing/route-handlers). - In a [Server Action](/docs/app/getting-started/updating-data) or [Route Handler](/docs/app/building-your-application/routing/route-handlers).
- If it belongs to the same domain from which `.set` is called. For wildcard domains, the specific subdomain must be an exact match. Additionally, the code must be executed on the same protocol (HTTP or HTTPS) as the cookie you want to delete. - If it belongs to the same domain from which `.set` is called. For wildcard domains, the specific subdomain must be an exact match. Additionally, the code must be executed on the same protocol (HTTP or HTTPS) as the cookie you want to delete.
- HTTP does not allow setting cookies after streaming starts, so you must use `.set` in a [Server Action](/docs/app/building-your-application/data-fetching/server-actions-and-mutations) or [Route Handler](/docs/app/building-your-application/routing/route-handlers). - HTTP does not allow setting cookies after streaming starts, so you must use `.set` in a [Server Action](/docs/app/getting-started/updating-data) or [Route Handler](/docs/app/building-your-application/routing/route-handlers).
## Understanding Cookie Behavior in Server Components ## Understanding Cookie Behavior in Server Components
...@@ -143,7 +143,7 @@ export default async function Page() { ...@@ -143,7 +143,7 @@ export default async function Page() {
### Setting a cookie ### Setting a cookie
You can use the `(await cookies()).set(name, value, options)` method in a [Server Action](/docs/app/building-your-application/data-fetching/server-actions-and-mutations) or [Route Handler](/docs/app/building-your-application/routing/route-handlers) to set a cookie. The [`options` object](#options) is optional. You can use the `(await cookies()).set(name, value, options)` method in a [Server Action](/docs/app/getting-started/updating-data) or [Route Handler](/docs/app/building-your-application/routing/route-handlers) to set a cookie. The [`options` object](#options) is optional.
```tsx filename="app/actions.ts" switcher ```tsx filename="app/actions.ts" switcher
'use server' 'use server'
......
...@@ -31,7 +31,7 @@ module.exports = { ...@@ -31,7 +31,7 @@ module.exports = {
} }
``` ```
`forbidden` can be invoked in [Server Components](/docs/app/getting-started/server-and-client-components), [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations), and [Route Handlers](/docs/app/building-your-application/routing/route-handlers). `forbidden` can be invoked in [Server Components](/docs/app/getting-started/server-and-client-components), [Server Actions](/docs/app/getting-started/updating-data), and [Route Handlers](/docs/app/building-your-application/routing/route-handlers).
```tsx filename="app/auth/page.tsx" switcher ```tsx filename="app/auth/page.tsx" switcher
import { verifySession } from '@/app/lib/dal' import { verifySession } from '@/app/lib/dal'
......
...@@ -6,7 +6,7 @@ related: ...@@ -6,7 +6,7 @@ related:
- app/api-reference/functions/redirect - app/api-reference/functions/redirect
--- ---
The `permanentRedirect` function allows you to redirect the user to another URL. `permanentRedirect` can be used in Server Components, Client Components, [Route Handlers](/docs/app/building-your-application/routing/route-handlers), and [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations). The `permanentRedirect` function allows you to redirect the user to another URL. `permanentRedirect` can be used in Server Components, Client Components, [Route Handlers](/docs/app/building-your-application/routing/route-handlers), and [Server Actions](/docs/app/getting-started/updating-data).
When used in a streaming context, this will insert a meta tag to emit the redirect on the client side. When used in a server action, it will serve a 303 HTTP redirect response to the caller. Otherwise, it will serve a 308 (Permanent) HTTP redirect response to the caller. When used in a streaming context, this will insert a meta tag to emit the redirect on the client side. When used in a server action, it will serve a 303 HTTP redirect response to the caller. Otherwise, it will serve a 308 (Permanent) HTTP redirect response to the caller.
...@@ -27,7 +27,7 @@ permanentRedirect(path, type) ...@@ -27,7 +27,7 @@ permanentRedirect(path, type)
| `path` | `string` | The URL to redirect to. Can be a relative or absolute path. | | `path` | `string` | The URL to redirect to. Can be a relative or absolute path. |
| `type` | `'replace'` (default) or `'push'` (default in Server Actions) | The type of redirect to perform. | | `type` | `'replace'` (default) or `'push'` (default in Server Actions) | The type of redirect to perform. |
By default, `permanentRedirect` will use `push` (adding a new entry to the browser history stack) in [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations) and `replace` (replacing the current URL in the browser history stack) everywhere else. You can override this behavior by specifying the `type` parameter. By default, `permanentRedirect` will use `push` (adding a new entry to the browser history stack) in [Server Actions](/docs/app/getting-started/updating-data) and `replace` (replacing the current URL in the browser history stack) everywhere else. You can override this behavior by specifying the `type` parameter.
The `type` parameter has no effect when used in Server Components. The `type` parameter has no effect when used in Server Components.
......
...@@ -6,7 +6,7 @@ related: ...@@ -6,7 +6,7 @@ related:
- app/api-reference/functions/permanentRedirect - app/api-reference/functions/permanentRedirect
--- ---
The `redirect` function allows you to redirect the user to another URL. `redirect` can be used in [Server Components](/docs/app/getting-started/server-and-client-components), [Route Handlers](/docs/app/building-your-application/routing/route-handlers), and [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations). The `redirect` function allows you to redirect the user to another URL. `redirect` can be used in [Server Components](/docs/app/getting-started/server-and-client-components), [Route Handlers](/docs/app/building-your-application/routing/route-handlers), and [Server Actions](/docs/app/getting-started/updating-data).
When used in a [streaming context](/docs/app/getting-started/linking-and-navigating#streaming), this will insert a meta tag to emit the redirect on the client side. When used in a server action, it will serve a 303 HTTP redirect response to the caller. Otherwise, it will serve a 307 HTTP redirect response to the caller. When used in a [streaming context](/docs/app/getting-started/linking-and-navigating#streaming), this will insert a meta tag to emit the redirect on the client side. When used in a server action, it will serve a 303 HTTP redirect response to the caller. Otherwise, it will serve a 307 HTTP redirect response to the caller.
...@@ -27,7 +27,7 @@ redirect(path, type) ...@@ -27,7 +27,7 @@ redirect(path, type)
| `path` | `string` | The URL to redirect to. Can be a relative or absolute path. | | `path` | `string` | The URL to redirect to. Can be a relative or absolute path. |
| `type` | `'replace'` (default) or `'push'` (default in Server Actions) | The type of redirect to perform. | | `type` | `'replace'` (default) or `'push'` (default in Server Actions) | The type of redirect to perform. |
By default, `redirect` will use `push` (adding a new entry to the browser history stack) in [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations) and `replace` (replacing the current URL in the browser history stack) everywhere else. You can override this behavior by specifying the `type` parameter. By default, `redirect` will use `push` (adding a new entry to the browser history stack) in [Server Actions](/docs/app/getting-started/updating-data) and `replace` (replacing the current URL in the browser history stack) everywhere else. You can override this behavior by specifying the `type` parameter.
The `type` parameter has no effect when used in Server Components. The `type` parameter has no effect when used in Server Components.
......
...@@ -31,7 +31,7 @@ module.exports = { ...@@ -31,7 +31,7 @@ module.exports = {
} }
``` ```
`unauthorized` can be invoked in [Server Components](/docs/app/getting-started/server-and-client-components), [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations), and [Route Handlers](/docs/app/building-your-application/routing/route-handlers). `unauthorized` can be invoked in [Server Components](/docs/app/getting-started/server-and-client-components), [Server Actions](/docs/app/getting-started/updating-data), and [Route Handlers](/docs/app/building-your-application/routing/route-handlers).
```tsx filename="app/dashboard/page.tsx" switcher ```tsx filename="app/dashboard/page.tsx" switcher
import { verifySession } from '@/app/lib/dal' import { verifySession } from '@/app/lib/dal'
......
...@@ -6,6 +6,6 @@ version: legacy ...@@ -6,6 +6,6 @@ version: legacy
> **Good to know**: This option is **no longer** needed as of Next.js 13.4. The App Router is now stable. > **Good to know**: This option is **no longer** needed as of Next.js 13.4. The App Router is now stable.
The App Router ([`app` directory](/docs/app/building-your-application/routing)) enables support for [layouts](/docs/app/api-reference/file-conventions/layout), [Server Components](/docs/app/getting-started/server-and-client-components), [streaming](/docs/app/api-reference/file-conventions/loading), and [colocated data fetching](/docs/app/building-your-application/data-fetching). The App Router ([`app` directory](/docs/app/building-your-application/routing)) enables support for [layouts](/docs/app/api-reference/file-conventions/layout), [Server Components](/docs/app/getting-started/server-and-client-components), [streaming](/docs/app/api-reference/file-conventions/loading), and [colocated data fetching](/docs/app/getting-started/fetching-data).
Using the `app` directory will automatically enable [React Strict Mode](https://react.dev/reference/react/StrictMode). Learn how to [incrementally adopt `app`](/docs/app/guides/migrating/app-router-migration#migrating-from-pages-to-app). Using the `app` directory will automatically enable [React Strict Mode](https://react.dev/reference/react/StrictMode). Learn how to [incrementally adopt `app`](/docs/app/guides/migrating/app-router-migration#migrating-from-pages-to-app).
...@@ -8,7 +8,7 @@ Forms enable you to create and update data in web applications. Next.js provides ...@@ -8,7 +8,7 @@ Forms enable you to create and update data in web applications. Next.js provides
> **Good to know:** > **Good to know:**
> >
> - We will soon recommend [incrementally adopting](/docs/app/guides/migrating/app-router-migration) the App Router and using [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations) for handling form submissions and data mutations. Server Actions allow you to define asynchronous server functions that can be called directly from your components, without needing to manually create an API Route. > - We will soon recommend [incrementally adopting](/docs/app/guides/migrating/app-router-migration) the App Router and using [Server Actions](/docs/app/getting-started/updating-data) for handling form submissions and data mutations. Server Actions allow you to define asynchronous server functions that can be called directly from your components, without needing to manually create an API Route.
> - API Routes [do not specify CORS headers](https://developer.mozilla.org/docs/Web/HTTP/CORS), meaning they are same-origin only by default. > - API Routes [do not specify CORS headers](https://developer.mozilla.org/docs/Web/HTTP/CORS), meaning they are same-origin only by default.
> - Since API Routes run on the server, we're able to use sensitive values (like API keys) through [Environment Variables](/docs/pages/guides/environment-variables) without exposing them to the client. This is critical for the security of your application. > - Since API Routes run on the server, we're able to use sensitive values (like API keys) through [Environment Variables](/docs/pages/guides/environment-variables) without exposing them to the client. This is critical for the security of your application.
......
...@@ -54,5 +54,5 @@ export default function Page({ stars }) { ...@@ -54,5 +54,5 @@ export default function Page({ stars }) {
## Caveats ## Caveats
- `getInitialProps` can only be used in `pages/` top level files, and not in nested components. To have nested data fetching at the component level, consider exploring the [App Router](/docs/app/building-your-application/data-fetching). - `getInitialProps` can only be used in `pages/` top level files, and not in nested components. To have nested data fetching at the component level, consider exploring the [App Router](/docs/app/getting-started/fetching-data).
- Regardless of whether your route is static or dynamic, any data returned from `getInitialProps` as `props` will be able to be examined on the client-side in the initial HTML. This is to allow the page to be [hydrated](https://react.dev/reference/react-dom/hydrate) correctly. Make sure that you don't pass any sensitive information that shouldn't be available on the client in `props`. - Regardless of whether your route is static or dynamic, any data returned from `getInitialProps` as `props` will be able to be examined on the client-side in the initial HTML. This is to allow the page to be [hydrated](https://react.dev/reference/react-dom/hydrate) correctly. Make sure that you don't pass any sensitive information that shouldn't be available on the client in `props`.
...@@ -133,8 +133,8 @@ export async function getServerSideProps(context) { ...@@ -133,8 +133,8 @@ export async function getServerSideProps(context) {
## Version History ## Version History
| Version | Changes | | Version | Changes |
| --------- | ----------------------------------------------------------------------------------------------------------- | | --------- | ------------------------------------------------------------------------------------------------- |
| `v13.4.0` | [App Router](/docs/app/building-your-application/data-fetching) is now stable with simplified data fetching | | `v13.4.0` | [App Router](/docs/app/getting-started/fetching-data) is now stable with simplified data fetching |
| `v10.0.0` | `locale`, `locales`, `defaultLocale`, and `notFound` options added. | | `v10.0.0` | `locale`, `locales`, `defaultLocale`, and `notFound` options added. |
| `v9.3.0` | `getServerSideProps` introduced. | | `v9.3.0` | `getServerSideProps` introduced. |
...@@ -250,10 +250,10 @@ export default Post ...@@ -250,10 +250,10 @@ export default Post
## Version History ## Version History
| Version | Changes | | Version | Changes |
| --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `v13.4.0` | [App Router](/docs/app/building-your-application/data-fetching) is now stable with simplified data fetching, including [`generateStaticParams()`](/docs/app/api-reference/functions/generate-static-params) | | `v13.4.0` | [App Router](/docs/app/getting-started/fetching-data) is now stable with simplified data fetching, including [`generateStaticParams()`](/docs/app/api-reference/functions/generate-static-params) |
| `v12.2.0` | [On-Demand Incremental Static Regeneration](/docs/pages/guides/incremental-static-regeneration#on-demand-revalidation-with-revalidatepath) is stable. | | `v12.2.0` | [On-Demand Incremental Static Regeneration](/docs/pages/guides/incremental-static-regeneration#on-demand-revalidation-with-revalidatepath) is stable. |
| `v12.1.0` | [On-Demand Incremental Static Regeneration](/docs/pages/guides/incremental-static-regeneration#on-demand-revalidation-with-revalidatepath) added (beta). | | `v12.1.0` | [On-Demand Incremental Static Regeneration](/docs/pages/guides/incremental-static-regeneration#on-demand-revalidation-with-revalidatepath) added (beta). |
| `v9.5.0` | Stable [Incremental Static Regeneration](/docs/pages/guides/incremental-static-regeneration) | | `v9.5.0` | Stable [Incremental Static Regeneration](/docs/pages/guides/incremental-static-regeneration) |
| `v9.3.0` | `getStaticPaths` introduced. | | `v9.3.0` | `getStaticPaths` introduced. |
...@@ -220,7 +220,7 @@ export default Blog ...@@ -220,7 +220,7 @@ export default Blog
| Version | Changes | | Version | Changes |
| --------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- | | --------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `v13.4.0` | [App Router](/docs/app/building-your-application/data-fetching) is now stable with simplified data fetching | | `v13.4.0` | [App Router](/docs/app/getting-started/fetching-data) is now stable with simplified data fetching |
| `v12.2.0` | [On-Demand Incremental Static Regeneration](/docs/pages/guides/incremental-static-regeneration#on-demand-revalidation-with-revalidatepath) is stable. | | `v12.2.0` | [On-Demand Incremental Static Regeneration](/docs/pages/guides/incremental-static-regeneration#on-demand-revalidation-with-revalidatepath) is stable. |
| `v12.1.0` | [On-Demand Incremental Static Regeneration](/docs/pages/guides/incremental-static-regeneration#on-demand-revalidation-with-revalidatepath) added (beta). | | `v12.1.0` | [On-Demand Incremental Static Regeneration](/docs/pages/guides/incremental-static-regeneration#on-demand-revalidation-with-revalidatepath) added (beta). |
| `v10.0.0` | `locale`, `locales`, `defaultLocale`, and `notFound` options added. | | `v10.0.0` | `locale`, `locales`, `defaultLocale`, and `notFound` options added. |
......
...@@ -4,7 +4,7 @@ title: Failed to find Server Action ...@@ -4,7 +4,7 @@ title: Failed to find Server Action
## Why This Message Occurred ## Why This Message Occurred
For security purposes, Next.js creates encrypted, non-deterministic keys (IDs) to allow for the client to reference and call the [Server Action](/docs/app/building-your-application/data-fetching/server-actions-and-mutations). These keys are periodically recalculated between builds for enhanced security. For security purposes, Next.js creates encrypted, non-deterministic keys (IDs) to allow for the client to reference and call the [Server Action](/docs/app/getting-started/updating-data). These keys are periodically recalculated between builds for enhanced security.
When self-hosting your Next.js application across multiple servers, each server instance may end up with a different encryption key, leading to potential inconsistencies. When self-hosting your Next.js application across multiple servers, each server instance may end up with a different encryption key, leading to potential inconsistencies.
...@@ -16,5 +16,5 @@ If you are deploying your Next.js application to Vercel, you can use the feature ...@@ -16,5 +16,5 @@ If you are deploying your Next.js application to Vercel, you can use the feature
## Useful Links ## Useful Links
- [Server Actions - Security](/docs/app/building-your-application/data-fetching/server-actions-and-mutations#security) - [Server Actions - Security](/docs/app/guides/data-security)
- [Overwriting encryption keys (advanced)](/docs/app/building-your-application/data-fetching/server-actions-and-mutations#overwriting-encryption-keys-advanced) - [Overwriting encryption keys (advanced)](/docs/app/guides/data-security#overwriting-encryption-keys-advanced)
...@@ -6,7 +6,7 @@ title: 'Invalid "use server" Value' ...@@ -6,7 +6,7 @@ title: 'Invalid "use server" Value'
This error occurs when a `"use server"` file exports a value that is not an async function. It might happen when you unintentionally export something like a configuration object, an arbitrary value, or missed the `async` keyword in the exported function declaration. This error occurs when a `"use server"` file exports a value that is not an async function. It might happen when you unintentionally export something like a configuration object, an arbitrary value, or missed the `async` keyword in the exported function declaration.
These functions are required to be defined as async, because `"use server"` marks them as [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations) and they can be invoked directly from the client through a network request. These functions are required to be defined as async, because `"use server"` marks them as [Server Actions](/docs/app/getting-started/updating-data) and they can be invoked directly from the client through a network request.
Examples of incorrect code: Examples of incorrect code:
...@@ -39,5 +39,5 @@ Check all exported values in the `"use server"` file (including `export *`) and ...@@ -39,5 +39,5 @@ Check all exported values in the `"use server"` file (including `export *`) and
## Useful Links ## Useful Links
- [Server Actions and Mutations - Next.js](/docs/app/building-your-application/data-fetching/server-actions-and-mutations) - [Server Actions and Mutations - Next.js](/docs/app/getting-started/updating-data)
- ['use server' directive - React](https://react.dev/reference/react/use-server) - ['use server' directive - React](https://react.dev/reference/react/use-server)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment