Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 19x 19x 1x 1x 1x 1x 1x 1x 1x 12x 12x 11x 11x 11x 11x 11x 11x 11x 1x 1x 1x 1x 86x 86x 86x 86x 86x 1x 1x 1x 1x 8x 8x 7x 7x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x | /**
* Metadata Generators
* Functions to generate Next.js Metadata objects for different page types
*/
import type { Metadata } from 'next';
import {
SITE_CONFIG,
DEFAULT_METADATA,
PAGE_METADATA,
SEO_LIMITS,
} from './constants';
import type {
SEOProduct,
SEOCategory,
MetadataGeneratorOptions,
} from './types';
/**
* Truncate text to a maximum length, adding ellipsis if needed
*/
function truncateText(text: string, maxLength: number): string {
if (text.length <= maxLength) return text;
return text.slice(0, maxLength - 3).trim() + '...';
}
/**
* Clean and format description text
* Returns empty string if no text provided (to allow fallback handling)
*/
function cleanDescription(text: string | null | undefined): string {
if (!text) return '';
// Remove HTML tags and extra whitespace
const cleaned = text
.replace(/<[^>]*>/g, '')
.replace(/\s+/g, ' ')
.trim();
return truncateText(cleaned, SEO_LIMITS.descriptionMaxLength);
}
/**
* Get absolute URL from relative path
*/
export function getAbsoluteUrl(path: string): string {
if (path.startsWith('http')) return path;
const cleanPath = path.startsWith('/') ? path : `/${path}`;
return `${SITE_CONFIG.url}${cleanPath}`;
}
/**
* Get product image URL
*/
function getProductImageUrl(product: SEOProduct): string {
if (product.images && product.images.length > 0) {
return getAbsoluteUrl(product.images[0].image);
}
return getAbsoluteUrl(SITE_CONFIG.defaultImage);
}
/**
* Generate metadata for product pages
*/
export function generateProductMetadata(
product: SEOProduct,
options?: MetadataGeneratorOptions
): Metadata {
const title = truncateText(product.title, SEO_LIMITS.titleMaxLength);
const cleanedDescription = cleanDescription(product.description);
// Always ensure a description is present - never return empty
const description = cleanedDescription.length > 0
? cleanedDescription
: `Shop ${product.title} at ${SITE_CONFIG.name}. Premium event supplies for all your special occasions.`;
const imageUrl = getProductImageUrl(product);
const productUrl = getAbsoluteUrl(`/product/${product.id}`);
const price = product.discountedPrice || product.price;
return {
title,
description,
keywords: [
product.title,
product.category?.title,
'event supplies',
'party supplies',
].filter(Boolean) as string[],
openGraph: {
type: 'website',
locale: SITE_CONFIG.locale,
url: productUrl,
siteName: SITE_CONFIG.name,
title: product.title,
description,
images: [
{
url: imageUrl,
width: 1200,
height: 630,
alt: product.title,
},
],
},
twitter: {
card: 'summary_large_image',
site: SITE_CONFIG.twitterHandle,
creator: SITE_CONFIG.twitterHandle,
title: product.title,
description,
images: [imageUrl],
},
alternates: {
canonical: options?.canonical || productUrl,
},
robots: options?.noIndex
? { index: false, follow: options?.noFollow === false }
: DEFAULT_METADATA.robots,
other: {
'product:price:amount': price.toString(),
'product:price:currency': 'USD',
'product:availability': product.stock > 0 ? 'in stock' : 'out of stock',
},
};
}
/**
* Generate metadata for category pages
*/
export function generateCategoryMetadata(
category: SEOCategory,
options?: MetadataGeneratorOptions
): Metadata {
const title = `${category.title} - Shop`;
const description = cleanDescription(category.description) ||
`Browse our ${category.title} collection. Find the perfect ${category.title.toLowerCase()} for your next event or celebration.`;
const imageUrl = category.imageUrl
? getAbsoluteUrl(category.imageUrl)
: getAbsoluteUrl(SITE_CONFIG.defaultImage);
const categoryUrl = getAbsoluteUrl(`/shop?category=${category.slug}`);
return {
title,
description,
keywords: [
category.title,
`${category.title} supplies`,
'event supplies',
'party supplies',
],
openGraph: {
type: 'website',
locale: SITE_CONFIG.locale,
url: categoryUrl,
siteName: SITE_CONFIG.name,
title: `${category.title} | ${SITE_CONFIG.name}`,
description,
images: [
{
url: imageUrl,
width: 1200,
height: 630,
alt: category.title,
},
],
},
twitter: {
card: 'summary_large_image',
site: SITE_CONFIG.twitterHandle,
creator: SITE_CONFIG.twitterHandle,
title: `${category.title} | ${SITE_CONFIG.name}`,
description,
images: [imageUrl],
},
alternates: {
canonical: options?.canonical || categoryUrl,
},
robots: options?.noIndex
? { index: false, follow: options?.noFollow === false }
: DEFAULT_METADATA.robots,
};
}
/**
* Generate metadata for static pages
*/
export function generatePageMetadata(
page: keyof typeof PAGE_METADATA,
options?: MetadataGeneratorOptions & { customTitle?: string; customDescription?: string }
): Metadata {
const pageConfig = PAGE_METADATA[page];
const title = options?.customTitle || pageConfig.title;
const description = options?.customDescription || pageConfig.description;
const pageUrl = getAbsoluteUrl(`/${page === 'home' ? '' : page}`);
return {
title,
description,
openGraph: {
type: 'website',
locale: SITE_CONFIG.locale,
url: pageUrl,
siteName: SITE_CONFIG.name,
title: `${title} | ${SITE_CONFIG.name}`,
description,
images: [
{
url: getAbsoluteUrl(SITE_CONFIG.defaultImage),
width: 1200,
height: 630,
alt: SITE_CONFIG.name,
},
],
},
twitter: {
card: 'summary_large_image',
site: SITE_CONFIG.twitterHandle,
creator: SITE_CONFIG.twitterHandle,
title: `${title} | ${SITE_CONFIG.name}`,
description,
images: [getAbsoluteUrl(SITE_CONFIG.defaultImage)],
},
alternates: {
canonical: options?.canonical || pageUrl,
},
robots: options?.noIndex
? { index: false, follow: options?.noFollow === false }
: DEFAULT_METADATA.robots,
};
}
/**
* Generate base metadata for the root layout
*/
export function generateRootMetadata(): Metadata {
return {
...DEFAULT_METADATA,
manifest: '/manifest.json',
icons: {
icon: [
{ url: '/icons/icon-96x96.png', sizes: '96x96', type: 'image/png' },
{ url: '/icon.svg', type: 'image/svg+xml' },
],
apple: [{ url: '/apple-touch-icon.png', sizes: '180x180' }],
},
appleWebApp: {
capable: true,
statusBarStyle: 'default',
title: SITE_CONFIG.name,
},
};
}
/**
* Generate search results metadata (noindex for filtered/paginated pages)
*/
export function generateSearchMetadata(
query: string,
resultsCount: number
): Metadata {
const title = query ? `Search results for "${query}"` : 'Search Products';
const description = query
? `Found ${resultsCount} results for "${query}" at ${SITE_CONFIG.name}`
: `Search our collection of event supplies and party decorations.`;
return {
title,
description,
robots: {
index: false, // Don't index search results pages
follow: true,
},
openGraph: {
type: 'website',
locale: SITE_CONFIG.locale,
siteName: SITE_CONFIG.name,
title,
description,
},
twitter: {
card: 'summary',
site: SITE_CONFIG.twitterHandle,
title,
description,
},
};
}
|