From c4aba81e38e24c6cbe0764f594abb36acfb26b43 Mon Sep 17 00:00:00 2001 From: Lee Robinson Date: Fri, 25 Aug 2023 14:46:41 -0500 Subject: [PATCH 1/2] docs: Add JS code snippets for forms --- .../03-forms-and-mutations.mdx | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) 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..304883c214fb2 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 @@ -745,6 +780,17 @@ export async function create() { } ``` +```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) +} +``` + ### Reading Cookies @@ -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. From fa42d47230982ec77fc328896ca67852dae32d5e Mon Sep 17 00:00:00 2001 From: Lee Robinson Date: Fri, 25 Aug 2023 18:07:46 -0500 Subject: [PATCH 2/2] Fix --- .../02-data-fetching/03-forms-and-mutations.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 304883c214fb2..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 @@ -775,7 +775,7 @@ 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) } ``` @@ -786,7 +786,7 @@ export async function create() { import { cookies } from 'next/headers' export async function create() { - const cart = await createCart(): + const cart = await createCart() cookies().set('cartId', cart.id) } ```