Skip to content

Commit 27f82b3

Browse files
committed
fix: 파일에서 meta 정보만 가져오게 수정
- 모든 포스트의 mdxSource 까지 포함되면 초기 로딩시 영향을 준다
1 parent d5d6f7f commit 27f82b3

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

lib/posts.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,26 @@ export async function getSortedPostsDataWithContent(): Promise<PostData[]> {
105105
(a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()
106106
);
107107
}
108+
109+
export type PostMeta = PostFrontmatter & {
110+
id: string;
111+
};
112+
113+
export function getPostsMetaOnly(): PostMeta[] {
114+
const allFiles = getAllMdxFiles();
115+
116+
const allPostsMeta: PostMeta[] = allFiles.map((fullPath) => {
117+
const id = extractIdFromFilename(fullPath);
118+
const fileContents = fs.readFileSync(fullPath, 'utf8');
119+
const { data } = matter(fileContents);
120+
121+
return {
122+
id,
123+
...(data as PostFrontmatter),
124+
};
125+
});
126+
127+
return allPostsMeta.sort(
128+
(a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()
129+
);
130+
}

pages/index.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import Link from 'next/link';
22
import Header from '../components/Header';
33
import Footer from '../components/Footer';
4-
import { MDXRemoteSerializeResult } from 'next-mdx-remote';
54
import { GetStaticProps } from 'next';
6-
import { getSortedPostsDataWithContent } from '../lib/posts';
5+
import {getPostsMetaOnly, getSortedPostsDataWithContent} from '../lib/posts';
76
import AdSense from '../components/AdSense';
87

98
const gradients = [
@@ -21,15 +20,13 @@ type Post = {
2120
title: string;
2221
summary?: string;
2322
description?: string;
24-
mdxSource: MDXRemoteSerializeResult<Record<string, unknown>>;
2523
};
2624

2725
type HomeProps = {
2826
allPostsData: Post[];
2927
gradientsForPosts: string[];
3028
};
3129

32-
// Fisher-Yates 셔플
3330
function shuffle<T>(array: T[]): T[] {
3431
const copy = [...array];
3532
for (let i = copy.length - 1; i > 0; i--) {
@@ -40,12 +37,11 @@ function shuffle<T>(array: T[]): T[] {
4037
}
4138

4239
export const getStaticProps: GetStaticProps<HomeProps> = async () => {
43-
const allPostsData = await getSortedPostsDataWithContent();
40+
const allPostsData = getPostsMetaOnly();
4441
const [latest, ...restAll] = allPostsData;
4542

4643
const rest = restAll.filter((post) => post.id !== latest.id).slice(0, 3);
4744

48-
// 서버에서 고정된 랜덤 gradient 선택 (중복 방지)
4945
const gradientsForPosts = shuffle(gradients).slice(0, rest.length);
5046

5147
return {

pages/post.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@ import { useState, useEffect } from 'react';
22
import Link from 'next/link';
33
import Header from '../components/Header';
44
import { GetStaticProps } from 'next';
5-
import { getSortedPostsDataWithContent } from '../lib/posts';
6-
import { MDXRemoteSerializeResult } from 'next-mdx-remote';
5+
import { getPostsMetaOnly } from '../lib/posts';
76

87
type Post = {
98
id: string;
109
title: string;
1110
summary?: string;
1211
description?: string;
1312
tags?: string[];
14-
mdxSource: MDXRemoteSerializeResult;
1513
};
1614

1715
type Props = {
@@ -20,7 +18,7 @@ type Props = {
2018
};
2119

2220
export const getStaticProps: GetStaticProps<Props> = async () => {
23-
const posts = await getSortedPostsDataWithContent();
21+
const posts = getPostsMetaOnly();
2422

2523
const postsByTag: Record<string, Post[]> = {};
2624
const tagCounts: Record<string, number> = {};

0 commit comments

Comments
 (0)