Repository navigation
Expand file tree
/
Copy pathrender.js
More file actions
125 lines (112 loc) · 4.7 KB
/
Copy pathrender.js
File metadata and controls
125 lines (112 loc) · 4.7 KB
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
const fs = require('fs');
const path = require('path');
const Handlebars = require('handlebars');
const {
findDescription,
prepareBaseTemplate,
getPrimaryImage,
generatePriceString,
getImageList,
sanitize,
} = require('../renderUtils');
const { generateLdJson } = require('./ldJson');
const { requestSaaS, getProductUrl } = require('../utils');
const { ProductQuery, ProductByUrlKeyQuery } = require('../queries');
const productTemplateCache = {};
// according to https://www.aem.live/developer/markup-reference
function toTemplateProductData(baseProduct) {
const primaryImage = getPrimaryImage(baseProduct)?.url;
return {
name: sanitize(baseProduct.name, 'inline'),
description: sanitize(baseProduct.description, 'all'),
externalId: sanitize(baseProduct.externalId, 'no'),
sku: sanitize(baseProduct.sku, 'no'),
__typename: sanitize(baseProduct.__typename, 'no'),
metaDescription: findDescription(baseProduct),
metaKeyword: sanitize(baseProduct.metaKeyword, 'no'),
metaTitle: sanitize(baseProduct.metaTitle, 'no') || sanitize(baseProduct.name, 'no') || 'Product Details',
metaImage: primaryImage,
primaryImage: primaryImage,
options: baseProduct.options ? [...baseProduct.options] : null,
hasImages: baseProduct.images?.length > 0,
imageList: getImageList(primaryImage, baseProduct.images),
priceString: generatePriceString(baseProduct),
};
}
async function generateProductHtml(sku, urlKey, context) {
if (!sku && !urlKey) {
const error = new Error('Either sku or urlKey must be provided');
error.statusCode = 404;
throw error;
}
const logger = context.logger;
let baseProduct;
if (sku) {
const baseProductData = await requestSaaS(ProductQuery, 'ProductQuery', { sku }, context);
if (!baseProductData?.data?.products || baseProductData?.data?.products?.length === 0) {
const error = new Error('Product not found');
error.statusCode = 404;
throw error;
}
baseProduct = baseProductData.data.products[0];
} else if (urlKey) {
const baseProductData = await requestSaaS(ProductByUrlKeyQuery, 'ProductByUrlKey', { urlKey }, context);
if (!baseProductData?.data?.productSearch || baseProductData?.data?.productSearch?.items?.length === 0) {
const error = new Error('Product not found');
error.statusCode = 404;
throw error;
}
baseProduct = baseProductData.data.productSearch.items[0].productView;
}
logger.debug('Retrieved base product', JSON.stringify(baseProduct, null, 4));
if (baseProduct.options && baseProduct.options.length > 0) {
baseProduct.options = baseProduct.options.map((option) => {
const baseUrl = getProductUrl(baseProduct, context);
if (Array.isArray(option.values)) {
option.values = option.values.map((value) => ({
title: sanitize(value.title, 'inline'),
url: baseUrl.toLowerCase() + '?optionsUIDs=' + value.id,
}));
option.values.sort((a, b) => a.title.localeCompare(b.title));
}
return {
title: sanitize(option.title, 'inline'),
id: sanitize(option.id, 'no'),
required: sanitize(String(option.required), 'no'),
values: option.values,
};
});
}
// Assign meta tag data for template
const templateProductData = toTemplateProductData(baseProduct);
// Generate LD-JSON
const ldJson = await generateLdJson(baseProduct, context);
// Load the Handlebars template
const [pageHbs, headHbs, productDetailsHbs] = ['page', 'head', 'product-details'].map((template) =>
fs.readFileSync(path.join(__dirname, 'templates', `${template}.hbs`), 'utf8'),
);
const pageTemplate = Handlebars.compile(pageHbs);
Handlebars.registerPartial('head', headHbs);
Handlebars.registerPartial('product-details', productDetailsHbs);
// Retrieve default product page as template
const blocksToReplace = ['product-details'];
const localeKey = context.locale || 'default';
if (context.productsTemplate) {
const productsTemplateURL = context.productsTemplate.replace(/\s+/g, '').replace('{locale}', localeKey);
if (!productTemplateCache[localeKey]) productTemplateCache[localeKey] = {};
if (!productTemplateCache[localeKey].baseTemplate)
productTemplateCache[localeKey].baseTemplate = prepareBaseTemplate(productsTemplateURL, blocksToReplace, context);
const baseTemplate = await productTemplateCache[localeKey].baseTemplate;
Handlebars.registerPartial('content', baseTemplate);
} else {
// Use product details block as sole content if no products template is defined
Handlebars.registerPartial('content', `<div>${productDetailsHbs}</div>`);
}
return pageTemplate({
...templateProductData,
ldJson,
});
}
module.exports = {
generateProductHtml,
};