diff --git a/src/lib/types/nav.ts b/src/lib/types/nav.ts index a6d0cec..a4eecd2 100644 --- a/src/lib/types/nav.ts +++ b/src/lib/types/nav.ts @@ -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; diff --git a/src/routes/sitemap.xml/+server.ts b/src/routes/sitemap.xml/+server.ts new file mode 100644 index 0000000..9a72964 --- /dev/null +++ b/src/routes/sitemap.xml/+server.ts @@ -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 ` + + ${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 + + `; +}