import { NextResponse } from 'next/server'; export const dynamic = 'force-dynamic'; export const revalidate = 86400; // Revalidate daily type SitemapWikiTerm = { key: string; term: string; definition?: string; updatedAt?: string; createdAt?: string; }; export async function GET() { const baseUrl = process.env.NEXT_PUBLIC_SITE_URL || 'https://canina.ir'; const apiUrl = process.env.INTERNAL_API_URL || process.env.NEXT_PUBLIC_API_URL || 'http://localhost:4001/api'; const normalizedApiUrl = apiUrl.replace(/\/$/, ''); const apiBase = normalizedApiUrl.endsWith('/api') ? normalizedApiUrl : `${normalizedApiUrl}/api`; const wikiRoutes: SitemapWikiTerm[] = []; try { let page = 1; let lastPage = 1; do { const res = await fetch(`${apiBase}/wiki?page=${page}&limit=50`, { cache: 'no-store' }); if (!res.ok) throw new Error(`Wiki sitemap request failed: ${res.status}`); const data = await res.json(); const wikiList: SitemapWikiTerm[] = data.data || []; if (!wikiList.length) break; wikiRoutes.push(...wikiList); lastPage = Number(data.meta?.lastPage) || 1; page += 1; } while (page <= lastPage); } catch (err) { console.error('Failed to fetch wiki terms for sitemap-wiki.xml:', err); } const urlsXml = wikiRoutes .map((item) => { const loc = `${baseUrl}/wiki/${encodeURIComponent(item.key)}`; const lastmod = item.updatedAt ? new Date(item.updatedAt).toISOString() : item.createdAt ? new Date(item.createdAt).toISOString() : new Date().toISOString(); return ` \n ${loc}\n ${lastmod}\n weekly\n 0.8\n `; }) .join('\n'); const xmlContent = `\n\n${urlsXml}\n`; return new NextResponse(xmlContent, { status: 200, headers: { 'Content-Type': 'application/xml; charset=utf-8', 'Cache-Control': 'public, max-age=86400, s-maxage=86400, stale-while-revalidate=604800', }, }); }