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

+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],
},