21 lines
749 B
TypeScript
21 lines
749 B
TypeScript
import { NextApiRequest, NextApiResponse } from 'next';
|
|
import { getAllPosts } from '../../../lib/content';
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
const { lang } = req.query as { lang: string };
|
|
const hostname = process.env.SITE_URL || 'https://your-domain.com';
|
|
const posts = getAllPosts(lang).filter(p => p.frontmatter.sitemap);
|
|
|
|
const urls = posts.map(p =>
|
|
`<url><loc>${hostname}/${lang}/${p.slug.join('/')}</loc><changefreq>daily</changefreq></url>`
|
|
).join('');
|
|
|
|
const xml = `<?xml version="1.0" encoding="UTF-8"?>
|
|
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
|
${urls}
|
|
</urlset>`;
|
|
|
|
res.setHeader('Content-Type', 'application/xml');
|
|
res.write(xml);
|
|
res.end();
|
|
} |