fix: improve project data handling and sitemap generation

Refactor project data parsing to ensure type safety by casting 
the project data as a string. Enhance the sitemap generation 
by fetching data from a dynamic API route, allowing for 
more accurate and up-to-date sitemap entries. Remove unused 
project markdown files to clean up the project structure. 
These changes improve code reliability and maintainability.
This commit is contained in:
2025-02-12 17:18:22 +01:00
parent 28f6bb74b8
commit b93226ad6f
10 changed files with 146 additions and 317 deletions

68
app/api/sitemap/route.tsx Normal file
View File

@@ -0,0 +1,68 @@
import { NextResponse } from "next/server";
interface Project {
slug: string;
}
interface ProjectsData {
posts: Project[];
}
interface SitemapRoute {
url: string;
lastModified: string;
}
const baseUrl = "http://localhost:3000";
const generateSitemap = async (): Promise<SitemapRoute[]> => {
try {
// 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(),
},
];
// Fetch project data from your API
console.log("Fetching project data from API...");
const response = await fetch(`${baseUrl}/api/fetchAllProjects`);
if (!response.ok) {
throw new Error(
`Failed to fetch projects from Ghost: ${response.statusText}`,
);
}
const projectsData = (await response.json()) as ProjectsData;
console.log("Fetched project data:", projectsData);
// Generate dynamic routes for projects
const projectRoutes: SitemapRoute[] = projectsData.posts.map((project) => ({
url: `${baseUrl}/projects/${project.slug}`,
lastModified: new Date().toISOString(),
}));
return [...staticRoutes, ...projectRoutes];
} catch (error) {
console.error("Error generating sitemap:", error);
throw error;
}
};
export async function GET() {
try {
const sitemap = await generateSitemap();
return NextResponse.json(sitemap);
} catch (error) {
console.error("Failed to generate sitemap:", error);
return NextResponse.json(
{ error: "Failed to generate sitemap" },
{ status: 500 },
);
}
}

View File

@@ -1,29 +0,0 @@
// app/api/stats/route.tsx
import {NextResponse} from "next/server";
const stats = {
views: 0,
projectsViewed: {} as { [key: string]: number },
};
export async function GET() {
return NextResponse.json(stats);
}
export async function POST(request: Request) {
const {type, projectId} = await request.json();
if (type === "page_view") {
stats.views += 1;
}
if (type === "project_view" && projectId) {
if (stats.projectsViewed[projectId]) {
stats.projectsViewed[projectId] += 1;
} else {
stats.projectsViewed[projectId] = 1;
}
}
return NextResponse.json({message: "Stats updated", stats});
}