Files
portfolio/lib/sitemap.ts
denshooter 2c2c1f5d2d
All checks were successful
CI / CD / test-build (push) Successful in 10m16s
CI / CD / deploy-dev (push) Successful in 1m55s
CI / CD / deploy-production (push) Has been skipped
fix: SEO canonical URLs, LCP performance, remove unused dependencies
- Remove duplicate app/projects/ route (was causing 5xx and soft 404)
- Fix nginx: redirect www.dk0.dev → dk0.dev (non-www canonical)
- Fix not-found.tsx: locale-prefixed links, remove framer-motion dependency
- Add fetchPriority='high' and will-change to Hero LCP image
- Add preconnect hints for hardcover.app and cms.dk0.dev
- Reduce background blur from 100px to 80px (LCP rendering delay)
- Remove boneyard-js (~20 KiB), replace with custom Skeleton component
- Remove react-icons (~10 KiB), replace with inline SVGs
- Conditionally render mobile menu (saves ~20 DOM nodes)
- Add /books to sitemap
- Optimize image config with explicit deviceSizes/imageSizes
2026-04-17 09:50:31 +02:00

85 lines
2.9 KiB
TypeScript

import { prisma } from "@/lib/prisma";
import { locales } from "@/i18n/locales";
import { getBaseUrl } from "@/lib/seo";
import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library";
export type SitemapEntry = {
url: string;
lastModified: string;
changefreq?: "daily" | "weekly" | "monthly" | "yearly";
priority?: number;
};
export function generateSitemapXml(entries: SitemapEntry[]): 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 = entries
.map((e) => {
const changefreq = e.changefreq ?? "monthly";
const priority = typeof e.priority === "number" ? e.priority : 0.8;
return `
<url>
<loc>${e.url}</loc>
<lastmod>${e.lastModified}</lastmod>
<changefreq>${changefreq}</changefreq>
<priority>${priority.toFixed(1)}</priority>
</url>`;
})
.join("");
return `${xmlHeader}${urlsetOpen}${urlEntries}${urlsetClose}`;
}
export async function getSitemapEntries(): Promise<SitemapEntry[]> {
const baseUrl = getBaseUrl();
const nowIso = new Date().toISOString();
const staticPaths = ["", "/projects", "/books", "/legal-notice", "/privacy-policy"];
const staticEntries: SitemapEntry[] = locales.flatMap((locale) =>
staticPaths.map((p) => {
const path = p === "" ? `/${locale}` : `/${locale}${p}`;
return {
url: `${baseUrl}${path}`,
lastModified: nowIso,
changefreq: p === "" ? "weekly" : (p === "/projects" || p === "/books") ? "weekly" : "yearly",
priority: p === "" ? 1.0 : (p === "/projects" || p === "/books") ? 0.8 : 0.5,
};
}),
);
// Projects: for each project slug we publish per locale (same slug)
let projects: Array<{ slug: string; updatedAt: Date | null }> = [];
try {
projects = await prisma.project.findMany({
where: { published: true },
select: { slug: true, updatedAt: true },
orderBy: { updatedAt: "desc" },
});
} catch (error) {
// If DB isn't ready/migrated/reachable yet, still serve a valid sitemap for static pages.
if (process.env.NODE_ENV === "development") {
console.warn("Sitemap: failed to load projects; serving static entries only.", error);
}
if (error instanceof PrismaClientKnownRequestError && (error.code === "P2021" || error.code === "P2022")) {
return staticEntries;
}
// Also fail soft on connection/init errors in dev/staging (keeps sitemap valid for crawlers)
return staticEntries;
}
const projectEntries: SitemapEntry[] = projects.flatMap((p) => {
const lastModified = (p.updatedAt ?? new Date()).toISOString();
return locales.map((locale) => ({
url: `${baseUrl}/${locale}/projects/${p.slug}`,
lastModified,
changefreq: "monthly",
priority: 0.7,
}));
});
return [...staticEntries, ...projectEntries];
}