refactor(sitemap): improve sitemap generation and error handling; rename sitemap file
This commit is contained in:
18
README.md
18
README.md
@@ -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
|
## Getting Started
|
||||||
|
|
||||||
@@ -16,9 +17,10 @@ bun dev
|
|||||||
|
|
||||||
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
|
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
|
## 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.
|
- [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.
|
- [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
|
## 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.
|
||||||
|
|||||||
@@ -1,75 +1,60 @@
|
|||||||
import { NextResponse } from "next/server";
|
import {NextResponse} from "next/server";
|
||||||
|
|
||||||
interface Project {
|
interface Project {
|
||||||
slug: string;
|
slug: string;
|
||||||
updated_at?: string; // Optional timestamp for last modification
|
updated_at?: string; // Optional timestamp for last modification
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ProjectsData {
|
interface ProjectsData {
|
||||||
posts: Project[];
|
posts: Project[];
|
||||||
}
|
}
|
||||||
|
|
||||||
interface SitemapRoute {
|
interface SitemapRoute {
|
||||||
url: string;
|
url: string;
|
||||||
lastModified: string;
|
lastModified: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || "https://dki.one";
|
const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || "https://dki.one";
|
||||||
|
|
||||||
const generateSitemap = async (): Promise<SitemapRoute[]> => {
|
const generateSitemap = async (): Promise<SitemapRoute[]> => {
|
||||||
// Static pages
|
try {
|
||||||
const staticRoutes: SitemapRoute[] = [
|
const response = await fetch(`${baseUrl}/api/fetchAllProjects`, {
|
||||||
{ url: `${baseUrl}/`, lastModified: new Date().toISOString() },
|
headers: {"Cache-Control": "no-cache"},
|
||||||
{
|
});
|
||||||
url: `${baseUrl}/privacy-policy`,
|
|
||||||
lastModified: new Date().toISOString(),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
url: `${baseUrl}/legal-notice`,
|
|
||||||
lastModified: new Date().toISOString(),
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
try {
|
if (!response.ok) {
|
||||||
console.log("Fetching project data from API...");
|
console.error(`Failed to fetch projects: ${response.statusText}`);
|
||||||
const response = await fetch(`${baseUrl}/api/fetchAllProjects`, {
|
return [];
|
||||||
headers: { "Cache-Control": "no-cache" },
|
}
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
const projectsData = (await response.json()) as ProjectsData;
|
||||||
console.error(`Failed to fetch projects: ${response.statusText}`);
|
|
||||||
return staticRoutes; // Fallback auf statische Seiten
|
// Dynamische Projekt-Routen generieren
|
||||||
|
const projectRoutes: SitemapRoute[] = projectsData.posts.map((project) => ({
|
||||||
|
url: `${baseUrl}/projects/${project.slug}`,
|
||||||
|
lastModified: project.updated_at
|
||||||
|
? new Date(project.updated_at).toISOString()
|
||||||
|
: new Date().toISOString(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
return [...projectRoutes];
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Failed to generate sitemap:", error);
|
||||||
|
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) => ({
|
|
||||||
url: `${baseUrl}/projects/${project.slug}`,
|
|
||||||
lastModified: project.updated_at
|
|
||||||
? new Date(project.updated_at).toISOString()
|
|
||||||
: new Date().toISOString(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
return [...staticRoutes, ...projectRoutes];
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Failed to generate sitemap:", error);
|
|
||||||
return staticRoutes; // Fallback nur auf statische Seiten
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
try {
|
try {
|
||||||
const sitemap = await generateSitemap();
|
const sitemap = await generateSitemap();
|
||||||
return NextResponse.json(sitemap, {
|
return NextResponse.json(sitemap, {
|
||||||
headers: { "Cache-Control": "no-cache" },
|
headers: {"Cache-Control": "no-cache"},
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to generate sitemap:", error);
|
console.error("Failed to generate sitemap:", error);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: "Failed to generate sitemap" },
|
{error: "Failed to generate sitemap"},
|
||||||
{ status: 500 },
|
{status: 500},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import type {GetServerSideProps} from "next";
|
import {NextResponse} from "next/server";
|
||||||
|
|
||||||
interface SitemapRoute {
|
interface SitemapRoute {
|
||||||
url: string;
|
url: string;
|
||||||
@@ -16,7 +16,9 @@ interface SitemapRoute {
|
|||||||
|
|
||||||
const baseUrl = "https://dki.one";
|
const baseUrl = "https://dki.one";
|
||||||
|
|
||||||
export const getServerSideProps: GetServerSideProps = async () => {
|
export async function GET() {
|
||||||
|
console.log("Generating sitemap...");
|
||||||
|
|
||||||
const staticRoutes: SitemapRoute[] = [
|
const staticRoutes: SitemapRoute[] = [
|
||||||
{url: `${baseUrl}/`, lastModified: new Date().toISOString(), changeFrequency: "weekly", priority: 1.0},
|
{url: `${baseUrl}/`, lastModified: new Date().toISOString(), changeFrequency: "weekly", priority: 1.0},
|
||||||
{
|
{
|
||||||
@@ -36,28 +38,24 @@ export const getServerSideProps: GetServerSideProps = async () => {
|
|||||||
try {
|
try {
|
||||||
const response = await fetch(`${baseUrl}/api/sitemap`);
|
const response = await fetch(`${baseUrl}/api/sitemap`);
|
||||||
if (!response.ok) {
|
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 dynamicRoutes = (await response.json()) as SitemapRoute[];
|
||||||
|
const sitemap = [...staticRoutes, ...dynamicRoutes];
|
||||||
|
|
||||||
return {
|
return new NextResponse(generateXml(sitemap), {
|
||||||
props: {
|
headers: {"Content-Type": "application/xml"},
|
||||||
sitemap: [...staticRoutes, ...dynamicRoutes],
|
});
|
||||||
},
|
|
||||||
};
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to fetch dynamic sitemap, using fallback:", error);
|
console.error("Error fetching dynamic routes, using fallback:", error);
|
||||||
|
return new NextResponse(generateXml(staticRoutes), {
|
||||||
return {
|
headers: {"Content-Type": "application/xml"},
|
||||||
props: {
|
});
|
||||||
sitemap: staticRoutes,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
const generateXml = (routes: SitemapRoute[]): string => {
|
function generateXml(routes: SitemapRoute[]): string {
|
||||||
const xmlHeader = '<?xml version="1.0" encoding="UTF-8"?>';
|
const xmlHeader = '<?xml version="1.0" encoding="UTF-8"?>';
|
||||||
const urlsetOpen = '<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">';
|
const urlsetOpen = '<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">';
|
||||||
const urlsetClose = '</urlset>';
|
const urlsetClose = '</urlset>';
|
||||||
@@ -75,17 +73,4 @@ const generateXml = (routes: SitemapRoute[]): string => {
|
|||||||
.join("");
|
.join("");
|
||||||
|
|
||||||
return `${xmlHeader}${urlsetOpen}${urlEntries}${urlsetClose}`;
|
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;
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
User-agent: *
|
User-agent: *
|
||||||
Allow: /
|
Allow: /
|
||||||
Disallow: ['/legal-notice', '/privacy-policy']
|
Disallow: ['/legal-notice', '/privacy-policy']
|
||||||
Sitemap: https://dki.one/sitemap.xml
|
Page: https://dki.one/sitemap.xml
|
||||||
|
|||||||
Reference in New Issue
Block a user