105 lines
3.0 KiB
TypeScript
105 lines
3.0 KiB
TypeScript
import { getAllBlogs } from "@/content/blogs/blogs";
|
|
import { getAllAlbums } from "@/content/photos/albums";
|
|
import { getAllProjects } from "@/content/projects/projects";
|
|
import { getSettings } from "@/content/settings/settings";
|
|
import type { APIRoute } from "astro";
|
|
import minifyXML from "minify-xml";
|
|
|
|
export const GET = (async () => {
|
|
const settings = await getSettings();
|
|
|
|
let sitemapIndex: SitemapIndex[] = [
|
|
{
|
|
url: "/sitemap/pages.xml",
|
|
lastModified: new Date()
|
|
}
|
|
];
|
|
|
|
if (settings.blog.enabled) {
|
|
const blogLastModifieds = [
|
|
settings.blog.lastModified,
|
|
settings.sitemap.lastModified,
|
|
settings.website.lastModified
|
|
];
|
|
|
|
let blogs = await getAllBlogs(settings);
|
|
|
|
blogs.forEach((blog) => {
|
|
blogLastModifieds.push(blog.lastModified);
|
|
});
|
|
|
|
const lastModifiedBlogs = blogLastModifieds.sort((a: Date, b: Date) => {
|
|
return b.getTime() - a.getTime();
|
|
});
|
|
|
|
sitemapIndex.push({
|
|
url: "/sitemap/blogs.xml",
|
|
lastModified: lastModifiedBlogs[0]
|
|
});
|
|
};
|
|
if (settings.project.enabled) {
|
|
const projectLastModifieds = [
|
|
settings.project.lastModified,
|
|
settings.sitemap.lastModified,
|
|
settings.website.lastModified
|
|
];
|
|
|
|
let projects = await getAllProjects(settings);
|
|
|
|
projects.forEach((project) => {
|
|
projectLastModifieds.push(project.lastModified);
|
|
});
|
|
|
|
const lastModifiedProjects = projectLastModifieds.sort((a: Date, b: Date) => {
|
|
return b.getTime() - a.getTime();
|
|
});
|
|
|
|
sitemapIndex.push({
|
|
url: "/sitemap/projects.xml",
|
|
lastModified: lastModifiedProjects[0]
|
|
});
|
|
};
|
|
if (settings.photo.enabled) {
|
|
const photoLastModifieds = [
|
|
settings.photo.lastModified,
|
|
settings.sitemap.lastModified,
|
|
settings.website.lastModified
|
|
];
|
|
|
|
let albums = await getAllAlbums(settings);
|
|
|
|
albums.forEach((album) => {
|
|
photoLastModifieds.push(album.lastModified);
|
|
});
|
|
|
|
const lastModifiedAlbums = photoLastModifieds.sort((a: Date, b: Date) => {
|
|
return b.getTime() - a.getTime();
|
|
});
|
|
|
|
sitemapIndex.push({
|
|
url: "/sitemap/albums.xml",
|
|
lastModified: lastModifiedAlbums[0]
|
|
});
|
|
};
|
|
|
|
let sitemapContent = `
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
|
${sitemapIndex.map((item) => `
|
|
<sitemap>
|
|
<loc>${settings.website.domainName}${item.url}</loc>
|
|
<lastmod>${item.lastModified.toISOString()}</lastmod>
|
|
</sitemap>
|
|
`).join('')}
|
|
</sitemapindex>
|
|
`;
|
|
|
|
return new Response(minifyXML(sitemapContent), {
|
|
status: 200,
|
|
statusText: "OK",
|
|
headers: {
|
|
"Content-Type": "application/xml"
|
|
}
|
|
});
|
|
}) satisfies APIRoute;
|