57 lines
1.9 KiB
TypeScript
57 lines
1.9 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { productService } from '../../lib/services/productService';
|
|
import { PRODUCTS } from '../../lib/data/products';
|
|
|
|
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 && res.data.length > 0 ? res.data : PRODUCTS;
|
|
} catch {
|
|
productsList = PRODUCTS;
|
|
}
|
|
|
|
// 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',
|
|
},
|
|
});
|
|
}
|