fix(seo): prevent duplicate brand affixes in page titles and add checkout success metadata
Deploy Canina / deploy (push) Successful in 2m30s
E2E Playwright Tests / Run Full E2E & Security Suites (push) Failing after 0s

This commit is contained in:
parsa aghaei committed 2026-09-05 19:43:28 +03:30
1 parent c57f775cb4
commit f81aabb479
15 files changed
+9682 -9548

No files matched your search

@@ -1,4 +1,14 @@
import type { Metadata } from 'next';
import OrderSuccess from "../../../../components/OrderSuccess";
import { getPageMetadata } from "../../../../lib/seo";
export async function generateMetadata(): Promise<Metadata> {
return getPageMetadata('checkout_success', {
fallbackTitle: 'سفارش با موفقیت ثبت شد',
fallbackDesc: 'رسید پرداخت و جزئیات ثبت سفارش در کنینا ایران.',
noIndex: true,
});
}
export default async function SuccessPage({ params }: { params: Promise<{ id: string }> }) {
const { id } = await params;
@@ -21,8 +21,9 @@ export async function generateMetadata(
const nameFa = product.nameFa || product.name;
const nameEn = product.nameEn ? ` (${product.nameEn})` : '';
const rawTitle = `${nameFa}${nameEn}`;
const title = product.metaTitle || formatPageTitle(rawTitle, config.brandNameFa, config.titleSeparator, config.titlePosition, false);
const rawTitle = product.metaTitle || `${nameFa}${nameEn}`;
const title = formatPageTitle(rawTitle, config.brandNameFa, config.titleSeparator, config.titlePosition, false);
const ogTitle = `${title} ${config.titleSeparator} ${config.brandNameFa}`;
const description = product.metaDescription || (product.shortDescription || product.description || '').substring(0, 155).trim() || config.defaultMetaDescription;
const rawKeywords = product.keywords;
@@ -71,7 +72,7 @@ export async function generateMetadata(
},
},
openGraph: {
title,
title: ogTitle,
description,
images: galleryImages,
url: canonicalUrl,
+4 -4
View File
@@ -14,10 +14,10 @@ export async function generateMetadata({
const canonicalPath = category ? `/shop?category=${encodeURIComponent(category)}` : '/shop';
const categoryTitles: Record<string, string> = {
'joints': 'مکمل‌های مفاصل و استخوان سگ و گربه | کنینا ایران',
'immune': 'مکمل‌های تقویت سیستم ایمنی و گوارش پت | کنینا ایران',
'energy': 'ویتامین‌ها و انرژی‌بخش‌های درمانی سگ و گربه | کنینا ایران',
'special-care': 'محصولات مراقبت ویژه پوست، مو و دندان پت | کنینا ایران',
'joints': 'مکمل‌های مفاصل و استخوان سگ و گربه',
'immune': 'مکمل‌های تقویت سیستم ایمنی و گوارش پت',
'energy': 'ویتامین‌ها و انرژی‌بخش‌های درمانی سگ و گربه',
'special-care': 'محصولات مراقبت ویژه پوست، مو و دندان پت',
};
const fallbackTitle = category && categoryTitles[category]
+55 -23
View File
@@ -172,11 +172,50 @@ export async function getSeoConfig(): Promise<SeoConfig> {
/**
* Uniform Page Title Formatter according to Store Brand Configuration
*/
/**
* Clean and strip any repeated brand affixes from a title.
* Removes occurrences of brand names (Fa / En) and separators so the template or title can format cleanly.
*/
export function cleanPageTitle(rawTitle: string, brandName = 'کنینا ایران'): string {
if (!rawTitle || !rawTitle.trim()) {
return '';
}
let clean = rawTitle.trim();
// Known brand aliases to strip to avoid repetitive keyword stuffing
const brandKeywords = [
brandName,
'کنینا ایران',
'فروشگاه کنینا ایران',
'فروشگاه کنینا',
'کنینا',
'Canina Iran',
'Canina',
];
for (const b of brandKeywords) {
if (!b) continue;
const escaped = b.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const regex = new RegExp(`(\\s*[-|•/]\\s*${escaped}|${escaped}\\s*[-|•/]\\s*)`, 'gi');
clean = clean.replace(regex, ' ').trim();
}
// Remove dangling separators at beginning or end
clean = clean.replace(/^[-|•/\s]+|[-|•/\s]+$/g, '').trim();
return clean;
}
/**
* Uniform Page Title Formatter according to Store Brand Configuration
* For Next.js subpages, returns a clean title so layout.tsx template can affix brand name cleanly without repetition.
*/
export function formatPageTitle(
rawTitle: string,
brandName = 'کنینا ایران',
separator = '|',
position: 'suffix' | 'prefix' | 'none' = 'suffix',
_separator = '|',
_position: 'suffix' | 'prefix' | 'none' = 'suffix',
isHomePage = false
): string {
if (!rawTitle || !rawTitle.trim()) {
@@ -185,28 +224,17 @@ export function formatPageTitle(
const clean = rawTitle.trim();
// If this is the home page and has a custom full title configured, return it directly
// If this is the home page, return the full configured home title as-is
if (isHomePage) {
return clean;
}
// Check if the title already explicitly contains the brand name
const escapedBrand = brandName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const brandRegex = new RegExp(`(\\s*[-|•]\\s*${escapedBrand}|${escapedBrand}\\s*[-|•]\\s*)`, 'gi');
// Clean any brand suffixes/prefixes already embedded in rawTitle
const baseTitle = cleanPageTitle(clean, brandName);
// Strip existing repeated brand affix if present so we can format cleanly
const baseTitle = clean.replace(brandRegex, '').trim();
if (position === 'none') {
return baseTitle || clean;
}
if (position === 'prefix') {
return `${brandName} ${separator} ${baseTitle || clean}`;
}
// Default: 'suffix' -> "{PageTitle} {separator} {BrandName}"
return `${baseTitle || clean} ${separator} ${brandName}`;
// Return baseTitle directly so layout.tsx's `title.template` handles affixing brand name exactly once!
// If baseTitle becomes empty (e.g. rawTitle was just "کنینا ایران"), fallback to rawTitle or brandName
return baseTitle || clean || brandName;
}
export interface PageMetadataOptions {
@@ -264,15 +292,19 @@ export async function getPageMetadata(
process.env.NEXT_PUBLIC_SITE_URL.includes('test') ||
process.env.NEXT_PUBLIC_SITE_URL.includes('dev')));
const ogTitle = isHome
? title
: (titlePosition === 'prefix' ? `${brandNameFa} ${titleSeparator} ${title}` : `${title} ${titleSeparator} ${brandNameFa}`);
return {
title,
title: isHome ? { absolute: title } : title,
description,
keywords,
alternates: {
canonical: canonicalUrl,
},
openGraph: {
title,
title: ogTitle,
description,
url: canonicalUrl,
siteName: brandNameFa,
@@ -283,13 +315,13 @@ export async function getPageMetadata(
url: image,
width: 1200,
height: 630,
alt: title,
alt: ogTitle,
},
],
},
twitter: {
card: 'summary_large_image',
title,
title: ogTitle,
description,
images: [image],
},