// app/api/sitemap/route.js import { getAllSlugs } from '@/utils/data'; import fs from 'fs'; import path from 'path'; export async function POST(req) { const secret = req.headers.get('x-revalidate-secret'); if (secret !== process.env.REVALIDATE_TOKEN) { return new Response('Unauthorized', { status: 401 }); } const slugs = await getAllSlugs(); const siteUrl = process.env.SITE_URL || 'https://yourdomain.com'; const urls = slugs.map(slugArr => { if (slugArr.length === 1 && slugArr[0] === '/') { return '/'; } return '/' + slugArr.join('/'); }); let uniqueUrls = Array.from(new Set(urls)); if (!uniqueUrls.includes('/')) { uniqueUrls.unshift('/'); } else { uniqueUrls = uniqueUrls.filter(url => url !== '/'); uniqueUrls.unshift('/'); } const sitemap = `\n\n${uniqueUrls.map(url => `\n \n ${siteUrl}${url}\n weekly\n 0.7\n `).join('')}\n`; fs.writeFileSync(path.join(process.cwd(), 'public/sitemap.xml'), sitemap, 'utf8'); return new Response('Sitemap regenerated', { status: 200 }); }