Skip to content

Commit d10ed21

Browse files
Reza Karim ZadehReza Karim Zadeh
authored andcommitted
docs: improve unstable_cache documentation with clearer DB example
1 parent f9f625b commit d10ed21

File tree

1 file changed

+36
-20
lines changed

1 file changed

+36
-20
lines changed

docs/01-app/03-api-reference/04-functions/unstable_cache.mdx

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,44 +48,60 @@ const data = unstable_cache(fetchData, keyParts, options)()
4848
```tsx filename="app/page.tsx" switcher
4949
import { unstable_cache } from 'next/cache'
5050

51-
export default async function Page({
52-
params,
53-
}: {
54-
params: Promise<{ userId: string }>
55-
}) {
56-
const { userId } = await params
57-
const getCachedUser = unstable_cache(
51+
export default async function Page() {
52+
const getCachedPosts = unstable_cache(
5853
async () => {
59-
return { id: userId }
54+
return [
55+
{ id: 1, title: 'Hello World' },
56+
{ id: 2, title: 'Next.js Caching' },
57+
]
6058
},
61-
[userId], // add the user ID to the cache key
59+
['posts'],
6260
{
63-
tags: ['users'],
61+
tags: ['posts'],
6462
revalidate: 60,
6563
}
6664
)
6765

68-
//...
66+
const posts = await getCachedPosts()
67+
68+
return (
69+
<ul>
70+
{posts.map((post) => (
71+
<li key={post.id}>{post.title}</li>
72+
))}
73+
</ul>
74+
)
6975
}
7076
```
7177

7278
```jsx filename="app/page.jsx" switcher
73-
import { unstable_cache } from 'next/cache';
79+
import { unstable_cache } from 'next/cache'
7480

75-
export default async function Page({ params } }) {
76-
const { userId } = await params
77-
const getCachedUser = unstable_cache(
81+
export default async function Page() {
82+
const getCachedPosts = unstable_cache(
7883
async () => {
79-
return { id: userId };
84+
return [
85+
{ id: 1, title: 'Hello World' },
86+
{ id: 2, title: 'Next.js Caching' },
87+
]
8088
},
81-
[userId], // add the user ID to the cache key
89+
['posts'],
8290
{
83-
tags: ["users"],
91+
tags: ['posts'],
8492
revalidate: 60,
8593
}
86-
);
94+
)
95+
96+
const posts = await getCachedPosts()
8797

88-
//...
98+
return (
99+
<ul>
100+
{posts.map((post) => (
101+
<li key={post.id}>{post.title}</li>
102+
))}
103+
</ul>
104+
)
89105
}
90106
```
91107

0 commit comments

Comments
 (0)