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.
53 lines
1.3 KiB
TypeScript
53 lines
1.3 KiB
TypeScript
import type { MetadataRoute } from "next";
|
|
|
|
interface SitemapRoute {
|
|
url: string;
|
|
lastModified: string;
|
|
changeFrequency?:
|
|
| "always"
|
|
| "hourly"
|
|
| "daily"
|
|
| "weekly"
|
|
| "monthly"
|
|
| "yearly"
|
|
| "never";
|
|
priority?: number;
|
|
}
|
|
|
|
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
|
|
const baseUrl = "http://localhost:3000";
|
|
|
|
// Fetch the sitemap data from the dynamic API route
|
|
const response = await fetch(`${baseUrl}/api/sitemap`);
|
|
if (!response.ok) {
|
|
throw new Error(`Failed to fetch sitemap: ${response.statusText}`);
|
|
}
|
|
const sitemapData = (await response.json()) as SitemapRoute[];
|
|
|
|
return sitemapData.map((route) => {
|
|
let changeFrequency: SitemapRoute["changeFrequency"];
|
|
let priority: number;
|
|
|
|
if (route.url === `${baseUrl}/`) {
|
|
changeFrequency = "weekly";
|
|
priority = 1.0;
|
|
} else if (route.url.startsWith(`${baseUrl}/projects`)) {
|
|
changeFrequency = "monthly";
|
|
priority = 0.8;
|
|
} else if (route.url.startsWith(`${baseUrl}/Blog`)) {
|
|
changeFrequency = "weekly";
|
|
priority = 0.6;
|
|
} else {
|
|
changeFrequency = "monthly";
|
|
priority = 0.5;
|
|
}
|
|
|
|
return {
|
|
url: route.url,
|
|
lastModified: route.lastModified,
|
|
changeFrequency,
|
|
priority,
|
|
};
|
|
});
|
|
}
|