diff --git a/libs/blocks/fragment/fragment.js b/libs/blocks/fragment/fragment.js index d3a6180da1..b6b13a2a64 100644 --- a/libs/blocks/fragment/fragment.js +++ b/libs/blocks/fragment/fragment.js @@ -95,7 +95,7 @@ export default async function init(a) { let resourcePath = a.href; if (a.href.includes('/federal/')) { - const { getFederatedUrl } = await import('../../utils/federated.js'); + const { getFederatedUrl } = await import('../../utils/utils.js'); resourcePath = getFederatedUrl(a.href); } const resp = await customFetch({ resource: `${resourcePath}.plain.html`, withCacheRules: true }) diff --git a/libs/blocks/global-footer/global-footer.js b/libs/blocks/global-footer/global-footer.js index 3ab25388ae..3bf311d5d1 100644 --- a/libs/blocks/global-footer/global-footer.js +++ b/libs/blocks/global-footer/global-footer.js @@ -6,6 +6,8 @@ import { getConfig, localizeLink, loadStyle, + getFederatedUrl, + getFedsPlaceholderConfig, } from '../../utils/utils.js'; import { @@ -22,8 +24,6 @@ import { isDarkMode, } from '../global-navigation/utilities/utilities.js'; -import { getFederatedUrl, getFedsPlaceholderConfig } from '../../utils/federated.js'; - import { replaceKey } from '../../features/placeholders.js'; const { miloLibs, codeRoot, locale, mep } = getConfig(); diff --git a/libs/blocks/global-navigation/features/breadcrumbs/breadcrumbs.js b/libs/blocks/global-navigation/features/breadcrumbs/breadcrumbs.js index 1f00fb7bcf..1ddd8292cb 100644 --- a/libs/blocks/global-navigation/features/breadcrumbs/breadcrumbs.js +++ b/libs/blocks/global-navigation/features/breadcrumbs/breadcrumbs.js @@ -1,6 +1,5 @@ -import { getMetadata, getConfig } from '../../../../utils/utils.js'; +import { getMetadata, getConfig, getFederatedUrl } from '../../../../utils/utils.js'; import { toFragment, lanaLog } from '../../utilities/utilities.js'; -import { getFederatedUrl } from '../../../../utils/federated.js'; const metadata = { seo: 'breadcrumbs-seo', diff --git a/libs/blocks/global-navigation/features/profile/dropdown.js b/libs/blocks/global-navigation/features/profile/dropdown.js index cdd0d8f9fe..8c47f2083b 100644 --- a/libs/blocks/global-navigation/features/profile/dropdown.js +++ b/libs/blocks/global-navigation/features/profile/dropdown.js @@ -1,7 +1,6 @@ -import { getConfig } from '../../../../utils/utils.js'; +import { getConfig, getFedsPlaceholderConfig } from '../../../../utils/utils.js'; import { toFragment, trigger, closeAllDropdowns, logErrorFor } from '../../utilities/utilities.js'; import { replaceKeyArray } from '../../../../features/placeholders.js'; -import { getFedsPlaceholderConfig } from '../../../../utils/federated.js'; const getLanguage = (ietfLocale) => { if (!ietfLocale.length) return 'en'; diff --git a/libs/blocks/global-navigation/features/search/gnav-search.js b/libs/blocks/global-navigation/features/search/gnav-search.js index d8fb361820..de11a3b007 100644 --- a/libs/blocks/global-navigation/features/search/gnav-search.js +++ b/libs/blocks/global-navigation/features/search/gnav-search.js @@ -8,9 +8,8 @@ import { lanaLog, } from '../../utilities/utilities.js'; import { replaceKeyArray } from '../../../../features/placeholders.js'; -import { getConfig } from '../../../../utils/utils.js'; +import { getConfig, getFedsPlaceholderConfig } from '../../../../utils/utils.js'; import { debounce } from '../../../../utils/action.js'; -import { getFedsPlaceholderConfig } from '../../../../utils/federated.js'; const CONFIG = { suggestions: { diff --git a/libs/blocks/global-navigation/global-navigation.js b/libs/blocks/global-navigation/global-navigation.js index b50084c493..00aac8aace 100644 --- a/libs/blocks/global-navigation/global-navigation.js +++ b/libs/blocks/global-navigation/global-navigation.js @@ -7,6 +7,7 @@ import { decorateLinks, loadScript, getGnavSource, + getFedsPlaceholderConfig, } from '../../utils/utils.js'; import { closeAllDropdowns, @@ -45,7 +46,6 @@ import { branchBannerLoadCheck, getBranchBannerInfo, } from './utilities/utilities.js'; -import { getFedsPlaceholderConfig } from '../../utils/federated.js'; import { replaceKey, replaceKeyArray } from '../../features/placeholders.js'; diff --git a/libs/blocks/global-navigation/utilities/utilities.js b/libs/blocks/global-navigation/utilities/utilities.js index 429369c1bd..c416b5d681 100644 --- a/libs/blocks/global-navigation/utilities/utilities.js +++ b/libs/blocks/global-navigation/utilities/utilities.js @@ -1,8 +1,15 @@ /* eslint import/no-relative-packages: 0 */ import { - getConfig, getMetadata, loadStyle, loadLana, decorateLinks, localizeLink, + getConfig, + getMetadata, + loadStyle, + loadLana, + decorateLinks, + localizeLink, + getFederatedContentRoot, + getFederatedUrl, + getFedsPlaceholderConfig, } from '../../../utils/utils.js'; -import { getFederatedContentRoot, getFederatedUrl, getFedsPlaceholderConfig } from '../../../utils/federated.js'; import { processTrackingLabels } from '../../../martech/attributes.js'; import { replaceText } from '../../../features/placeholders.js'; import { PERSONALIZATION_TAGS } from '../../../features/personalization/personalization.js'; diff --git a/libs/blocks/modal/modal.js b/libs/blocks/modal/modal.js index 0fe6c7d91e..c0b87569e9 100644 --- a/libs/blocks/modal/modal.js +++ b/libs/blocks/modal/modal.js @@ -100,7 +100,7 @@ function getCustomModal(custom, dialog) { async function getPathModal(path, dialog) { let href = path; if (path.includes('/federal/')) { - const { getFederatedUrl } = await import('../../utils/federated.js'); + const { getFederatedUrl } = await import('../../utils/utils.js'); href = getFederatedUrl(path); } const block = createTag('a', { href }); diff --git a/libs/features/georoutingv2/georoutingv2.js b/libs/features/georoutingv2/georoutingv2.js index 7213696f63..de19681b39 100644 --- a/libs/features/georoutingv2/georoutingv2.js +++ b/libs/features/georoutingv2/georoutingv2.js @@ -1,4 +1,4 @@ -import { getFederatedContentRoot } from '../../utils/federated.js'; +import { getFederatedContentRoot } from '../../utils/utils.js'; let config; let createTag; diff --git a/libs/features/personalization/personalization.js b/libs/features/personalization/personalization.js index b26f4c9d8b..a3f8edcf63 100644 --- a/libs/features/personalization/personalization.js +++ b/libs/features/personalization/personalization.js @@ -2,8 +2,14 @@ /* eslint-disable no-underscore-dangle */ /* eslint-disable no-console */ -import { createTag, getConfig, loadLink, loadScript, localizeLink } from '../../utils/utils.js'; -import { getFederatedUrl } from '../../utils/federated.js'; +import { + createTag, + getConfig, + loadLink, + loadScript, + localizeLink, + getFederatedUrl, +} from '../../utils/utils.js'; /* c8 ignore start */ const PHONE_SIZE = window.screen.width < 550 || window.screen.height < 550; diff --git a/libs/features/webapp-prompt/webapp-prompt.js b/libs/features/webapp-prompt/webapp-prompt.js index 517f2a713f..1bc8bb10af 100644 --- a/libs/features/webapp-prompt/webapp-prompt.js +++ b/libs/features/webapp-prompt/webapp-prompt.js @@ -4,9 +4,8 @@ import { lanaLog, toFragment, } from '../../blocks/global-navigation/utilities/utilities.js'; -import { getConfig, decorateSVG } from '../../utils/utils.js'; +import { getConfig, decorateSVG, getFedsPlaceholderConfig } from '../../utils/utils.js'; import { replaceKey, replaceText } from '../placeholders.js'; -import { getFedsPlaceholderConfig } from '../../utils/federated.js'; export const DISMISSAL_CONFIG = { animationCount: 2, diff --git a/libs/utils/decorate.js b/libs/utils/decorate.js index 7cbc051a32..55dd891afd 100644 --- a/libs/utils/decorate.js +++ b/libs/utils/decorate.js @@ -1,5 +1,11 @@ -import { createTag, loadStyle, getConfig, createIntersectionObserver } from './utils.js'; -import { getFederatedContentRoot, getFedsPlaceholderConfig } from './federated.js'; +import { + createTag, + loadStyle, + getConfig, + createIntersectionObserver, + getFederatedContentRoot, + getFedsPlaceholderConfig, +} from './utils.js'; const { miloLibs, codeRoot } = getConfig(); const HIDE_CONTROLS = '_hide-controls'; diff --git a/libs/utils/federated.js b/libs/utils/federated.js index d21f1354a6..15d0d11dee 100644 --- a/libs/utils/federated.js +++ b/libs/utils/federated.js @@ -1,59 +1,3 @@ -import { getConfig, SLD } from './utils.js'; - -let federatedContentRoot; -/* eslint-disable import/prefer-default-export */ -export const getFederatedContentRoot = () => { - const cdnWhitelistedOrigins = [ - 'https://www.adobe.com', - 'https://business.adobe.com', - 'https://blog.adobe.com', - 'https://milo.adobe.com', - 'https://news.adobe.com', - ]; - const { allowedOrigins = [], origin: configOrigin } = getConfig(); - if (federatedContentRoot) return federatedContentRoot; - // Non milo consumers will have its origin from config - const origin = configOrigin || window.location.origin; - - federatedContentRoot = [...allowedOrigins, ...cdnWhitelistedOrigins].some((o) => origin.replace('.stage', '') === o) - ? origin - : 'https://www.adobe.com'; - - if (origin.includes('localhost') || origin.includes(`.${SLD}.`)) { - federatedContentRoot = `https://main--federal--adobecom.aem.${origin.endsWith('.live') ? 'live' : 'page'}`; - } - - return federatedContentRoot; -}; - -// TODO we should match the akamai patterns /locale/federal/ at the start of the url -// and make the check more strict. -export const getFederatedUrl = (url = '') => { - if (typeof url !== 'string' || !url.includes('/federal/')) return url; - if (url.startsWith('/')) return `${getFederatedContentRoot()}${url}`; - try { - const { pathname, search, hash } = new URL(url); - return `${getFederatedContentRoot()}${pathname}${search}${hash}`; - } catch (e) { - window.lana?.log(`getFederatedUrl errored parsing the URL: ${url}: ${e.toString()}`); - } - return url; -}; - -let fedsPlaceholderConfig; -export const getFedsPlaceholderConfig = ({ useCache = true } = {}) => { - if (useCache && fedsPlaceholderConfig) return fedsPlaceholderConfig; - - const { locale, placeholders } = getConfig(); - const libOrigin = getFederatedContentRoot(); - - fedsPlaceholderConfig = { - locale: { - ...locale, - contentRoot: `${libOrigin}${locale.prefix}/federal/globalnav`, - }, - placeholders, - }; - - return fedsPlaceholderConfig; -}; +import { getFederatedContentRoot, getFederatedUrl, getFedsPlaceholderConfig } from './utils.js'; +// DO NOT USE THIS FILE, THIS IS HERE FOR HISTORY PURPOSES AND AVOIDING BREAKING CHANGES. +export { getFederatedContentRoot, getFederatedUrl, getFedsPlaceholderConfig }; diff --git a/libs/utils/utils.js b/libs/utils/utils.js index fba5117d5b..da4d558c4a 100644 --- a/libs/utils/utils.js +++ b/libs/utils/utils.js @@ -263,6 +263,64 @@ export const [setConfig, updateConfig, getConfig] = (() => { ]; })(); +let federatedContentRoot; +/* eslint-disable import/prefer-default-export */ +export const getFederatedContentRoot = () => { + const cdnWhitelistedOrigins = [ + 'https://www.adobe.com', + 'https://business.adobe.com', + 'https://blog.adobe.com', + 'https://milo.adobe.com', + 'https://news.adobe.com', + ]; + const { allowedOrigins = [], origin: configOrigin } = getConfig(); + if (federatedContentRoot) return federatedContentRoot; + // Non milo consumers will have its origin from config + const origin = configOrigin || window.location.origin; + + federatedContentRoot = [...allowedOrigins, ...cdnWhitelistedOrigins].some((o) => origin.replace('.stage', '') === o) + ? origin + : 'https://www.adobe.com'; + + if (origin.includes('localhost') || origin.includes(`.${SLD}.`)) { + federatedContentRoot = `https://main--federal--adobecom.aem.${origin.endsWith('.live') ? 'live' : 'page'}`; + } + + return federatedContentRoot; +}; + +// TODO we should match the akamai patterns /locale/federal/ at the start of the url +// and make the check more strict. +export const getFederatedUrl = (url = '') => { + if (typeof url !== 'string' || !url.includes('/federal/')) return url; + if (url.startsWith('/')) return `${getFederatedContentRoot()}${url}`; + try { + const { pathname, search, hash } = new URL(url); + return `${getFederatedContentRoot()}${pathname}${search}${hash}`; + } catch (e) { + window.lana?.log(`getFederatedUrl errored parsing the URL: ${url}: ${e.toString()}`); + } + return url; +}; + +let fedsPlaceholderConfig; +export const getFedsPlaceholderConfig = ({ useCache = true } = {}) => { + if (useCache && fedsPlaceholderConfig) return fedsPlaceholderConfig; + + const { locale, placeholders } = getConfig(); + const libOrigin = getFederatedContentRoot(); + + fedsPlaceholderConfig = { + locale: { + ...locale, + contentRoot: `${libOrigin}${locale.prefix}/federal/globalnav`, + }, + placeholders, + }; + + return fedsPlaceholderConfig; +}; + export function isInTextNode(node) { return node.parentElement.firstChild.nodeType === Node.TEXT_NODE; } diff --git a/test/blocks/global-navigation/utilities/utilities.test.js b/test/blocks/global-navigation/utilities/utilities.test.js index 9acf19d2c7..133cceb64e 100644 --- a/test/blocks/global-navigation/utilities/utilities.test.js +++ b/test/blocks/global-navigation/utilities/utilities.test.js @@ -17,11 +17,10 @@ import { dropWhile, getBranchBannerInfo, } from '../../../../libs/blocks/global-navigation/utilities/utilities.js'; -import { setConfig, getConfig } from '../../../../libs/utils/utils.js'; +import { setConfig, getConfig, getFedsPlaceholderConfig } from '../../../../libs/utils/utils.js'; import { createFullGlobalNavigation, config, mockRes } from '../test-utilities.js'; import gnavWithlocalNav from '../mocks/gnav-with-localnav.plain.js'; import mepInBlock from '../mocks/mep-config.js'; -import { getFedsPlaceholderConfig } from '../../../../libs/utils/federated.js'; const baseHost = 'https://main--federal--adobecom.aem.page'; describe('global navigation utilities', () => { diff --git a/test/features/georoutingv2/georoutingv2.test.js b/test/features/georoutingv2/georoutingv2.test.js index bab9b39c14..199702e833 100644 --- a/test/features/georoutingv2/georoutingv2.test.js +++ b/test/features/georoutingv2/georoutingv2.test.js @@ -4,8 +4,7 @@ import { setViewport } from '@web/test-runner-commands'; const { default: init, getCookie } = await import('../../../libs/features/georoutingv2/georoutingv2.js'); let { getMetadata } = await import('../../../libs/utils/utils.js'); -const { getFederatedContentRoot } = await import('../../../libs/utils/federated.js'); -const { createTag, loadStyle, loadBlock, setConfig } = await import('../../../libs/utils/utils.js'); +const { createTag, loadStyle, loadBlock, setConfig, getFederatedContentRoot } = await import('../../../libs/utils/utils.js'); const mockConfig = { locales: { diff --git a/test/utils/federated.test.js b/test/utils/federated.test.js index cc19a72ff3..909a140be2 100644 --- a/test/utils/federated.test.js +++ b/test/utils/federated.test.js @@ -1,5 +1,5 @@ import { expect } from '@esm-bundle/chai'; -import { getFederatedUrl, getFederatedContentRoot } from '../../libs/utils/federated.js'; +import { getFederatedUrl, getFederatedContentRoot } from '../../libs/utils/utils.js'; const baseHost = 'https://main--federal--adobecom.aem.page';