feat: create sitemap.xml programmatically

This commit is contained in:
Bart van der Braak 2024-01-16 02:44:46 +01:00
parent 70a0eb8aa1
commit 7d70f2d2ed
2 changed files with 68 additions and 1 deletions

View file

@ -2,7 +2,7 @@ import type { Icons } from '$lib/components/site/icons';
export type NavItem = {
title: string;
href?: string;
href: string;
disabled?: boolean;
external?: boolean;
icon?: keyof typeof Icons;

View file

@ -0,0 +1,67 @@
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 `<?xml version="1.0" encoding="UTF-8" ?>
<urlset
xmlns="https://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:news="https://www.google.com/schemas/sitemap-news/0.9"
xmlns:xhtml="https://www.w3.org/1999/xhtml"
xmlns:mobile="https://www.google.com/schemas/sitemap-mobile/1.0"
xmlns:image="https://www.google.com/schemas/sitemap-image/1.1"
xmlns:video="https://www.google.com/schemas/sitemap-video/1.1"
>
${generateUrlElement(siteUrl)}
${pages.map((page: string) => generateUrlElement(`${siteUrl}/${page}`)).join('')}
</urlset>`;
}
/**
* 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>
<loc>${url}</loc>
<changefreq>daily</changefreq>
<priority>0.7</priority>
</url>
`;
}