fix(seo): paginate sitemap blogs outside Next data cache [skip ci]

This commit is contained in:
devops committed 2026-10-14 19:27:13 +00:00
1 parent 33afd7f45a
commit f4e715f2d1
2 files changed
+72 -9

No files matched your search

+19 -9
View File
@@ -89,20 +89,30 @@ export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
let blogRoutes: MetadataRoute.Sitemap = [];
try {
const apiUrl = process.env.INTERNAL_API_URL || process.env.NEXT_PUBLIC_API_URL || 'http://localhost:4001/api';
const apiBase = apiUrl.endsWith('/api') ? apiUrl : `${apiUrl.replace(/\/$/, '')}/api`;
const res = await fetch(`${apiBase}/blogs?page=1&limit=999`, { next: { revalidate: 3600 } });
if (res.ok) {
const normalizedApiUrl = apiUrl.replace(/\/$/, '');
const apiBase = normalizedApiUrl.endsWith('/api') ? normalizedApiUrl : `${normalizedApiUrl}/api`;
type SitemapBlog = { slug: string; noIndex?: boolean; status: string; updatedAt?: string; featured?: boolean };
// The public list includes article bodies. Keep requests small and bypass
// Next's 2MB per-entry data cache, even for an unusually large article.
let page = 1;
let lastPage = 1;
do {
const res = await fetch(`${apiBase}/blogs?page=${page}&limit=10`, { cache: 'no-store' });
if (!res.ok) throw new Error(`Blog sitemap request failed: ${res.status}`);
const data = await res.json();
const blogsList = data.data || [];
blogRoutes = blogsList
.filter((b: any) => !b.noIndex && b.status === 'PUBLISHED')
.map((b: any) => ({
const blogsList: SitemapBlog[] = data.data || [];
if (!blogsList.length) break;
blogRoutes.push(...blogsList
.filter((b) => !b.noIndex && b.status === 'PUBLISHED')
.map((b): MetadataRoute.Sitemap[number] => ({
url: `${baseUrl}/blog/${b.slug}`,
lastModified: b.updatedAt ? new Date(b.updatedAt) : new Date(),
changeFrequency: 'weekly',
priority: b.featured ? 0.9 : 0.7,
}));
}
})));
lastPage = Number(data.meta?.lastPage) || 1;
page += 1;
} while (page <= lastPage);
} catch {
// Fallback if backend not reachable at build time
blogRoutes = [];