refactor: Update JSON-LD generation in generateProductJsonLd function to separate product and breadcrumb scripts for improved SEO structure
This commit is contained in:
@@ -86,7 +86,7 @@ const generateProductJsonLd = (product, baseUrl, config, categoryInfo = null) =>
|
||||
const priceValidDate = new Date();
|
||||
priceValidDate.setMonth(priceValidDate.getMonth() + 3);
|
||||
|
||||
const jsonLd = {
|
||||
const productJsonLd = {
|
||||
"@context": "https://schema.org/",
|
||||
"@type": "Product",
|
||||
name: product.name,
|
||||
@@ -150,9 +150,14 @@ const generateProductJsonLd = (product, baseUrl, config, categoryInfo = null) =>
|
||||
},
|
||||
};
|
||||
|
||||
// Add breadcrumb if category information is available
|
||||
const productScript = `<script type="application/ld+json">${JSON.stringify(
|
||||
productJsonLd
|
||||
)}</script>`;
|
||||
|
||||
// BreadcrumbList is not a valid property on Product; emit as its own JSON-LD block (WebPage path context).
|
||||
if (categoryInfo && categoryInfo.name && categoryInfo.seoName) {
|
||||
jsonLd.breadcrumb = {
|
||||
const breadcrumbJsonLd = {
|
||||
"@context": "https://schema.org/",
|
||||
"@type": "BreadcrumbList",
|
||||
itemListElement: [
|
||||
{
|
||||
@@ -175,11 +180,13 @@ const generateProductJsonLd = (product, baseUrl, config, categoryInfo = null) =>
|
||||
},
|
||||
],
|
||||
};
|
||||
const breadcrumbScript = `<script type="application/ld+json">${JSON.stringify(
|
||||
breadcrumbJsonLd
|
||||
)}</script>`;
|
||||
return `${productScript}\n${breadcrumbScript}`;
|
||||
}
|
||||
|
||||
return `<script type="application/ld+json">${JSON.stringify(
|
||||
jsonLd
|
||||
)}</script>`;
|
||||
return productScript;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
|
||||
Reference in New Issue
Block a user