71 lines
2.4 KiB
TypeScript
71 lines
2.4 KiB
TypeScript
import type { MetadataRoute } from 'next';
|
|
|
|
const SITE_URL = 'https://ifixkart.trionixsolution.com';
|
|
|
|
const API_URL =
|
|
process.env.API_URL ||
|
|
process.env.NEXT_PUBLIC_API_URL ||
|
|
'https://ifixkartbe.trionixsolution.com';
|
|
|
|
// Rebuild the sitemap at most once an hour
|
|
export const revalidate = 3600;
|
|
|
|
const STATIC_ROUTES: MetadataRoute.Sitemap = [
|
|
{ url: `${SITE_URL}/`, changeFrequency: 'daily', priority: 1 },
|
|
{ url: `${SITE_URL}/shop`, changeFrequency: 'daily', priority: 0.9 },
|
|
{ url: `${SITE_URL}/repair-services`, changeFrequency: 'weekly', priority: 0.8 },
|
|
{ url: `${SITE_URL}/about`, changeFrequency: 'monthly', priority: 0.4 },
|
|
{ url: `${SITE_URL}/contact`, changeFrequency: 'monthly', priority: 0.4 },
|
|
{ url: `${SITE_URL}/faqs`, changeFrequency: 'monthly', priority: 0.3 },
|
|
{ url: `${SITE_URL}/blog`, changeFrequency: 'weekly', priority: 0.5 },
|
|
];
|
|
|
|
interface CatalogCard {
|
|
slug: string;
|
|
variant_sku?: string | null;
|
|
variant_url?: string | null;
|
|
}
|
|
|
|
/**
|
|
* Emits one URL per product variant (Sunsky-style):
|
|
* /products/[slug]?variant=[sku]
|
|
* Google indexes query-param variants as long as they are internally linked
|
|
* and each PDP URL carries a self-referential canonical tag.
|
|
*/
|
|
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
|
|
const entries: MetadataRoute.Sitemap = [...STATIC_ROUTES];
|
|
const seen = new Set<string>();
|
|
|
|
try {
|
|
// 40 pages x 100 = up to 4000 variant cards per sitemap build
|
|
for (let page = 1; page <= 40; page++) {
|
|
const res = await fetch(
|
|
`${API_URL}/api/v1/catalog/products?page=${page}&limit=100&expand_variants=true`,
|
|
{ next: { revalidate: 3600, tags: ['products'] } }
|
|
);
|
|
if (!res.ok) break;
|
|
const data = await res.json();
|
|
const products: CatalogCard[] = data?.products || [];
|
|
if (products.length === 0) break;
|
|
|
|
for (const p of products) {
|
|
const path = p.variant_url || `/products/${p.slug}`;
|
|
if (seen.has(path)) continue;
|
|
seen.add(path);
|
|
entries.push({
|
|
url: `${SITE_URL}${path}`,
|
|
changeFrequency: 'weekly',
|
|
priority: p.variant_url ? 0.7 : 0.8,
|
|
});
|
|
}
|
|
|
|
const total = typeof data?.total === 'number' ? data.total : 0;
|
|
if (page * 100 >= total) break;
|
|
}
|
|
} catch (err) {
|
|
// Backend unreachable: sitemap still ships with static routes
|
|
console.error('Sitemap: failed to fetch catalog for variant URLs:', err);
|
|
}
|
|
|
|
return entries;
|
|
}
|