import { siteConfig } from '$lib/config/site';
import { navConfig } from '$lib/config/nav';
import type { NavItem } from '$lib/types/nav';
const SITE_URL = siteConfig.url;
/**
* SvelteKit RequestHandler for generating sitemap.
*/
export async function GET() {
const pages = generatePagePaths(navConfig.mainNav);
const body = generateSitemapXml(SITE_URL, pages);
return new Response(body, {
headers: {
'Cache-Control': `public, max-age=${86400}`, // 24 hours
'Content-Type': 'application/xml'
}
});
}
/**
* Generates paths for pages from navigation configuration.
* @param {Array} navItems Navigation items configuration.
* @returns {Array} Filtered and transformed page paths.
*/
function generatePagePaths(navItems: NavItem[]) {
return navItems
.map((item: { href: string }) => item.href.replace(/^\//, ''))
.filter((href: string) => href !== '');
}
/**
* Generates XML sitemap content.
* @param {string} siteUrl Base site URL.
* @param {Array} pages Array of page paths.
* @returns {string} Sitemap XML content.
*/
function generateSitemapXml(siteUrl: string, pages: string[]) {
return `
${generateUrlElement(siteUrl)}
${pages.map((page: string) => generateUrlElement(`${siteUrl}/${page}`)).join('')}
`;
}
/**
* Generates a URL element for sitemap XML.
* @param {string} url URL for the sitemap entry.
* @returns {string} URL element XML string.
*/
function generateUrlElement(url: string) {
return `
${url}
daily
0.7
`;
}