feat(seo): enrich Torob product title, subtitle and technical specs for optimal search indexation
This commit is contained in:
1 parent
88060431bb
commit
382ebc0ce6
16 files changed
+151527
-5915
No files matched your search
@@ -6,6 +6,101 @@ import { getMediaUrl } from '../../../../lib/media';
|
||||
export const dynamic = 'force-dynamic';
|
||||
export const revalidate = 3600;
|
||||
|
||||
function buildTorobProductTitle(product: {
|
||||
nameFa?: string | null;
|
||||
name?: string | null;
|
||||
nameEn?: string | null;
|
||||
packageSize?: unknown;
|
||||
unit?: string | null;
|
||||
suitableFor?: string | null;
|
||||
}): string {
|
||||
const nameFa = (product.nameFa || product.name || '').trim();
|
||||
let nameEn = (product.nameEn || '').trim();
|
||||
|
||||
// Clean brand duplicate if nameEn already starts with Canina
|
||||
if (/^Canina\b/i.test(nameEn)) {
|
||||
nameEn = nameEn.replace(/^Canina\s*[-–:]*\s*/i, '').trim();
|
||||
}
|
||||
|
||||
// Package size & unit
|
||||
let sizePart = '';
|
||||
const numSize = Number(product.packageSize);
|
||||
const unit = (product.unit || '').trim();
|
||||
if (numSize && numSize > 0) {
|
||||
sizePart = unit ? `${numSize} ${unit}` : `${numSize}`;
|
||||
}
|
||||
|
||||
// Suitable for pet
|
||||
let petPart = '';
|
||||
if (product.suitableFor) {
|
||||
const pet = product.suitableFor.trim();
|
||||
if (pet === 'هر دو' || pet === 'سگ و گربه' || pet === 'سگ، گربه') {
|
||||
petPart = 'مناسب سگ و گربه';
|
||||
} else if (pet) {
|
||||
petPart = `مناسب ${pet}`;
|
||||
}
|
||||
}
|
||||
|
||||
const parts: string[] = [];
|
||||
if (nameFa) parts.push(nameFa);
|
||||
|
||||
// Brand part
|
||||
if (!nameFa.includes('کنینا') && !nameFa.includes('Canina')) {
|
||||
parts.push('برند کنینا Canina');
|
||||
} else if (!nameFa.includes('Canina')) {
|
||||
parts.push('Canina');
|
||||
}
|
||||
|
||||
if (nameEn && nameEn.toLowerCase() !== nameFa.toLowerCase()) {
|
||||
parts.push(nameEn);
|
||||
}
|
||||
|
||||
if (sizePart && !nameFa.includes(sizePart) && !nameEn.includes(sizePart)) {
|
||||
parts.push(sizePart);
|
||||
}
|
||||
|
||||
if (petPart && !nameFa.includes(petPart)) {
|
||||
parts.push(petPart);
|
||||
}
|
||||
|
||||
return parts.join(' ').replace(/\s+/g, ' ').trim();
|
||||
}
|
||||
|
||||
function buildTorobProductSpec(product: {
|
||||
packageSize?: unknown;
|
||||
unit?: string | null;
|
||||
suitableFor?: string | null;
|
||||
artNo?: string | null;
|
||||
category?: string | { name?: string } | null;
|
||||
}): Record<string, string> {
|
||||
const spec: Record<string, string> = {
|
||||
'برند': 'کنینا (Canina Pharma Germany)',
|
||||
'کشور سازنده': 'آلمان',
|
||||
'ضمانت': 'اصالت ۱۰۰٪ کالای آلمانی و سلامت فیزیکی',
|
||||
};
|
||||
|
||||
if (product.suitableFor) {
|
||||
spec['گونه هدف'] = product.suitableFor === 'هر دو' ? 'سگ و گربه' : product.suitableFor;
|
||||
}
|
||||
|
||||
const numSize = Number(product.packageSize);
|
||||
const unit = (product.unit || '').trim();
|
||||
if (numSize && numSize > 0) {
|
||||
spec['حجم / وزن / تعداد'] = unit ? `${numSize} ${unit}` : `${numSize}`;
|
||||
}
|
||||
|
||||
if (product.artNo) {
|
||||
spec['کد محصول (Art.No)'] = product.artNo;
|
||||
}
|
||||
|
||||
const catName = typeof product.category === 'object' ? product.category?.name : product.category;
|
||||
if (catName) {
|
||||
spec['دسته دارویی و درمانی'] = catName;
|
||||
}
|
||||
|
||||
return spec;
|
||||
}
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
@@ -36,9 +131,19 @@ export async function GET(request: NextRequest) {
|
||||
const slug = product.slug || product.artNo || product.id;
|
||||
const pageUrl = `${baseUrl}/shop/${slug}`;
|
||||
const productId = String(product.artNo || product.id || slug);
|
||||
const nameFa = product.nameFa || product.name || '';
|
||||
const nameEn = product.nameEn || '';
|
||||
const title = nameEn ? `${nameFa} (${nameEn})` : nameFa;
|
||||
|
||||
const title = buildTorobProductTitle(product);
|
||||
const spec = buildTorobProductSpec(product);
|
||||
|
||||
const rawEn = (product.nameEn || '').trim();
|
||||
const numSize = Number(product.packageSize);
|
||||
const unit = (product.unit || '').trim();
|
||||
const sizeStr = numSize && numSize > 0 ? (unit ? `${numSize} ${unit}` : `${numSize}`) : '';
|
||||
let subtitle = rawEn.startsWith('Canina') ? rawEn : `Canina ${rawEn}`.trim();
|
||||
if (sizeStr && !subtitle.includes(sizeStr)) {
|
||||
subtitle = `${subtitle} - ${sizeStr}`;
|
||||
}
|
||||
|
||||
const primaryImg = product.ogImage || product.image || product.imageUrl || '';
|
||||
let imageLink = '';
|
||||
if (primaryImg) {
|
||||
@@ -51,23 +156,27 @@ export async function GET(request: NextRequest) {
|
||||
const isInStock =
|
||||
product.stockStatus !== 'OUT_OF_STOCK' &&
|
||||
product.stockStatus !== 'out_of_stock' &&
|
||||
product.stockStatus !== 'DISCONTINUED' &&
|
||||
product.inStock !== false;
|
||||
|
||||
const price = Number(product.priceValue || product.price || 0);
|
||||
const oldPrice = Number(product.oldPrice || product.priceValue || product.price || 0);
|
||||
|
||||
const catName = typeof product.category === 'object' ? product.category?.name : product.category;
|
||||
|
||||
return {
|
||||
product_id: productId,
|
||||
page_unique_code: productId,
|
||||
title,
|
||||
subtitle: nameEn || product.scientificTagline || '',
|
||||
subtitle,
|
||||
page_url: pageUrl,
|
||||
price,
|
||||
old_price: oldPrice,
|
||||
availability: isInStock ? 'instock' : 'outofstock',
|
||||
image_link: imageLink,
|
||||
guarantee: 'ضمانت اصالت ۱۰۰٪ کمپانی Canina آلمان',
|
||||
category_name: product.category || 'مکمل حیوانات خانگی',
|
||||
guarantee: 'ضمانت اصالت ۱۰۰٪ کمپانی Canina آلمان و اصالت کالا',
|
||||
category_name: catName || 'مکمل حیوانات خانگی',
|
||||
spec,
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user