refactor(sitemap): improve sitemap generation and error handling; rename sitemap file

This commit is contained in:
2025-02-13 16:10:14 +01:00
parent 12d7709437
commit a8f10c8e37
4 changed files with 69 additions and 93 deletions

View File

@@ -1,4 +1,5 @@
This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app).
This is a [Next.js](https://nextjs.org) project bootstrapped with [
`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app).
## Getting Started
@@ -16,9 +17,10 @@ bun dev
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.
You can start editing the page by modifying `app/route.tsx`. The page auto-updates as you edit the file.
This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel.
This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically
optimize and load [Geist](https://vercel.com/font), a new font family for Vercel.
## Learn More
@@ -27,10 +29,14 @@ To learn more about Next.js, take a look at the following resources:
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js) - your feedback and contributions are welcome!
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js) - your feedback and contributions
are welcome!
## Deploy on Vercel
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
The easiest way to deploy your Next.js app is to use
the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme)
from the creators of Next.js.
Check out our [Next.js deployment documentation](https://nextjs.org/docs/app/building-your-application/deploying) for more details.
Check out our [Next.js deployment documentation](https://nextjs.org/docs/app/building-your-application/deploying) for
more details.

View File

@@ -1,4 +1,4 @@
import { NextResponse } from "next/server";
import {NextResponse} from "next/server";
interface Project {
slug: string;
@@ -17,32 +17,17 @@ interface SitemapRoute {
const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || "https://dki.one";
const generateSitemap = async (): Promise<SitemapRoute[]> => {
// Static pages
const staticRoutes: SitemapRoute[] = [
{ url: `${baseUrl}/`, lastModified: new Date().toISOString() },
{
url: `${baseUrl}/privacy-policy`,
lastModified: new Date().toISOString(),
},
{
url: `${baseUrl}/legal-notice`,
lastModified: new Date().toISOString(),
},
];
try {
console.log("Fetching project data from API...");
const response = await fetch(`${baseUrl}/api/fetchAllProjects`, {
headers: { "Cache-Control": "no-cache" },
headers: {"Cache-Control": "no-cache"},
});
if (!response.ok) {
console.error(`Failed to fetch projects: ${response.statusText}`);
return staticRoutes; // Fallback auf statische Seiten
return [];
}
const projectsData = (await response.json()) as ProjectsData;
console.log("Fetched project data:", projectsData);
// Dynamische Projekt-Routen generieren
const projectRoutes: SitemapRoute[] = projectsData.posts.map((project) => ({
@@ -52,10 +37,10 @@ const generateSitemap = async (): Promise<SitemapRoute[]> => {
: new Date().toISOString(),
}));
return [...staticRoutes, ...projectRoutes];
return [...projectRoutes];
} catch (error) {
console.error("Failed to generate sitemap:", error);
return staticRoutes; // Fallback nur auf statische Seiten
return [];
}
};
@@ -63,13 +48,13 @@ export async function GET() {
try {
const sitemap = await generateSitemap();
return NextResponse.json(sitemap, {
headers: { "Cache-Control": "no-cache" },
headers: {"Cache-Control": "no-cache"},
});
} catch (error) {
console.error("Failed to generate sitemap:", error);
return NextResponse.json(
{ error: "Failed to generate sitemap" },
{ status: 500 },
{error: "Failed to generate sitemap"},
{status: 500},
);
}
}

View File

@@ -1,4 +1,4 @@
import type {GetServerSideProps} from "next";
import {NextResponse} from "next/server";
interface SitemapRoute {
url: string;
@@ -16,7 +16,9 @@ interface SitemapRoute {
const baseUrl = "https://dki.one";
export const getServerSideProps: GetServerSideProps = async () => {
export async function GET() {
console.log("Generating sitemap...");
const staticRoutes: SitemapRoute[] = [
{url: `${baseUrl}/`, lastModified: new Date().toISOString(), changeFrequency: "weekly", priority: 1.0},
{
@@ -36,28 +38,24 @@ export const getServerSideProps: GetServerSideProps = async () => {
try {
const response = await fetch(`${baseUrl}/api/sitemap`);
if (!response.ok) {
console.error(`Failed to fetch dynamic sitemap: ${response.statusText}`);
console.error(`Failed to fetch dynamic routes: ${response.statusText}`);
}
const dynamicRoutes = (await response.json()) as SitemapRoute[];
const sitemap = [...staticRoutes, ...dynamicRoutes];
return {
props: {
sitemap: [...staticRoutes, ...dynamicRoutes],
},
};
return new NextResponse(generateXml(sitemap), {
headers: {"Content-Type": "application/xml"},
});
} catch (error) {
console.error("Failed to fetch dynamic sitemap, using fallback:", error);
return {
props: {
sitemap: staticRoutes,
},
};
console.error("Error fetching dynamic routes, using fallback:", error);
return new NextResponse(generateXml(staticRoutes), {
headers: {"Content-Type": "application/xml"},
});
}
};
}
const generateXml = (routes: SitemapRoute[]): string => {
function generateXml(routes: SitemapRoute[]): 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>';
@@ -75,17 +73,4 @@ const generateXml = (routes: SitemapRoute[]): string => {
.join("");
return `${xmlHeader}${urlsetOpen}${urlEntries}${urlsetClose}`;
};
const Sitemap = ({sitemap}: { sitemap: SitemapRoute[] }) => {
const xmlSitemap = generateXml(sitemap);
return new Response(xmlSitemap, {
headers: {
"Content-Type": "application/xml",
"Cache-Control": "no-cache",
},
});
};
export default Sitemap;
}

View File

@@ -1,4 +1,4 @@
User-agent: *
Allow: /
Disallow: ['/legal-notice', '/privacy-policy']
Sitemap: https://dki.one/sitemap.xml
Page: https://dki.one/sitemap.xml