import { allPosts } from 'contentlayer2/generated'; import { siteConfig } from '@/lib/config'; export async function GET() { const sortedPosts = allPosts .filter((post) => post.status === 'published') .sort((a, b) => { const dateA = a.published_at ? new Date(a.published_at).getTime() : 0; const dateB = b.published_at ? new Date(b.published_at).getTime() : 0; return dateB - dateA; }) .slice(0, 20); // Latest 20 posts const siteUrl = process.env.NEXT_PUBLIC_SITE_URL || 'http://localhost:3000'; const rss = ` ${escapeXml(siteConfig.name)} ${siteUrl} ${escapeXml(siteConfig.description)} ${siteConfig.defaultLocale.replace('_', '-')} ${new Date().toUTCString()} ${sortedPosts .map((post) => { const postUrl = `${siteUrl}${post.url}`; const pubDate = post.published_at ? new Date(post.published_at).toUTCString() : new Date(post.created_at || Date.now()).toUTCString(); return ` ${escapeXml(post.title)} ${postUrl} ${postUrl} ${escapeXml(post.description || post.custom_excerpt || post.title)} ${post.body?.html ? `` : ''} ${pubDate} ${post.authors?.map((author) => `${escapeXml(author)}`).join('\n ') || ''} ${post.tags?.map((tag) => `${escapeXml(tag)}`).join('\n ') || ''} `; }) .join('')} `; return new Response(rss, { headers: { 'Content-Type': 'application/xml; charset=utf-8', 'Cache-Control': 'public, max-age=3600, s-maxage=3600', }, }); } function escapeXml(unsafe: string): string { return unsafe .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"') .replace(/'/g, '''); }