feat(sitemap): enhance sitemap generation and error handling
Add optional `updated_at` field to the Project interface for tracking last modification. Update base URL to use an environment variable for better configurability. Improve error handling during sitemap data fetching by logging errors and returning static routes as a fallback. Refactor change frequency and priority logic for clarity and maintainability.
This commit is contained in:
@@ -17,36 +17,58 @@ interface SitemapRoute {
|
||||
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
|
||||
const baseUrl = "https://dki.one";
|
||||
|
||||
// Fetch the sitemap data from the dynamic API routes
|
||||
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;
|
||||
try {
|
||||
const response = await fetch(`${baseUrl}/api/sitemap`);
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch sitemap: ${response.statusText}`);
|
||||
}
|
||||
|
||||
return {
|
||||
url: route.url,
|
||||
lastModified: route.lastModified,
|
||||
changeFrequency,
|
||||
priority,
|
||||
};
|
||||
});
|
||||
const sitemapData = (await response.json()) as SitemapRoute[];
|
||||
|
||||
return sitemapData.map((route) => {
|
||||
const config: Record<
|
||||
string,
|
||||
{ changeFrequency: SitemapRoute["changeFrequency"]; priority: number }
|
||||
> = {
|
||||
[`${baseUrl}/`]: { changeFrequency: "weekly", priority: 1.0 },
|
||||
};
|
||||
|
||||
if (route.url.startsWith(`${baseUrl}/projects`)) {
|
||||
config[route.url] = { changeFrequency: "monthly", priority: 0.8 };
|
||||
} else if (route.url.startsWith(`${baseUrl}/blog`)) {
|
||||
config[route.url] = { changeFrequency: "weekly", priority: 0.6 };
|
||||
} else {
|
||||
config[route.url] = { changeFrequency: "monthly", priority: 0.5 };
|
||||
}
|
||||
|
||||
return {
|
||||
url: route.url,
|
||||
lastModified: route.lastModified,
|
||||
...config[route.url],
|
||||
};
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Failed to fetch dynamic sitemap, using fallback:", error);
|
||||
|
||||
return [
|
||||
{
|
||||
url: `${baseUrl}/`,
|
||||
lastModified: new Date().toISOString(),
|
||||
changeFrequency: "weekly",
|
||||
priority: 1.0,
|
||||
},
|
||||
{
|
||||
url: `${baseUrl}/privacy-policy`,
|
||||
lastModified: new Date().toISOString(),
|
||||
changeFrequency: "yearly",
|
||||
priority: 0.3,
|
||||
},
|
||||
{
|
||||
url: `${baseUrl}/legal-notice`,
|
||||
lastModified: new Date().toISOString(),
|
||||
changeFrequency: "yearly",
|
||||
priority: 0.3,
|
||||
},
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user