diff --git a/docs/02-app/01-building-your-application/02-data-fetching/03-forms-and-mutations.mdx b/docs/02-app/01-building-your-application/02-data-fetching/03-forms-and-mutations.mdx index 6e642cc6836ff..7122a6637b22e 100644 --- a/docs/02-app/01-building-your-application/02-data-fetching/03-forms-and-mutations.mdx +++ b/docs/02-app/01-building-your-application/02-data-fetching/03-forms-and-mutations.mdx @@ -185,6 +185,17 @@ export default async function submit() { } ``` +```js filename="app/actions.js" switcher +'use server' + +import { revalidatePath } from 'next/cache' + +export default async function submit() { + await submitForm() + revalidatePath('/') +} +``` + Or invalidate a specific data fetch with a cache tag using [`revalidateTag`](/docs/app/api-reference/functions/revalidateTag): ```ts filename="app/actions.ts" switcher @@ -198,6 +209,17 @@ export default async function submit() { } ``` +```js filename="app/actions.js" switcher +'use server' + +import { revalidateTag } from 'next/cache' + +export default async function submit() { + await addPost() + revalidateTag('posts') +} +``` + ### Redirecting @@ -244,6 +266,19 @@ export default async function submit() { } ``` +```js filename="app/actions.js" switcher +'use server' + +import { redirect } from 'next/navigation' +import { revalidateTag } from 'next/cache' + +export default async function submit() { + const id = await addPost() + revalidateTag('posts') // Update cached posts + redirect(`/post/${id}`) // Navigate to new route +} +``` + ### Form Validation @@ -740,7 +775,18 @@ You can set cookies inside a Server Action using the [`cookies`](/docs/app/api-r import { cookies } from 'next/headers' export async function create() { - const cart = await createCart(): + const cart = await createCart() + cookies().set('cartId', cart.id) +} +``` + +```js filename="app/actions.js" switcher +'use server' + +import { cookies } from 'next/headers' + +export async function create() { + const cart = await createCart() cookies().set('cartId', cart.id) } ``` @@ -789,6 +835,17 @@ export async function create() { } ``` +```js filename="app/actions.js" switcher +'use server' + +import { cookies } from 'next/headers' + +export async function create() { + const auth = cookies().get('authorization')?.value + // ... +} +``` + ### Deleting Cookies @@ -833,6 +890,17 @@ export async function create() { } ``` +```js filename="app/actions.js" switcher +'use server' + +import { cookies } from 'next/headers' + +export async function create() { + cookies().delete('name') + // ... +} +``` + See [additional examples](/docs/app/api-reference/functions/cookies#deleting-cookies) for deleting cookies from Server Actions.