Deploy Canina / deploy (push) Successful in 2m10s
- SEO-P1-07: extract FAQs and accordion items from blog content and inject JSON-LD FAQPage schema - SEO-P1-10: add dynamic sub-sitemaps /sitemap-blog.xml and /sitemap-wiki.xml and register in robots.ts - SEO-P2-16: host Lalezar font locally as optimized WOFF2 with preload enabled in lib/fonts.ts - Add full regression test suites with 100% pass rate
79 lines
2.9 KiB
TypeScript
79 lines
2.9 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
|
|
export const dynamic = 'force-dynamic';
|
|
export const revalidate = 3600; // Revalidate every hour
|
|
|
|
type SitemapBlog = {
|
|
slug: string;
|
|
noIndex?: boolean;
|
|
status: string;
|
|
updatedAt?: string;
|
|
createdAt?: string;
|
|
featured?: boolean;
|
|
title?: string;
|
|
imageUrl?: string;
|
|
ogImage?: 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 blogRoutes: SitemapBlog[] = [];
|
|
|
|
try {
|
|
let page = 1;
|
|
let lastPage = 1;
|
|
do {
|
|
const res = await fetch(`${apiBase}/blogs?page=${page}&limit=20`, { cache: 'no-store' });
|
|
if (!res.ok) throw new Error(`Blog sitemap request failed: ${res.status}`);
|
|
const data = await res.json();
|
|
const blogsList: SitemapBlog[] = data.data || [];
|
|
if (!blogsList.length) break;
|
|
|
|
const validBlogs = blogsList.filter((b) => !b.noIndex && b.status === 'PUBLISHED');
|
|
blogRoutes.push(...validBlogs);
|
|
|
|
lastPage = Number(data.meta?.lastPage) || 1;
|
|
page += 1;
|
|
} while (page <= lastPage);
|
|
} catch (err) {
|
|
console.error('Failed to fetch blogs for sitemap-blog.xml:', err);
|
|
}
|
|
|
|
const urlsXml = blogRoutes
|
|
.map((blog) => {
|
|
const loc = `${baseUrl}/blog/${encodeURIComponent(blog.slug)}`;
|
|
const lastmod = blog.updatedAt
|
|
? new Date(blog.updatedAt).toISOString()
|
|
: blog.createdAt
|
|
? new Date(blog.createdAt).toISOString()
|
|
: new Date().toISOString();
|
|
const priority = blog.featured ? '0.9' : '0.7';
|
|
const primaryImg = blog.ogImage || blog.imageUrl;
|
|
const title = blog.title || 'مقاله وبلاگ تخصصی کنینا';
|
|
|
|
const imageTag = primaryImg
|
|
? `\n <image:image>\n <image:loc>${
|
|
primaryImg.startsWith('http') ? primaryImg : `${baseUrl}${primaryImg.startsWith('/') ? '' : '/'}${primaryImg}`
|
|
}</image:loc>\n <image:title><![CDATA[${title}]]></image:title>\n </image:image>`
|
|
: '';
|
|
|
|
return ` <url>\n <loc>${loc}</loc>\n <lastmod>${lastmod}</lastmod>\n <changefreq>weekly</changefreq>\n <priority>${priority}</priority>${imageTag}\n </url>`;
|
|
})
|
|
.join('\n');
|
|
|
|
const xmlContent = `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"\n xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">\n${urlsXml}\n</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',
|
|
},
|
|
});
|
|
}
|