Skip to content

Commit

Permalink
docs(use-cache): add missing switcher and types
Browse files Browse the repository at this point in the history
  • Loading branch information
devpla committed Dec 26, 2024
1 parent 34ed922 commit fba6f15
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions docs/01-app/04-api-reference/01-directives/use-cache.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ The `use cache` directive designates a component and/or a function to be cached.

Enable support for the `use cache` directive with the [`dynamicIO`](/docs/app/api-reference/config/next-config-js/dynamicIO) flag in your `next.config.ts` file:

```ts filename="next.config.ts"
```ts filename="next.config.ts" switcher
import type { NextConfig } from 'next'

const nextConfig: NextConfig = {
Expand All @@ -31,6 +31,17 @@ const nextConfig: NextConfig = {
export default nextConfig
```

```js filename="next.config.js" switcher
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
dynamicIO: true,
},
}

module.exports = nextConfig
```

Then, you can use the `use cache` directive at the file, component, or function level:

```tsx
Expand Down Expand Up @@ -137,7 +148,7 @@ You can use `use cache` at the component level to cache any fetches or computati

The props are serialized and form part of the cache key, and the cache entry will be reused as long as the serialized props produce the same value in each instance.

```tsx filename="app/components/bookings.tsx" highlight={2}
```tsx filename="app/components/bookings.tsx" highlight={2} switcher
export async function Bookings({ type = 'haircut' }: BookingsProps) {
'use cache'
async function getBookingsData() {
Expand All @@ -152,7 +163,7 @@ interface BookingsProps {
}
```

```jsx filename="app/components/bookings.js" highlight={2}
```jsx filename="app/components/bookings.js" highlight={2} switcher
export async function Bookings({ type = 'haircut' }) {
'use cache'
async function getBookingsData() {
Expand Down Expand Up @@ -295,7 +306,11 @@ async function CachedComponent({ performUpdate }) {
```tsx filename="app/ClientComponent.tsx" switcher
'use client'

export default function ClientComponent({ action }) {
export default function ClientComponent({
action,
}: {
action: () => Promise<void>
}) {
return <button onClick={action}>Update</button>
}
```
Expand Down

0 comments on commit fba6f15

Please sign in to comment.