Skip to content

Commit

Permalink
docs: fix code block for server action (#68268)
Browse files Browse the repository at this point in the history
Co-authored-by: Delba de Oliveira <[email protected]>
  • Loading branch information
leerob and delbaoliveira authored Jul 30, 2024
1 parent c28b2d2 commit eedbfa1
Showing 1 changed file with 17 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Caching is the process of storing data to reduce the number of requests made to

### `fetch` requests

By default, `fetch` requests fetch fresh data at runtime.
Starting with Next.js 15, `fetch` requests are not cached by default.

To cache an individual `fetch` request, you can use the `cache: 'force-cache'` option:

Expand All @@ -19,70 +19,19 @@ To cache an individual `fetch` request, you can use the `cache: 'force-cache'` o
fetch('https://...', { cache: 'force-cache' })
```

To opt out of caching for individual `fetch` requests, you can use the `cache: 'no-store'` option:

```js filename="layout.js | page.js"
fetch('https://...', { cache: 'no-store' })
```

> **Advanced**: If you have multiple `fetch` requests in a layout or page segment, you can configure the caching behavior of all data requests in the segment using the `const dynamic = 'force-dynamic'` or `const fetchCache = 'force-no-store'` [Segment Config Options](/docs/app/api-reference/file-conventions/route-segment-config).
### Data fetching libraries and ORMs

Whether a data request is cached will depend on the default semantics of your Data Fetching Library, Database Client, or ORM.

To cache specific requests, you can use the [`unstable_cache`](/docs/app/api-reference/functions/unstable_cache) API:

```tsx filename="@/app/lib/data.ts switcher highlight={6}
import { unstable_cache as cache } from 'next/cache'

export async function getPosts() {
cache()

try {
// Fetch Data
} catch (error) {}
}
```

```jsx filename="@/app/lib/data.js switcher highlight={6}
import { unstable_cache as cache } from 'next/cache'
To cache specific requests to your database or ORM, you can use the [`unstable_cache`](/docs/app/api-reference/functions/unstable_cache) API:

export async function getPosts() {
cache()
try {
// Fetch Data
} catch (error) {}
}
```

To opt specific requests out of caching, you can use the [`unstable_noStore`](/docs/app/api-reference/functions/unstable_noStore) API:
```jsx
import { getUser } from './data'
import { unstable_cache } from 'next/cache'

```tsx filename="@/app/lib/data.ts switcher highlight={6}
import { unstable_noStore as noStore } from 'next/cache'
const getCachedUser = unstable_cache(async (id) => getUser(id), ['my-app-user'])

export async function getTransactions() {
// Prevent the response from being cached.
// This is equivalent to in fetch(..., {cache: 'no-store'}).
noStore()

try {
// Fetch Data
} catch (error) {}
}
```

```jsx filename="@/app/lib/data.js switcher highlight={6}
import { unstable_noStore as noStore } from 'next/cache'

export async function getTransactions() {
// Prevent the response from being cached.
// This is equivalent to in fetch(..., {cache: 'no-store'}).
noStore()

try {
// Fetch Data
} catch (error) {}
export default async function Component({ userID }) {
const user = await getCachedUser(userID)
return user
}
```

Expand Down Expand Up @@ -126,24 +75,18 @@ Use `revalidatePath` in [Server Actions](/docs/app/building-your-application/dat
```tsx filename="@/app/actions.tsx switcher
import { revalidatePath } from 'next/cache'

export default async createPost() {
try {
// Mutate data
revalidatePath('/posts')
} catch(error) {}

export async function createPost() {
// Mutate data
revalidatePath('/posts')
}
```

```jsx filename="@/app/actions.js switcher
import { revalidatePath } from 'next/cache'

export default async createPost() {
try {
// Mutate data
revalidatePath('/posts')
} catch(error) {}

export async function createPost() {
// Mutate data
revalidatePath('/posts')
}
```

Expand Down Expand Up @@ -177,7 +120,7 @@ You can then revalidate this `fetch` call tagged with `collection` by calling `r

import { revalidateTag } from 'next/cache'

export default async function action() {
export async function action() {
revalidateTag('collection')
}
```
Expand All @@ -187,7 +130,7 @@ export default async function action() {

import { revalidateTag } from 'next/cache'

export default async function action() {
export async function action() {
revalidateTag('collection')
}
```
Expand Down

0 comments on commit eedbfa1

Please sign in to comment.