Skip to content

Commit cec502e

Browse files
committed
refactor: profile image API and add server-side caching with sitemap improvements
1 parent e880c2c commit cec502e

File tree

4 files changed

+10
-21
lines changed

4 files changed

+10
-21
lines changed

pages/api/profile-image.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
88
if (!process.env.NOTION_PROFILE_ID) throw new Error('NOTION_PROFILE_ID is not defined');
99
const pageId = process.env.NOTION_PROFILE_ID;
1010
try {
11-
const response = await notionClient.pages.retrieve({
12-
page_id: pageId,
13-
});
11+
const url = await notionClient.getFileUrl(pageId, 'media');
1412

15-
// 타입 체크를 위한 조건문 추가
16-
if (!('properties' in response) || !response.properties.media) {
17-
return res.status(404).json({ message: 'Media files not found' });
18-
}
19-
20-
// 타입 선언 후 안전하게 접근
21-
const mediaProperty = response.properties.media;
22-
23-
// media 파일 배열이 비어있지 않은지 확인 후 파일 URL 추출
24-
if (
25-
'files' in mediaProperty &&
26-
mediaProperty.files.length > 0 &&
27-
'file' in mediaProperty.files[0]
28-
) {
29-
const fileUrl = mediaProperty.files[0].file.url;
30-
const response = await fetch(fileUrl);
13+
if (url) {
14+
const response = await fetch(url);
3115
const contentType = response.headers.get('content-type');
3216

3317
if (!contentType) throw new Error('content header does not exist');

pages/sitemap.xml.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export const getServerSideProps: GetServerSideProps = async ({ res }) => {
1010
if (!process.env.NOTION_POST_DATABASE_ID)
1111
throw new Error('NOTION_POST_DATABASE_ID is not defined');
1212
const databaseItems = await getNotionPosts(process.env.NOTION_POST_DATABASE_ID);
13-
1413
res.setHeader('Content-Type', 'text/xml');
1514
res.write(generateSitemap(databaseItems));
1615
res.end();

src/serverCache/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// ! Only use this cache for server-side rendering
2+
const globalCache = new Map();
3+
4+
export default globalCache;

src/utils/generateSitemap.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { GetPageResponse } from 'notion-to-utils';
22

3+
import { SlugAndDate } from '@/interfaces';
4+
35
import getSlugsAndDates from '@/utils/getSlugsAndDates';
46

57
const generateSitemap = (notionPostsResponse: GetPageResponse[]) => {
6-
const paths = getSlugsAndDates(notionPostsResponse);
8+
const paths: SlugAndDate[] = getSlugsAndDates(notionPostsResponse);
79

810
const urlSet = paths
911
.map((path) => {

0 commit comments

Comments
 (0)