Skip to content

Commit ae3ecfe

Browse files
committed
refactor: fetch previous post slug only
1 parent f94e1fe commit ae3ecfe

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

app/posts/[slug]/actions.test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as postsActions from '../actions'
22

3-
import { fetchPostBySlug, fetchPreviousPost } from './actions'
3+
import { fetchPostBySlug, fetchPreviousPostSlug } from './actions'
44

55
import * as mdx from '@/lib/mdx'
66
import type { Post } from '@/lib/types'
@@ -57,7 +57,7 @@ describe('/posts/[slug]/actions', () => {
5757
})
5858
})
5959

60-
describe('fetchPreviousPost', () => {
60+
describe('fetchPreviousPostBySlug', () => {
6161
const mockPosts: Post[] = [
6262
{ slug: 'slug-1', ...getMockFrontmatter(), source: getMockSource() },
6363
{ slug: 'slug-2', ...getMockFrontmatter(), source: getMockSource() },
@@ -66,15 +66,14 @@ describe('/posts/[slug]/actions', () => {
6666
it('returns the next index of results returned from fetchPublishedPosts()', async () => {
6767
vi.mocked(postsActions.fetchPublishedPosts).mockResolvedValue(mockPosts)
6868

69-
const actual = await fetchPreviousPost('slug-1')
70-
expect(actual).toBeTruthy()
71-
expect(actual?.slug).toEqual('slug-2')
69+
const actual = await fetchPreviousPostSlug('slug-1')
70+
expect(actual).toEqual('slug-2')
7271
})
7372

7473
it('returns undefined when the bottom of the list is reached', async () => {
7574
vi.mocked(postsActions.fetchPublishedPosts).mockResolvedValue(mockPosts)
7675

77-
const actual = await fetchPreviousPost('slug-2')
76+
const actual = await fetchPreviousPostSlug('slug-2')
7877
expect(actual).toBeUndefined()
7978
})
8079
})

app/posts/[slug]/actions.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ export async function fetchPostBySlug(slug: string): Promise<Post> {
2323
}
2424
}
2525

26-
export async function fetchPreviousPost(slug: string): Promise<Post | undefined> {
26+
export async function fetchPreviousPostSlug(slug: string): Promise<string | undefined> {
2727
const publishedPosts = await fetchPublishedPosts()
2828
const postIndex = publishedPosts.findIndex((post) => post.slug === slug)
2929

3030
if (postIndex === publishedPosts.length - 1) return undefined
3131

32-
return publishedPosts[postIndex + 1]
32+
const previousPost = publishedPosts.at(postIndex + 1)
33+
34+
return previousPost && previousPost.slug
3335
}

app/posts/[slug]/page.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Link from 'next/link'
55

66
import { fetchPublishedPosts } from '../actions'
77

8-
import { fetchPostBySlug, fetchPreviousPost } from './actions'
8+
import { fetchPostBySlug, fetchPreviousPostSlug } from './actions'
99
import MDXContent from './mdx-content'
1010
import TimeInformation from './time-information'
1111

@@ -49,8 +49,7 @@ export const generateMetadata = async ({ params }: { params: Promise<{ slug: str
4949

5050
export default async function PostPage({ params }: { params: Promise<{ slug: string }> }) {
5151
const { slug } = await params
52-
const post = await fetchPostBySlug(slug)
53-
const previousPost = await fetchPreviousPost(slug)
52+
const [post, previousPostSlug] = await Promise.all([fetchPostBySlug(slug), fetchPreviousPostSlug(slug)])
5453

5554
return (
5655
<div className="mx-0 my-10 flex flex-col items-center md:mx-20">
@@ -74,9 +73,9 @@ export default async function PostPage({ params }: { params: Promise<{ slug: str
7473
<ChevronLeft width={16} height={16} />
7574
&nbsp;All posts
7675
</Link>
77-
{!!previousPost && (
76+
{previousPostSlug && (
7877
<Link
79-
href={`/posts/${previousPost.slug}`}
78+
href={`/posts/${previousPostSlug}`}
8079
className={cn(buttonVariants({ variant: 'outline' }), 'w-2/5 md:w-1/2')}
8180
>
8281
<ChevronRight width={16} height={16} />

0 commit comments

Comments
 (0)