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.
69 lines
1.7 KiB
TypeScript
69 lines
1.7 KiB
TypeScript
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 },
|
|
);
|
|
}
|
|
}
|