feat(sitemap): implement server-side rendering for dynamic sitemap and improve XML generation
This commit is contained in:
109
app/sitemap.tsx
109
app/sitemap.tsx
@@ -1,4 +1,4 @@
|
|||||||
import type { MetadataRoute } from "next";
|
import type {GetServerSideProps} from "next";
|
||||||
|
|
||||||
interface SitemapRoute {
|
interface SitemapRoute {
|
||||||
url: string;
|
url: string;
|
||||||
@@ -14,61 +14,78 @@ interface SitemapRoute {
|
|||||||
priority?: number;
|
priority?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
|
const baseUrl = "https://dki.one";
|
||||||
const baseUrl = "https://dki.one";
|
|
||||||
|
|
||||||
try {
|
export const getServerSideProps: GetServerSideProps = async () => {
|
||||||
const response = await fetch(`${baseUrl}/api/sitemap`);
|
const staticRoutes: SitemapRoute[] = [
|
||||||
if (!response.ok) {
|
{url: `${baseUrl}/`, lastModified: new Date().toISOString(), changeFrequency: "weekly", priority: 1.0},
|
||||||
throw new Error(`Failed to fetch sitemap: ${response.statusText}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
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`,
|
url: `${baseUrl}/privacy-policy`,
|
||||||
lastModified: new Date().toISOString(),
|
lastModified: new Date().toISOString(),
|
||||||
changeFrequency: "yearly",
|
changeFrequency: "yearly",
|
||||||
priority: 0.3,
|
priority: 0.3
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
url: `${baseUrl}/legal-notice`,
|
url: `${baseUrl}/legal-notice`,
|
||||||
lastModified: new Date().toISOString(),
|
lastModified: new Date().toISOString(),
|
||||||
changeFrequency: "yearly",
|
changeFrequency: "yearly",
|
||||||
priority: 0.3,
|
priority: 0.3
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(`${baseUrl}/api/sitemap`);
|
||||||
|
if (!response.ok) {
|
||||||
|
console.error(`Failed to fetch dynamic sitemap: ${response.statusText}`);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
const dynamicRoutes = (await response.json()) as SitemapRoute[];
|
||||||
|
|
||||||
|
return {
|
||||||
|
props: {
|
||||||
|
sitemap: [...staticRoutes, ...dynamicRoutes],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Failed to fetch dynamic sitemap, using fallback:", error);
|
||||||
|
|
||||||
|
return {
|
||||||
|
props: {
|
||||||
|
sitemap: staticRoutes,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const generateXml = (routes: SitemapRoute[]): string => {
|
||||||
|
const xmlHeader = '<?xml version="1.0" encoding="UTF-8"?>';
|
||||||
|
const urlsetOpen = '<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">';
|
||||||
|
const urlsetClose = '</urlset>';
|
||||||
|
|
||||||
|
const urlEntries = routes
|
||||||
|
.map(
|
||||||
|
(route) => `
|
||||||
|
<url>
|
||||||
|
<loc>${route.url}</loc>
|
||||||
|
<lastmod>${route.lastModified}</lastmod>
|
||||||
|
${route.changeFrequency ? `<changefreq>${route.changeFrequency}</changefreq>` : ""}
|
||||||
|
${route.priority ? `<priority>${route.priority}</priority>` : ""}
|
||||||
|
</url>`
|
||||||
|
)
|
||||||
|
.join("");
|
||||||
|
|
||||||
|
return `${xmlHeader}${urlsetOpen}${urlEntries}${urlsetClose}`;
|
||||||
|
};
|
||||||
|
|
||||||
|
const Sitemap = ({sitemap}: { sitemap: SitemapRoute[] }) => {
|
||||||
|
const xmlSitemap = generateXml(sitemap);
|
||||||
|
|
||||||
|
return new Response(xmlSitemap, {
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/xml",
|
||||||
|
"Cache-Control": "no-cache",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Sitemap;
|
||||||
Reference in New Issue
Block a user