Skip to content

Commit 76765a5

Browse files
committed
time change
1 parent eafad5b commit 76765a5

1 file changed

Lines changed: 47 additions & 26 deletions

File tree

app/sitemap.ts

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,68 @@
11
import { getPostDatabase } from '@/notionApi/getPostDatabase'
2-
import { MetadataRoute } from 'next'
32
import { PageObjectResponse, DatePropertyItemObjectResponse } from '@notionhq/client/build/src/api-endpoints'
43

54
// 날짜를 YYYY-MM-DD 형식으로 포맷팅하는 헬퍼 함수
65
function formatDate(date: Date): string {
76
return date.toISOString().split('T')[0]
87
}
98

10-
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
9+
export default async function sitemap() {
1110
try {
1211
const baseUrl = 'https://ppippi-dev.github.io'
1312

14-
// 정적 페이지 URLs
15-
const staticPages = [
16-
{
17-
url: baseUrl,
18-
lastModified: formatDate(new Date(new Date().getTime() - (9 * 60 * 60 * 1000)))
19-
}
20-
]
13+
// XML 헤더와 시작 태그
14+
let xml = '<?xml version="1.0" encoding="UTF-8"?>\n'
15+
xml += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n'
16+
17+
// 메인 페이지 추가
18+
xml += '<url>\n'
19+
xml += `<loc>${baseUrl}</loc>\n`
20+
xml += `<lastmod>${formatDate(new Date(new Date().getTime() - (9 * 60 * 60 * 1000)))}</lastmod>\n`
21+
xml += '</url>\n'
2122

23+
// 블로그 포스트 추가
2224
const posts = await getPostDatabase()
2325

24-
const postsUrls = posts.results.map((post) => {
26+
for (const post of posts.results) {
2527
const postDate = ((post as PageObjectResponse).properties.post_date as DatePropertyItemObjectResponse).date?.start
26-
return {
27-
url: `${baseUrl}/post/${post.id}`,
28-
lastModified: postDate
29-
? formatDate(new Date(new Date(postDate).getTime() - (9 * 60 * 60 * 1000)))
30-
: formatDate(new Date(new Date().getTime() - (9 * 60 * 60 * 1000))),
31-
changefreq: 'monthly'
32-
}
28+
const lastmod = postDate
29+
? formatDate(new Date(new Date(postDate).getTime() - (9 * 60 * 60 * 1000)))
30+
: formatDate(new Date(new Date().getTime() - (9 * 60 * 60 * 1000)))
31+
32+
xml += '<url>\n'
33+
xml += `<loc>${baseUrl}/post/${post.id}</loc>\n`
34+
xml += `<lastmod>${lastmod}</lastmod>\n`
35+
xml += '</url>\n'
36+
}
37+
38+
// XML 종료 태그
39+
xml += '</urlset>'
40+
41+
// Response 헤더 설정과 함께 XML 반환
42+
return new Response(xml, {
43+
headers: {
44+
'Content-Type': 'application/xml',
45+
'Cache-Control': 'public, max-age=3600',
46+
},
3347
})
3448

35-
return [
36-
...staticPages,
37-
...postsUrls,
38-
]
3949
} catch (error) {
4050
console.error('Sitemap generation failed:', error)
41-
// 기본 sitemap 반환
42-
return [{
43-
url: 'https://ppippi-dev.github.io',
44-
lastModified: formatDate(new Date(new Date().getTime() - (9 * 60 * 60 * 1000)))
45-
}]
51+
52+
// 에러 발생 시 기본 sitemap 반환
53+
const fallbackXml = `<?xml version="1.0" encoding="UTF-8"?>
54+
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
55+
<url>
56+
<loc>https://ppippi-dev.github.io</loc>
57+
<lastmod>${formatDate(new Date(new Date().getTime() - (9 * 60 * 60 * 1000)))}</lastmod>
58+
</url>
59+
</urlset>`
60+
61+
return new Response(fallbackXml, {
62+
headers: {
63+
'Content-Type': 'application/xml',
64+
'Cache-Control': 'public, max-age=3600',
65+
},
66+
})
4667
}
4768
}

0 commit comments

Comments
 (0)