32 lines
1 KiB
TypeScript
32 lines
1 KiB
TypeScript
import { publishedPosts } from '@/lib/posts';
|
|
|
|
export const dynamic = 'force-static';
|
|
|
|
function escapeXml(s: string): string {
|
|
return s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
}
|
|
|
|
export function GET() {
|
|
const base = (process.env.NEXT_PUBLIC_SITE_URL || 'http://localhost:3000').replace(/\/$/, '');
|
|
const posts = publishedPosts();
|
|
const items = posts
|
|
.map(
|
|
(p) => ` <item>
|
|
<title>${escapeXml(p.title)}</title>
|
|
<link>${base}/blog/${p.slug}</link>
|
|
<guid>${base}/blog/${p.slug}</guid>
|
|
<pubDate>${new Date(p.date).toUTCString()}</pubDate>
|
|
<description>${escapeXml(p.excerpt)}</description>
|
|
</item>`,
|
|
)
|
|
.join('\n');
|
|
const xml = `<?xml version="1.0" encoding="UTF-8"?>
|
|
<rss version="2.0"><channel>
|
|
<title>Acme Blog</title>
|
|
<link>${base}/blog</link>
|
|
<description>Notes on launching and growing a product site.</description>
|
|
${items}
|
|
</channel></rss>`;
|
|
return new Response(xml, { headers: { 'content-type': 'application/rss+xml; charset=utf-8' } });
|
|
}
|