Skip to content

Commit

Permalink
Add caching for fetching next and previous articles in `ContentDetail…
Browse files Browse the repository at this point in the history
…Page`

* Create `cachedGetNextArticle` and `cachedGetPreviousArticle` functions with caching similar to `cachedGetContentDetail`
* Replace direct calls to `getNextArticle` and `getPreviousArticle` with cached versions in `ContentDetailPage`
* Pass the next and previous articles as props to the `ContentDetail` component
  • Loading branch information
WakuwakuP committed Nov 6, 2024
1 parent 8ff2c80 commit 1d5611f
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/app/content/detail/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,28 @@ const cachedGetContentDetail = (id: string) =>
},
)

const cachedGetNextArticle = (publishedAt: string) =>
unstable_cache(
async () => {
return await getNextArticle(publishedAt)
},
['next-article', publishedAt],
{
tags: ['next-article'],
},
)

const cachedGetPreviousArticle = (publishedAt: string) =>
unstable_cache(
async () => {
return await getPreviousArticle(publishedAt)
},
['previous-article', publishedAt],
{
tags: ['previous-article'],
},
)

export default async function ContentDetailPage({ params }: { params: { id: string } }) {
const getContentDetail = cachedGetContentDetail(params.id)

Expand All @@ -67,8 +89,8 @@ export default async function ContentDetailPage({ params }: { params: { id: stri
}

const content = data
const nextArticle = await getNextArticle(content.publishedAt)
const previousArticle = await getPreviousArticle(content.publishedAt)
const nextArticle = cachedGetNextArticle(content.publishedAt)
const previousArticle = cachedGetPreviousArticle(content.publishedAt)

return (
<ContentDetail
Expand Down

0 comments on commit 1d5611f

Please sign in to comment.