fix(seo): paginate sitemap blogs outside Next data cache [skip ci]
This commit is contained in:
1 parent
33afd7f45a
commit
f4e715f2d1
2 files changed
+72
-9
No files matched your search
@@ -0,0 +1,53 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import sitemap from './sitemap';
|
||||
|
||||
vi.mock('../lib/services/productService', () => ({
|
||||
productService: {
|
||||
getProducts: vi.fn().mockResolvedValue({ data: [] }),
|
||||
getActiveFilters: vi.fn().mockResolvedValue({ categories: [] }),
|
||||
},
|
||||
}));
|
||||
|
||||
afterEach(() => { vi.unstubAllGlobals(); vi.unstubAllEnvs(); });
|
||||
|
||||
describe('sitemap blog retrieval', () => {
|
||||
it('retrieves every page without putting full blog content in the Next data cache', async () => {
|
||||
vi.stubEnv('INTERNAL_API_URL', 'http://backend:3000/api/');
|
||||
vi.stubEnv('NEXT_PUBLIC_SITE_URL', 'https://stage.canina.ir');
|
||||
const fetchMock = vi.fn()
|
||||
.mockResolvedValueOnce({ ok: true, json: async () => ({ data: [
|
||||
{ slug: 'first', status: 'PUBLISHED', updatedAt: '2026-10-01', featured: true },
|
||||
{ slug: 'hidden', status: 'PUBLISHED', noIndex: true },
|
||||
{ slug: 'draft', status: 'DRAFT' },
|
||||
], meta: { lastPage: 2 } }) })
|
||||
.mockResolvedValueOnce({ ok: true, json: async () => ({ data: [
|
||||
{ slug: 'last', status: 'PUBLISHED' },
|
||||
], meta: { lastPage: 2 } }) });
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
const routes = await sitemap();
|
||||
expect(fetchMock.mock.calls).toEqual([
|
||||
['http://backend:3000/api/blogs?page=1&limit=10', { cache: 'no-store' }],
|
||||
['http://backend:3000/api/blogs?page=2&limit=10', { cache: 'no-store' }],
|
||||
]);
|
||||
const blogs = routes.filter(r => r.url.includes('/blog/'));
|
||||
expect(blogs.map(r => r.url)).toEqual(['https://stage.canina.ir/blog/first', 'https://stage.canina.ir/blog/last']);
|
||||
expect(blogs[0].priority).toBe(0.9);
|
||||
expect(blogs[0].lastModified).toEqual(new Date('2026-10-01'));
|
||||
});
|
||||
|
||||
it('terminates on an empty page even when pagination metadata is stale', async () => {
|
||||
const fetchMock = vi.fn().mockResolvedValue({ ok: true, json: async () => ({ data: [], meta: { lastPage: 999 } }) });
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
expect((await sitemap()).filter(r => r.url.includes('/blog/'))).toEqual([]);
|
||||
expect(fetchMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('falls back without publishing a partial blog sitemap when a later page fails', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn()
|
||||
.mockResolvedValueOnce({ ok: true, json: async () => ({ data: [{ slug: 'first', status: 'PUBLISHED' }], meta: { lastPage: 2 } }) })
|
||||
.mockResolvedValueOnce({ ok: false, status: 503 }));
|
||||
const routes = await sitemap();
|
||||
expect(routes.filter(r => r.url.includes('/blog/'))).toEqual([]);
|
||||
expect(routes.some(r => r.url.endsWith('/shop'))).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -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 = [];
|
||||
|
||||
Reference in new issue
Block a user