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:
@@ -1,31 +1,52 @@
|
||||
import {MetadataRoute} from "next";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import type { MetadataRoute } from "next";
|
||||
|
||||
export default function sitemap(): MetadataRoute.Sitemap {
|
||||
const baseUrl = "https://dki.one";
|
||||
interface SitemapRoute {
|
||||
url: string;
|
||||
lastModified: string;
|
||||
changeFrequency?:
|
||||
| "always"
|
||||
| "hourly"
|
||||
| "daily"
|
||||
| "weekly"
|
||||
| "monthly"
|
||||
| "yearly"
|
||||
| "never";
|
||||
priority?: number;
|
||||
}
|
||||
|
||||
// Static pages
|
||||
const staticRoutes = [
|
||||
{url: `${baseUrl}/`, lastModified: new Date().toISOString()},
|
||||
{url: `${baseUrl}/privacy-policy`, lastModified: new Date().toISOString()},
|
||||
];
|
||||
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
|
||||
const baseUrl = "http://localhost:3000";
|
||||
|
||||
// Read project markdown files from the public folder
|
||||
const projectsDirectory = path.join(process.cwd(), "public/projects");
|
||||
let projectRoutes: { url: string; lastModified: string; }[] = [];
|
||||
// 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[];
|
||||
|
||||
if (fs.existsSync(projectsDirectory)) {
|
||||
const projectFiles = fs.readdirSync(projectsDirectory).filter(file => file.endsWith(".md"));
|
||||
return sitemapData.map((route) => {
|
||||
let changeFrequency: SitemapRoute["changeFrequency"];
|
||||
let priority: number;
|
||||
|
||||
projectRoutes = projectFiles.map((file) => {
|
||||
const slug = file.replace(".md", "");
|
||||
return {
|
||||
url: `${baseUrl}/projects/${slug}`,
|
||||
lastModified: new Date().toISOString(),
|
||||
};
|
||||
});
|
||||
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 [...staticRoutes, ...projectRoutes];
|
||||
return {
|
||||
url: route.url,
|
||||
lastModified: route.lastModified,
|
||||
changeFrequency,
|
||||
priority,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user