Files
canina/frontend/application/app/sitemap-products.xml/route.ts
T
devops 5e0541c739
Deploy Canina / deploy (push) Successful in 2m24s
E2E Playwright Tests / Run Full E2E & Security Suites (push) Failing after 7s
fix(frontend): remove static fallback data and mock fallbacks across storefront
2026-10-13 16:42:46 +00:00

57 lines
1.9 KiB
TypeScript

import { NextResponse } from 'next/server';
import { productService } from '../../lib/services/productService';
export const dynamic = 'force-dynamic';
export const revalidate = 3600; // Revalidate every hour
export async function GET() {
const baseUrl = process.env.NEXT_PUBLIC_SITE_URL || 'https://canina.ir';
let productsList: any[] = [];
try {
const res = await productService.getProducts({ limit: 999 });
productsList = res.data || [];
} catch (err) {
console.error("Failed to fetch products for sitemap-products.xml:", err);
productsList = [];
}
// Filter out noIndex products
const indexableProducts = productsList.filter(p => !p.noIndex);
const urlsXml = indexableProducts.map(product => {
const slug = product.slug || product.artNo || product.id;
const loc = `${baseUrl}/shop/${slug}`;
const lastmod = product.updatedAt ? new Date(product.updatedAt).toISOString() : new Date().toISOString();
const primaryImg = product.ogImage || product.image;
const title = product.nameFa || product.name || 'مکمل درمانی کنینا';
const imageTag = primaryImg ? `
<image:image>
<image:loc>${primaryImg.startsWith('http') ? primaryImg : `${baseUrl}${primaryImg}`}</image:loc>
<image:title><![CDATA[${title}]]></image:title>
</image:image>` : '';
return ` <url>
<loc>${loc}</loc>
<lastmod>${lastmod}</lastmod>
<changefreq>daily</changefreq>
<priority>0.9</priority>${imageTag}
</url>`;
}).join('\n');
const xmlContent = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
${urlsXml}
</urlset>`;
return new NextResponse(xmlContent, {
status: 200,
headers: {
'Content-Type': 'application/xml; charset=utf-8',
'Cache-Control': 'public, max-age=3600, s-maxage=3600, stale-while-revalidate=86400',
},
});
}