UNPKG

@sitecore-jss/sitecore-jss

Version:

This module is provided as a part of Sitecore JavaScript Rendering SDK. It contains the core JSS APIs (layout service) and utilities.

137 lines (136 loc) 5.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CdpHelper = exports.VARIANT_PREFIX = exports.DEFAULT_VARIANT = void 0; exports.getPersonalizedRewrite = getPersonalizedRewrite; exports.getPersonalizedRewriteData = getPersonalizedRewriteData; exports.getGroomedVariantIds = getGroomedVariantIds; exports.normalizePersonalizedRewrite = normalizePersonalizedRewrite; exports.DEFAULT_VARIANT = '_default'; exports.VARIANT_PREFIX = '_variantId_'; /** * Get a personalized rewrite path for given pathname * @param {string} pathname the pathname * @param {string[]} variantIds the variantIds to include in the rewrite * @returns {string} the rewrite path */ function getPersonalizedRewrite(pathname, variantIds) { const path = pathname.startsWith('/') ? pathname : '/' + pathname; return `${variantIds.map((variantId) => `/${exports.VARIANT_PREFIX}${variantId}`).join('')}${path}`; } /** * Get personalize data from the rewrite path * @param {string} pathname the pathname * @returns {PersonalizedRewriteData} the personalize data from the rewrite */ function getPersonalizedRewriteData(pathname) { const segments = pathname.split('/'); const variantIds = []; segments.forEach((segment) => { const result = segment.match(`${exports.VARIANT_PREFIX}(.*$)`); if (result) { variantIds.push(result[1]); } }); return getGroomedVariantIds(variantIds); } /** * Parses a list of variantIds and divides into layout and component variants * @param {string[]} variantIds the list of variant IDs for a page * @returns {PersonalizedRewriteData} object with variant IDs sorted */ function getGroomedVariantIds(variantIds) { const data = { variantId: exports.DEFAULT_VARIANT, componentVariantIds: [], }; variantIds.forEach((variantId) => { var _a; if (variantId.includes('_')) { // Component-level personalization in format "<ComponentID>_<VariantID>" // There can be multiple (_a = data.componentVariantIds) === null || _a === void 0 ? void 0 : _a.push(variantId); } else { // Embedded (page-level) personalization in format "<VariantID>" // There should be only one data.variantId = variantId; } }); return data; } /** * Normalize a personalized rewrite path (remove personalize data) * @param {string} pathname the pathname * @returns {string} the pathname with personalize data removed */ function normalizePersonalizedRewrite(pathname) { if (!pathname.includes(exports.VARIANT_PREFIX)) { return pathname; } let segments = pathname.split('/'); segments = segments.filter((segment) => !segment.includes(exports.VARIANT_PREFIX)); const result = segments.join('/'); // return root path if all segments were personalize data return result ? result : '/'; } /** * Static utility class for Sitecore CDP */ class CdpHelper { /** * Gets the page variant id for CDP in the required format * @param {string} pageId the page id * @param {string} language the language * @param {string} variantId the variant id * @param {string} [scope] the scope value * @returns {string} the formatted page variant id */ static getPageVariantId(pageId, language, variantId, scope) { const formattedPageId = pageId.replace(/[{}-]/g, ''); const formattedLanguage = language.replace('-', '_'); const scopeId = scope ? `${this.normalizeScope(scope)}_` : ''; let formattedVariantId = variantId; if (!variantId || variantId === exports.DEFAULT_VARIANT) { formattedVariantId = 'default'; } return `${scopeId}${formattedPageId}_${formattedLanguage}_${formattedVariantId}`.toLowerCase(); } /** * Gets the friendly id for (page-level) Embedded Personalization in the required format `embedded_[<scope>_]<id>_<lang>` * @param {string} pageId the page id * @param {string} language the language * @param {string} [scope] the scope value * @returns {string} the friendly id */ static getPageFriendlyId(pageId, language, scope) { const formattedPageId = pageId.replace(/[{}-]/g, ''); const formattedLanguage = language.replace('-', '_'); const scopeId = scope ? `${this.normalizeScope(scope)}_` : ''; return `embedded_${scopeId}${formattedPageId}_${formattedLanguage}`.toLowerCase(); } /** * Gets the friendly id for Component A/B Testing in the required format `component_[<scope>_]<pageId>_<componentId>_<language>*` * @param {string} pageId the page id * @param {string} componentId the component id * @param {string} language the language * @param {string} [scope] the scope value * @returns {string} the friendly id */ static getComponentFriendlyId(pageId, componentId, language, scope) { const formattedPageId = pageId.replace(/[{}-]/g, ''); const formattedComponentId = componentId.replace(/[{}-]/g, ''); const formattedLanguage = language.replace('-', '_'); const scopeId = scope ? `${this.normalizeScope(scope)}_` : ''; return `component_${scopeId}${formattedPageId}_${formattedComponentId}_${formattedLanguage}*`.toLowerCase(); } /** * Normalizes the scope from the given string value * Removes all non-alphanumeric characters * @param {string} [scope] the scope value * @returns {string} normalized scope value */ static normalizeScope(scope) { return (scope === null || scope === void 0 ? void 0 : scope.replace(/[^a-zA-Z0-9]+/g, '')) || ''; } } exports.CdpHelper = CdpHelper;