26 lines
915 B
JavaScript
26 lines
915 B
JavaScript
// 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 => '/' + slugArr.join('/'));
|
|
const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
|
|
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
|
${urls.map(url => `
|
|
<url>
|
|
<loc>${siteUrl}${url}</loc>
|
|
<changefreq>weekly</changefreq>
|
|
<priority>0.7</priority>
|
|
</url>
|
|
`).join('')}
|
|
</urlset>`;
|
|
fs.writeFileSync(path.join(process.cwd(), 'public/sitemap.xml'), sitemap, 'utf8');
|
|
return new Response('Sitemap regenerated', { status: 200 });
|
|
} |