-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
61 lines (54 loc) · 1.92 KB
/
route.ts
File metadata and controls
61 lines (54 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { getAllBlogPosts } from "../blogs/utils";
import { twitterArticles } from "../blogs/articles";
import { siteConfig } from "@/site.config";
function parseDate(dateStr: string | undefined) {
if (!dateStr) return new Date().toISOString();
const parsed = new Date(dateStr);
return isNaN(parsed.getTime()) ? new Date().toISOString() : parsed.toISOString();
}
export async function GET() {
const baseUrl = siteConfig.contact.url;
const posts = await getAllBlogPosts();
const pages = [
{ path: "", priority: "1.0", changefreq: "weekly" },
{ path: "/blogs", priority: "0.9", changefreq: "daily" },
{ path: "/projects", priority: "0.8", changefreq: "weekly" },
{ path: "/experience", priority: "0.7", changefreq: "monthly" },
{ path: "/hackathons", priority: "0.7", changefreq: "monthly" },
{ path: "/research", priority: "0.7", changefreq: "monthly" },
{ path: "/newsletter", priority: "0.5", changefreq: "monthly" },
].map(({ path, priority, changefreq }) => ({
url: `${baseUrl}${path}`,
lastModified: new Date().toISOString(),
priority,
changefreq,
}));
const externalSlugs = new Set(twitterArticles.map((a) => a.slug));
const blogPosts = posts
.filter((post) => !externalSlugs.has(post.slug))
.map((post) => ({
url: `${baseUrl}/blogs/${post.slug}`,
lastModified: parseDate(post.date),
priority: "0.8",
changefreq: "weekly",
}));
const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${[...pages, ...blogPosts]
.map(
(page) => `
<url>
<loc>${page.url}</loc>
<lastmod>${page.lastModified}</lastmod>
<priority>${page.priority}</priority>
<changefreq>${page.changefreq}</changefreq>
</url>`
)
.join("")}
</urlset>`;
return new Response(sitemap, {
headers: {
"Content-Type": "application/xml",
},
});
}