59 lines
1.7 KiB
TypeScript
59 lines
1.7 KiB
TypeScript
import {NextResponse} from "next/server";
|
|
|
|
interface SitemapRoute {
|
|
url: string;
|
|
lastModified: string;
|
|
changeFrequency?:
|
|
| "always"
|
|
| "hourly"
|
|
| "daily"
|
|
| "weekly"
|
|
| "monthly"
|
|
| "yearly"
|
|
| "never";
|
|
priority?: number;
|
|
}
|
|
|
|
const baseUrl = "https://dki.one";
|
|
|
|
export async function GET() {
|
|
try {
|
|
const response = await fetch(`${baseUrl}/api/sitemap`);
|
|
if (!response.ok) {
|
|
console.error(`Failed to fetch dynamic routes: ${response.statusText}`);
|
|
}
|
|
|
|
const dynamicRoutes = (await response.json()) as SitemapRoute[];
|
|
const sitemap = [...dynamicRoutes];
|
|
|
|
return new NextResponse(generateXml(sitemap), {
|
|
headers: {"Content-Type": "application/xml"},
|
|
});
|
|
} catch (error) {
|
|
console.error("Error fetching dynamic routes, using fallback:", error);
|
|
return new NextResponse(generateXml([]), {
|
|
headers: {"Content-Type": "application/xml"},
|
|
});
|
|
}
|
|
}
|
|
|
|
function generateXml(routes: SitemapRoute[]): string {
|
|
const xmlHeader = '<?xml version="1.0" encoding="UTF-8"?>';
|
|
const urlsetOpen = '<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">';
|
|
const urlsetClose = '</urlset>';
|
|
|
|
const urlEntries = routes
|
|
.map(
|
|
(route) => `
|
|
<url>
|
|
<loc>${route.url}</loc>
|
|
<lastmod>${route.lastModified}</lastmod>
|
|
${route.changeFrequency ? `<changefreq>${route.changeFrequency}</changefreq>` : ""}
|
|
${route.priority ? `<priority>${route.priority}</priority>` : ""}
|
|
</url>`
|
|
)
|
|
.join("");
|
|
|
|
return `${xmlHeader}${urlsetOpen}${urlEntries}${urlsetClose}`;
|
|
}
|