From f4e715f2d17f4fa84f6b82095de2a49a6ff34810 Mon Sep 17 00:00:00 2001 From: devops Date: Wed, 14 Oct 2026 19:27:13 +0000 Subject: [PATCH] fix(seo): paginate sitemap blogs outside Next data cache [skip ci] --- frontend/application/app/sitemap.test.ts | 53 ++++++++++++++++++++++++ frontend/application/app/sitemap.ts | 28 +++++++++---- 2 files changed, 72 insertions(+), 9 deletions(-) create mode 100644 frontend/application/app/sitemap.test.ts diff --git a/frontend/application/app/sitemap.test.ts b/frontend/application/app/sitemap.test.ts new file mode 100644 index 00000000..6a20e044 --- /dev/null +++ b/frontend/application/app/sitemap.test.ts @@ -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); + }); +}); diff --git a/frontend/application/app/sitemap.ts b/frontend/application/app/sitemap.ts index 7868a4f8..2ecfa395 100644 --- a/frontend/application/app/sitemap.ts +++ b/frontend/application/app/sitemap.ts @@ -89,20 +89,30 @@ export default async function sitemap(): Promise { 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 = [];